fix latex in codeblock behavior

This commit is contained in:
7w1 committed 2026-05-06 11:51:47 -05:00
1 parent b5373133ff
commit 861cce92ad
4 files changed
+195 -4

No files matched your search

+5
View File
@@ -0,0 +1,5 @@
---
default: patch
---
Fix latex in codeblocks getting parsed.
@@ -1,5 +1,160 @@
import type { TokenizerExtension, RendererExtension } from 'marked';
/** Private-use char so math extensions do not match `$` / `$$` inside code spans. Not U+E000–U+E002 (emoticon placeholders). */
export const MATH_CODE_DOLLAR_MASK = '\uE020';
function findSameLineFenceClose(md: string, from: number, tick: string, minLen: number): number {
let j = from;
while (j < md.length && md[j] !== '\n') {
if (md[j] === tick) {
let run = 0;
while (j + run < md.length && md[j + run] === tick) run++;
if (run >= minLen) return j;
j += run;
} else {
j++;
}
}
return -1;
}
function findMultilineFenceEnd(
md: string,
contentStart: number,
tick: string,
minLen: number
): { blockEnd: number; contentEnd: number } | null {
let p = contentStart;
while (p <= md.length) {
const nl = md.indexOf('\n', p);
const lineStart = p;
const lineEnd = nl === -1 ? md.length : nl;
const line = md.slice(lineStart, lineEnd);
const m = tick === '`' ? /^ {0,3}(`{3,})\s*$/.exec(line) : /^ {0,3}(~{3,})\s*$/.exec(line);
const fenceRun = m?.[1];
if (fenceRun && fenceRun.length >= minLen && fenceRun[0] === tick) {
return {
blockEnd: nl === -1 ? md.length : nl + 1,
contentEnd: lineStart,
};
}
if (nl === -1) return null;
p = nl + 1;
}
return null;
}
function tryConsumeFence(md: string, i: number): { text: string; end: number } | null {
const atLineStart = i === 0 || md[i - 1] === '\n';
if (!atLineStart) return null;
const rest = md.slice(i);
const open = /^(\s{0,3})(`{3,}|~{3,})/.exec(rest);
if (!open?.[2]) return null;
const fenceStr = open[2];
const tick = fenceStr.charAt(0);
const openLen = fenceStr.length;
const afterOpen = i + open[0].length;
if (afterOpen < md.length && md[afterOpen] === '\n') {
const contentStart = afterOpen + 1;
const close = findMultilineFenceEnd(md, contentStart, tick, openLen);
if (!close) {
const inner = md.slice(contentStart, md.length);
const masked = inner.replace(/\$/g, MATH_CODE_DOLLAR_MASK);
return { text: md.slice(i, contentStart) + masked, end: md.length };
}
const inner = md.slice(contentStart, close.contentEnd);
const maskedInner = inner.replace(/\$/g, MATH_CODE_DOLLAR_MASK);
return {
text: md.slice(i, contentStart) + maskedInner + md.slice(close.contentEnd, close.blockEnd),
end: close.blockEnd,
};
}
const closeIdx = findSameLineFenceClose(md, afterOpen, tick, openLen);
if (closeIdx < 0) return null;
let closeRun = 0;
while (closeIdx + closeRun < md.length && md[closeIdx + closeRun] === tick) closeRun++;
const inner = md.slice(afterOpen, closeIdx);
const maskedInner = inner.replace(/\$/g, MATH_CODE_DOLLAR_MASK);
return {
text: md.slice(i, afterOpen) + maskedInner + md.slice(closeIdx, closeIdx + closeRun),
end: closeIdx + closeRun,
};
}
function tryConsumeInlineCode(md: string, i: number): { text: string; end: number } | null {
if (md[i] !== '`') return null;
let run = 0;
while (i + run < md.length && md[i + run] === '`') run++;
const contentStart = i + run;
let j = contentStart;
while (j < md.length) {
if (md[j] === '`') {
let cr = 0;
while (j + cr < md.length && md[j + cr] === '`') cr++;
if (cr === run) {
const inner = md.slice(contentStart, j);
const maskedInner = inner.replace(/\$/g, MATH_CODE_DOLLAR_MASK);
return {
text: md.slice(i, contentStart) + maskedInner + md.slice(j, j + run),
end: j + run,
};
}
j += cr;
} else {
j++;
}
}
return null;
}
/**
* Replaces `$` inside fenced and inline code so Matrix math extensions do not run on code literals.
* {@link unmaskMathCodeDollarPlaceholders} must be applied to the final HTML.
*/
export function maskDollarSignsInsideMarkdownCode(markdown: string): string {
const md = markdown.replace(/\r\n/g, '\n');
let out = '';
let i = 0;
const n = md.length;
while (i < n) {
const atLineStart = i === 0 || md[i - 1] === '\n';
if (atLineStart) {
const fence = tryConsumeFence(md, i);
if (fence) {
out += fence.text;
i = fence.end;
continue;
}
}
if (md[i] === '`') {
const span = tryConsumeInlineCode(md, i);
if (span) {
out += span.text;
i = span.end;
continue;
}
}
out += md[i];
i++;
}
return out;
}
export function unmaskMathCodeDollarPlaceholders(html: string): string {
return html.replaceAll(MATH_CODE_DOLLAR_MASK, '$');
}
function escapeHtml(text: string): string {
return text
.replace(/&/g, '&amp;')
@@ -40,6 +40,27 @@ describe('markdownToHtml', () => {
expect(result).toContain('E = mc^2');
});
it('does not parse dollars inside fenced code as math', () => {
expect(markdownToHtml('```\n$$test$$\n```')).not.toContain('data-mx-maths');
expect(markdownToHtml('```\n$$test$$\n```')).toContain('$$test$$');
});
it('does not parse dollars inside single-line fenced code as math', () => {
expect(markdownToHtml('```$$test$$```')).not.toContain('data-mx-maths');
expect(markdownToHtml('```$$test$$```')).toContain('$$test$$');
});
it('does not parse dollars inside inline code as math', () => {
expect(markdownToHtml('`$$test$$`')).not.toContain('data-mx-maths');
expect(markdownToHtml('`$$test$$`')).toContain('$$test$$');
});
it('does not parse inline math when dollars are only inside backticks in a sentence', () => {
const result = markdownToHtml('See `$$test$$` here.');
expect(result).not.toContain('data-mx-maths');
expect(result).toContain('$$test$$');
});
it('converts block math syntax', () => {
const result = markdownToHtml('$$\\frac{a}{b}$$');
expect(result).toContain('data-mx-maths');
+14 -4
View File
@@ -1,7 +1,12 @@
import { marked } from 'marked';
import DOMPurify from 'dompurify';
import { matrixSpoilerExtension } from './extensions/matrix-spoiler';
import { matrixMathExtension, matrixMathBlockExtension } from './extensions/matrix-math';
import {
matrixMathExtension,
matrixMathBlockExtension,
maskDollarSignsInsideMarkdownCode,
unmaskMathCodeDollarPlaceholders,
} from './extensions/matrix-math';
import { matrixSubscriptExtension } from './extensions/matrix-subscript';
import { matrixEmoticonExtension, preprocessEmoticon } from './extensions/matrix-emoticon';
import {
@@ -53,13 +58,15 @@ export function markdownToHtml(markdown: string): string {
// (e.g., &lt; becomes < for link URLs)
const decoded = decodeHtmlEntities(markdown);
// Only treat `> ` as block quote; escape bare `>` at line start (e.g. `>:3`) for CommonMark.
// Only treat `> ` as block quote, escape bare `>` at line start (e.g. `>:3`)
const blockquotePrefixed = escapeLineStartBlockquoteWithoutFollowingSpace(decoded);
const preprocessed = preprocessEmoticon(blockquotePrefixed);
const mathInput = maskDollarSignsInsideMarkdownCode(preprocessed);
// Parse markdown to HTML using marked with our Matrix extensions
const html = processor.parse(preprocessed) as string;
const html = processor.parse(mathInput) as string;
// Unescape inline sequences (e.g., \*, \_) after parsing
const unescapedInline = unescapeMarkdownInlineSequences(html);
@@ -139,5 +146,8 @@ export function markdownToHtml(markdown: string): string {
DOMPurify.removeHook('afterSanitizeAttributes');
return sanitized.replace(/<li>(<p><\/p>)?<\/li>/gi, '<li><br></li>');
return unmaskMathCodeDollarPlaceholders(sanitized).replace(
/<li>(<p><\/p>)?<\/li>/gi,
'<li><br></li>'
);
}