diff --git a/src/index.ts b/src/index.ts index 43a3054..9c4e741 100644 --- a/src/index.ts +++ b/src/index.ts @@ -462,6 +462,7 @@ export default function (md: import('markdown-it'), options?: MarkdownKatexOptio const enableBareBlocks = options?.enableBareBlocks; const enableMathBlockInHtml = options?.enableMathBlockInHtml; const enableMathInlineInHtml = options?.enableMathInlineInHtml; + const enableFencedBlocks = options?.enableFencedBlocks; const katexInline = (latex: string) => { const displayMode = /\\begin\{(align|equation|gather|cd|alignat)\}/ig.test(latex); @@ -494,6 +495,7 @@ export default function (md: import('markdown-it'), options?: MarkdownKatexOptio return katexBlockRenderer(tokens[idx].content) + '\n'; } + md.inline.ruler.after('escape', 'math_inline', inlineMath); md.inline.ruler.after('escape', 'math_inline_block', inlineMathBlock); if (enableBareBlocks) { @@ -509,6 +511,16 @@ export default function (md: import('markdown-it'), options?: MarkdownKatexOptio alt: ['paragraph', 'reference', 'blockquote', 'list'] }); + const originalFenceRenderer = md.renderer.rules.fence; + md.renderer.rules.fence = function (tokens: Token[], idx: number, options, env, self) { + const token = tokens[idx]; + if (token.info.trim() === 'math' && enableFencedBlocks) { + return blockRenderer([token], 0); + } else { + return originalFenceRenderer?.call(this, tokens, idx, options, env, self) || ''; + } + }; + // Regex to capture any html prior to math block, the math block (single or multi line), and any html after the math block const math_block_within_html_regex = /(?[\s\S]*?)\$\$(?[\s\S]+?)\$\$(?(?:(?!\$\$[\s\S]+?\$\$)[\s\S])*)/gm; diff --git a/test/all.js b/test/all.js index 9538ddd..d01ee71 100644 --- a/test/all.js +++ b/test/all.js @@ -50,3 +50,5 @@ runTest(path.join(__dirname, 'fixtures', 'default.txt'), mdIt({ html: true }).us runTest(path.join(__dirname, 'fixtures', 'bare.txt'), mdIt().use(mdk, { enableBareBlocks: true })); runTest(path.join(__dirname, 'fixtures', 'math-in-html.txt'), mdIt({ html: true }).use(mdk, { enableMathBlockInHtml: true, enableMathInlineInHtml: true })); + +runTest(path.join(__dirname, 'fixtures', 'fence.txt'), mdIt({ html: true }).use(mdk, { enableFencedBlocks: true })); diff --git a/test/fixtures/fence.txt b/test/fixtures/fence.txt new file mode 100644 index 0000000..62c5573 --- /dev/null +++ b/test/fixtures/fence.txt @@ -0,0 +1,85 @@ +Simple fence block with backticks +. +start + +```math +2 +``` + +end +. +

start

+

22 +

+

end

+. + +Simple fence block with tildes +. +start + +~~~math +2 +~~~ + +end +. +

start

+

22 +

+

end

+. + +Fence block without newlines +. +start +```math +2 +``` +end +. +

start

+

22 +

+

end

+. + +Fence block on multiple lines +. +```math +f(x) = ax^2 + +bx + c +``` +. +

f(x)=ax2+bx+cf(x) = ax^2 + +bx + c +

+. + +Fence block over multiple lines +. +```math +f(x) = ax^2 + bx + c \\ +y = mx + b +``` +. +

f(x)=ax2+bx+cy=mx+bf(x) = ax^2 + bx + c \\ +y = mx + b +

+. + +Should parse as code block (not math block) +. +start +``` +f(x) = 2*x +``` +end +. +

start

+
f(x) = 2*x
+
+

end

+. + + diff --git a/types.d.ts b/types.d.ts index 542e210..2ea79a2 100644 --- a/types.d.ts +++ b/types.d.ts @@ -16,6 +16,11 @@ export interface MarkdownKatexOptions { */ readonly enableMathInlineInHtml?: boolean; + /** + Enable rendering of fenced math blocks. + */ + readonly enableFencedBlocks?: boolean; + /** * Controls if an exception is thrown on katex errors. */