fix: Fix KaTeX corner cases

This commit is contained in:
Hwang In Tak 2024-09-25 02:15:53 +09:00
parent 07b1327708
commit d501ece247
No known key found for this signature in database

View File

@ -1,14 +1,13 @@
import katex from 'katex'; import katex from 'katex';
const DELIMITER_LIST = [ const DELIMITER_LIST = [
{ left: '$$\n', right: '\n$$', display: true }, { left: '$$', right: '$$', display: true },
{ left: '$$', right: '$$', display: false }, // This should be on top to prevent conflict with $ delimiter
{ left: '$', right: '$', display: false }, { left: '$', right: '$', display: false },
{ left: '\\pu{', right: '}', display: false }, { left: '\\pu{', right: '}', display: false },
{ left: '\\ce{', right: '}', display: false }, { left: '\\ce{', right: '}', display: false },
{ left: '\\(', right: '\\)', display: false }, { left: '\\(', right: '\\)', display: false },
{ left: '\\[\n', right: '\n\\]', display: true }, { left: '\\[', right: '\\]', display: true },
{ left: '\\[', right: '\\]', display: false } { left: '\\begin{equation}', right: '\\end{equation}', display: true }
]; ];
// const DELIMITER_LIST = [ // const DELIMITER_LIST = [
@ -34,14 +33,18 @@ function generateRegexRules(delimiters) {
const escapedRight = escapeRegex(right); const escapedRight = escapeRegex(right);
if (!display) { if (!display) {
// For inline delimiters, we match everyting
inlinePatterns.push(`${escapedLeft}((?:\\\\[^]|[^\\\\])+?)${escapedRight}`); inlinePatterns.push(`${escapedLeft}((?:\\\\[^]|[^\\\\])+?)${escapedRight}`);
} else { } else {
blockPatterns.push(`${escapedLeft}((?:\\\\[^]|[^\\\\])+?)${escapedRight}`); // Block delimiters doubles as inline delimiters when not followed by a newline
inlinePatterns.push(`${escapedLeft}(?!\\n)((?:\\\\[^]|[^\\\\])+?)(?!\\n)${escapedRight}`);
blockPatterns.push(`${escapedLeft}\\n((?:\\\\[^]|[^\\\\])+?)\\n${escapedRight}`);
} }
}); });
const inlineRule = new RegExp(`^(${inlinePatterns.join('|')})(?=[\\s?!.,:?!。,:]|$)`, 'u'); // Math formulas can end in special characters
const blockRule = new RegExp(`^(${blockPatterns.join('|')})(?=[\\s?!.,:?!。,:]|$)`, 'u'); const inlineRule = new RegExp(`^(${inlinePatterns.join('|')})(?=[\\s?。,!-\/:-@[-\`{-~]|$)`, 'u');
const blockRule = new RegExp(`^(${blockPatterns.join('|')})(?=[\\s?。,!-\/:-@[-\`{-~]|$)`, 'u');
return { inlineRule, blockRule }; return { inlineRule, blockRule };
} }
@ -51,8 +54,8 @@ const { inlineRule, blockRule } = generateRegexRules(DELIMITER_LIST);
export default function (options = {}) { export default function (options = {}) {
return { return {
extensions: [ extensions: [
blockKatex(options), // This should be on top to prevent conflict with inline delimiters. inlineKatex(options),
inlineKatex(options) blockKatex(options),
] ]
}; };
} }
@ -86,7 +89,9 @@ function katexStart(src, displayMode: boolean) {
return; return;
} }
const f = index === 0 || indexSrc.charAt(index - 1) === ' '; // Check if the delimiter is preceded by a special character.
// If it does, then it's potentially a math formula.
const f = index === 0 || indexSrc.charAt(index - 1).match(/[\s?。,!-\/:-@[-`{-~]/);
if (f) { if (f) {
const possibleKatex = indexSrc.substring(index); const possibleKatex = indexSrc.substring(index);