Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: allow some punctuation after inline katex #196

Merged
merged 3 commits into from
Aug 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions spec/__snapshots__/index.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,31 @@ exports[`marked-katex-extension inline katex with $ inside 1`] = `
"
`;

exports[`marked-katex-extension inline katex with a colon after 1`] = `
"<p>this is inline katex: <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>x</mi></mrow><annotation encoding="application/x-tex">x</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.4306em;"></span><span class="mord mathnormal">x</span></span></span></span>:</p>
"
`;

exports[`marked-katex-extension inline katex with a comma after 1`] = `
"<p>this is inline katex: <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>x</mi></mrow><annotation encoding="application/x-tex">x</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.4306em;"></span><span class="mord mathnormal">x</span></span></span></span>,</p>
"
`;

exports[`marked-katex-extension inline katex with a period after 1`] = `
"<p>this is inline katex: <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>x</mi></mrow><annotation encoding="application/x-tex">x</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.4306em;"></span><span class="mord mathnormal">x</span></span></span></span>.</p>
"
`;

exports[`marked-katex-extension inline katex with a question mark after 1`] = `
"<p>this is inline katex: <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>x</mi></mrow><annotation encoding="application/x-tex">x</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.4306em;"></span><span class="mord mathnormal">x</span></span></span></span>?</p>
"
`;

exports[`marked-katex-extension inline katex with an exclamation mark after 1`] = `
"<p>this is inline katex: <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>x</mi></mrow><annotation encoding="application/x-tex">x</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.4306em;"></span><span class="mord mathnormal">x</span></span></span></span>!</p>
"
`;

exports[`marked-katex-extension inline katex with newline 1`] = `
"<p>this is not katex: $
c = \\pm\\sqrt{a^2 + b^2}
Expand Down
5 changes: 5 additions & 0 deletions spec/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ a$}
$$
`,
'inline katex with $ inside': 'this is inline katex: $a\\raisebox{0.25em}{$b$}c$',
'inline katex with a question mark after': 'this is inline katex: $x$?',
'inline katex with an exclamation mark after': 'this is inline katex: $x$!',
'inline katex with a period after': 'this is inline katex: $x$.',
'inline katex with a comma after': 'this is inline katex: $x$,',
'inline katex with a colon after': 'this is inline katex: $x$:',
'inline katex $$...$': 'this is not katex: $$a\\raisebox{0.25em}{$b$}c$',
'inline katex $...$$': 'this is not katex: $a\\raisebox{0.25em}{$b$}c$$',
'slash $': 'must include space between katex and end delimiter: $ \\$ $',
Expand Down
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import katex from 'katex';

const inlineStartRule = /(?<=\s|^)\${1,2}(?!\$)/;
const inlineRule = /^(\${1,2})(?!\$)((?:\\.|[^\\\n])+?)(?<!\$)\1(?=\s|$)/;
const inlineRule = /^(\${1,2})(?!\$)((?:\\.|[^\\\n])+?)(?<!\$)\1(?=[\s?!\.,:]|$)/;
const blockRule = /^(\${1,2})\n((?:\\[^]|[^\\])+?)\n\1(?:\n|$)/;

export default function(options = {}) {
Expand Down