@@ -226,19 +226,16 @@ export function preprocessLaTeX(content: string): string {
226226 return expr ;
227227 } ) ;
228228
229- // Step 5: Restore code blocks
230- content = content . replace ( / < < C O D E _ B L O C K _ ( \d + ) > > / g, ( _ , index ) => {
231- return codeBlocks [ parseInt ( index ) ] ;
232- } ) ;
233-
234- // Step 6: Apply additional escaping functions (brackets and mhchem)
229+ // Step 5: Apply additional escaping functions (brackets and mhchem)
230+ // This must happen BEFORE restoring code blocks to avoid affecting code content
235231 content = escapeBrackets ( content ) ;
236232
237233 if ( doEscapeMhchem && ( content . includes ( '\\ce{' ) || content . includes ( '\\pu{' ) ) ) {
238234 content = escapeMhchem ( content ) ;
239235 }
240236
241- // Final pass: Convert \(...\) → $...$, \[...\] → $$...$$
237+ // Step 6: Convert remaining \(...\) → $...$, \[...\] → $$...$$
238+ // This must happen BEFORE restoring code blocks to avoid affecting code content
242239 content = content
243240 // Using the look‑behind pattern `(?<!\\)` we skip matches
244241 // that are preceded by a backslash, e.g.
@@ -248,12 +245,18 @@ export function preprocessLaTeX(content: string): string {
248245 // Using the look‑behind pattern `(?<!\\)` we skip matches
249246 // that are preceded by a backslash, e.g. `\\[4pt]`.
250247 / (?< ! \\ ) \\ \[ ( [ \s \S ] * ?) \\ \] / g, // display, see also PR #16599
251- ( _ , prefix : string , content : string ) => {
252- return `${ prefix } $ $${ content } $$` ;
248+ ( _ , content : string ) => {
249+ return `$$${ content } $$` ;
253250 }
254251 ) ;
255252
256- // Step 7: Restore blockquote markers
253+ // Step 7: Restore code blocks
254+ // This happens AFTER all LaTeX conversions to preserve code content
255+ content = content . replace ( / < < C O D E _ B L O C K _ ( \d + ) > > / g, ( _ , index ) => {
256+ return codeBlocks [ parseInt ( index ) ] ;
257+ } ) ;
258+
259+ // Step 8: Restore blockquote markers
257260 if ( blockquoteMarkers . size > 0 ) {
258261 const finalLines = content . split ( '\n' ) ;
259262 const restoredLines = finalLines . map ( ( line , index ) => {
0 commit comments