@@ -27,15 +27,16 @@ function focusSink() {
2727function 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 }
0 commit comments