Skip to content

Commit 1a0bcc5

Browse files
CopilotDiraw
andcommitted
Address code review feedback - improve delimiter pairing validation
Co-authored-by: Diraw <139880164+Diraw@users.noreply.github.com>
1 parent e16c33d commit 1a0bcc5

2 files changed

Lines changed: 20 additions & 18 deletions

File tree

assets/templates/result_logic.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,16 @@ function focusSink() {
2727
function render(md) {
2828
if (typeof marked==='undefined') return;
2929
// Step 1: Extract LaTeX from backticks (e.g., `$formula$` -> $formula$)
30+
// Only unwrap if the backticked content is a valid LaTeX expression (properly paired delimiters)
3031
var text = md || '';
31-
// Match backtick-wrapped content that contains LaTeX delimiters
32-
// Pattern matches: `...content with $ or \( or \[ or $$...`
33-
text = text.replace(/`([^`]*(?:\$\$|\\\[|\\\(|\$)[^`]*)`/g, function(match, inner) {
34-
// If the backticked content contains LaTeX delimiters, unwrap it
35-
if (/\$\$|\\\[|\\\]|\\\(|\\\)|\$/.test(inner)) {
36-
return inner;
32+
text = text.replace(/`([^`]+)`/g, function(match, inner) {
33+
// Check if content is a complete LaTeX expression with properly paired delimiters
34+
// Match: $...$, $$...$$, \(...\), or \[...\]
35+
// Using [\s\S]* to allow empty or any content including newlines
36+
if (/^(?:\$\$[\s\S]*?\$\$|\$[^\$\n]+\$|\\\([\s\S]*?\\\)|\\\[[\s\S]*?\\\])$/.test(inner.trim())) {
37+
return inner; // Unwrap backticks
3738
}
38-
return match; // Keep original if no LaTeX found
39+
return match; // Keep original backticks if not valid LaTeX
3940
});
4041

4142
// Step 2: Protect all LaTeX expressions before markdown parsing
@@ -65,9 +66,8 @@ function render(md) {
6566
{left:'\\(', right:'\\)', display:false},
6667
{left:'\\[', right:'\\]', display:true}
6768
],
68-
throwOnError: false,
69-
ignoredTags: [], // Allow rendering in all tags
70-
ignoredClasses: [] // Allow rendering in all classes
69+
throwOnError: false
70+
// Use KaTeX defaults for ignoredTags/ignoredClasses for safety
7171
});
7272
}
7373
}

assets/templates/summary_logic.js

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -71,12 +71,15 @@ function renderContent(id, markdownOverride) {
7171
var markdown = (typeof markdownOverride === 'string') ? markdownOverride : (raw ? raw.textContent : '');
7272

7373
// Step 1: Extract LaTeX from backticks (e.g., `$formula$` -> $formula$)
74-
markdown = markdown.replace(/`([^`]*(?:\$\$|\\\[|\\\(|\$)[^`]*)`/g, function(match, inner) {
75-
// If the backticked content contains LaTeX delimiters, unwrap it
76-
if (/\$\$|\\\[|\\\]|\\\(|\\\)|\$/.test(inner)) {
77-
return inner;
74+
// Only unwrap if the backticked content is a valid LaTeX expression (properly paired delimiters)
75+
markdown = markdown.replace(/`([^`]+)`/g, function(match, inner) {
76+
// Check if content is a complete LaTeX expression with properly paired delimiters
77+
// Match: $...$, $$...$$, \(...\), or \[...\]
78+
// Using [\s\S]* to allow empty or any content including newlines
79+
if (/^(?:\$\$[\s\S]*?\$\$|\$[^\$\n]+\$|\\\([\s\S]*?\\\)|\\\[[\s\S]*?\\\])$/.test(inner.trim())) {
80+
return inner; // Unwrap backticks
7881
}
79-
return match; // Keep original if no LaTeX found
82+
return match; // Keep original backticks if not valid LaTeX
8083
});
8184

8285
// Step 2: Protect math expressions before markdown parsing
@@ -101,9 +104,8 @@ function renderContent(id, markdownOverride) {
101104
{left: '\\(', right: '\\)', display: false},
102105
{left: '\\[', right: '\\]', display: true}
103106
],
104-
throwOnError: false,
105-
ignoredTags: [], // Allow rendering in all tags
106-
ignoredClasses: [] // Allow rendering in all classes
107+
throwOnError: false
108+
// Use KaTeX defaults for ignoredTags/ignoredClasses for safety
107109
});
108110
});
109111
}

0 commit comments

Comments
 (0)