DeepTutor: Lifelong Personalized Tutoring. https://deeptutor.info/.
Python ai-agentsai-tutorclawbotcli-tooldeepresearchinteractive-learninglearningmulti-agent-systemsrag
fix(web): improve robust parsing for LaTeX block math
sagnikonlya2808cf7befd
diff --git a/web/lib/latex.ts b/web/lib/latex.tsindex 62532eedb..5c0c2ae73 100644--- a/web/lib/latex.ts+++ b/web/lib/latex.ts@@ -20,16 +20,22 @@ export function convertLatexDelimiters(content: string): string {// editor.md examples sometimes wrap \( ... \) inside $$ ... $$.// In that case the inner delimiters should be stripped rather than rewrapped.- result = result.replace(/\$\$\s*\\\(([\s\S]*?)\\\)\s*\$\$/g, "\n$$\n$1\n$$\n");+ result = result.replace(/\$\$\s*\\\(([\s\S]*?)\\\)\s*\$\$/g, (_match, expr) => {+ return `\n$$\n${expr}\n$$\n`;+ });- // Convert \[...\] to $$...$$ (block math)+ // Convert \[...\] to $$...$$ (block math).// Use a regex that handles multiline content// Note: In JSON strings, \[ becomes \\[ which in JS becomes \[- result = result.replace(/\\\[([\s\S]*?)\\\]/g, "\n$$\n$1\n$$\n");+ result = result.replace(/\\\[([\s\S]*?)\\\]/g, (_match, expr) => {+ return `\n$$\n${expr}\n$$\n`;+ });- // Convert \(...\) to $...$ (inline math)+ // Convert \(...\) to $...$ (inline math).// Be careful not to match escaped parentheses in other contexts- result = result.replace(/\\\(([\s\S]*?)\\\)/g, " $$$1$$ ");+ result = result.replace(/\\\(([\s\S]*?)\\\)/g, (_match, expr) => {+ return ` $${expr}$ `;+ });// Also handle cases where LaTeX is directly in the text without proper delimiters// e.g., standalone \lim, \frac, etc. that should be wrapped@@ -42,32 +48,67 @@ export function convertLatexDelimiters(content: string): string {}function normalizeEditorMdHeadings(content: string): string {- return content.replace(/^(#{1,6})(\S)/gm, "$1 $2");+ return content.replace(/^(#{1,6})([^#\s])/gm, "$1 $2");+}++const LIKELY_LATEX_BLOCK_RE = /\\[A-Za-z]+|\\\\|[_^&]/;++function looksLikeLatexBlock(lines: string[]): boolean {+ const block = lines+ .map((line) => line.trim())+ .filter(Boolean)+ .join("\n");++ return block.length > 0 && LIKELY_LATEX_BLOCK_RE.test(block);}function normalizeEditorMdInlineMath(content: string): string {- return content- .split("\n")- .map((line) => {- const trimmed = line.trim();-- // Preserve block math on its own line, but normalize one-line block math.- if (- /^\$\$[\s\S]+\$\$$/.test(trimmed) &&- (trimmed.match(/\$\$/g)?.length ?? 0) === 2- ) {- if (line.trim() === trimmed) {- const inner = trimmed.slice(2, -2).trim();- return `$$\n${inner}\n$$`;+ const lines = content.split("\n");+ const result: string[] = [];++ for (let i = 0; i < lines.length; i++) {+ const line = lines[i];+ const trimmed = line.trim();++ if (trimmed === "$" && i + 1 < lines.length) {+ let endIdx = -1;+ for (let j = i + 1; j < lines.length; j++) {+ if (lines[j].trim() === "$") {+ endIdx = j;+ break;}}- // editor.md commonly uses $$...$$ for inline math.- return line.replace(/\$\$([^$\n]+?)\$\$/g, (_match, expr: string) => {- return `$${expr.trim()}$`;- });- })- .join("\n");+ if (endIdx > i + 1 && looksLikeLatexBlock(lines.slice(i + 1, endIdx))) {+ result.push("$$");+ for (let j = i + 1; j < endIdx; j++) {+ result.push(lines[j]);+ }+ result.push("$$");+ i = endIdx;+ continue;+ }+ }++ // Preserve block math on its own line, but normalize one-line block math.+ if (+ /^\$\$[\s\S]+\$\$/.test(trimmed) &&+ (trimmed.match(/\$\$/g)?.length ?? 0) === 2+ ) {+ if (line.trim() === trimmed) {+ const inner = trimmed.slice(2, -2).trim();+ result.push(`$$\n${inner}\n$$`);+ continue;+ }+ }++ // editor.md commonly uses $...$ for inline math.+ result.push(line.replace(/\$\$([^$\n]+?)\$\$/g, (_match, expr: string) => {+ return `$${expr.trim()}$`;+ }));+ }++ return result.join("\n");}type HeadingEntry = {