2024-01-18 14:44:10 -05:00
|
|
|
// Regex to check if the processed content contains any potential LaTeX patterns
|
|
|
|
|
const containsLatexRegex =
|
|
|
|
|
/\\\(.*?\\\)|\\\[.*?\\\]|\$.*?\$|\\begin\{equation\}.*?\\end\{equation\}/;
|
2024-01-22 10:02:36 -05:00
|
|
|
|
2024-01-18 14:44:10 -05:00
|
|
|
// Regex for inline and block LaTeX expressions
|
|
|
|
|
const inlineLatex = new RegExp(/\\\((.+?)\\\)/, 'g');
|
|
|
|
|
const blockLatex = new RegExp(/\\\[(.*?[^\\])\\\]/, 'gs');
|
|
|
|
|
|
2024-01-22 10:02:36 -05:00
|
|
|
// Function to restore code blocks
|
|
|
|
|
const restoreCodeBlocks = (content: string, codeBlocks: string[]) => {
|
|
|
|
|
return content.replace(/<<CODE_BLOCK_(\d+)>>/g, (match, index) => codeBlocks[index]);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Regex to identify code blocks and inline code
|
|
|
|
|
const codeBlockRegex = /(```[\s\S]*?```|`.*?`)/g;
|
|
|
|
|
|
|
|
|
|
export const processLaTeX = (_content: string) => {
|
|
|
|
|
let content = _content;
|
|
|
|
|
// Temporarily replace code blocks and inline code with placeholders
|
|
|
|
|
const codeBlocks: string[] = [];
|
|
|
|
|
let index = 0;
|
|
|
|
|
content = content.replace(codeBlockRegex, (match) => {
|
|
|
|
|
codeBlocks[index] = match;
|
|
|
|
|
return `<<CODE_BLOCK_${index++}>>`;
|
|
|
|
|
});
|
|
|
|
|
|
2024-01-18 14:44:10 -05:00
|
|
|
// Escape dollar signs followed by a digit or space and digit
|
|
|
|
|
let processedContent = content.replace(/(\$)(?=\s?\d)/g, '\\$');
|
|
|
|
|
|
2024-01-22 10:02:36 -05:00
|
|
|
// If no LaTeX patterns are found, restore code blocks and return the processed content
|
2024-01-18 14:44:10 -05:00
|
|
|
if (!containsLatexRegex.test(processedContent)) {
|
2024-01-22 10:02:36 -05:00
|
|
|
return restoreCodeBlocks(processedContent, codeBlocks);
|
2024-01-18 14:44:10 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Convert LaTeX expressions to a markdown compatible format
|
|
|
|
|
processedContent = processedContent
|
|
|
|
|
.replace(inlineLatex, (match: string, equation: string) => `$${equation}$`) // Convert inline LaTeX
|
|
|
|
|
.replace(blockLatex, (match: string, equation: string) => `$$${equation}$$`); // Convert block LaTeX
|
|
|
|
|
|
2024-01-22 10:02:36 -05:00
|
|
|
// Restore code blocks
|
|
|
|
|
return restoreCodeBlocks(processedContent, codeBlocks);
|
2024-01-18 14:44:10 -05:00
|
|
|
};
|