Skip to content

Commit e828d19

Browse files
authored
fix: katex should not be used if opts.allowMath is false (#72)
1 parent d554ced commit e828d19

File tree

2 files changed

+26
-3
lines changed

2 files changed

+26
-3
lines changed

mod.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,13 @@ Marked.marked.use(mangle());
1515
Marked.marked.use(gfmHeadingId());
1616

1717
class Renderer extends Marked.Renderer {
18+
allowMath: boolean;
19+
20+
constructor(options: Marked.marked.MarkedOptions & RenderOptions = {}) {
21+
super(options);
22+
this.allowMath = options.allowMath ?? false;
23+
}
24+
1825
heading(
1926
text: string,
2027
level: 1 | 2 | 3 | 4 | 5 | 6,
@@ -36,7 +43,7 @@ class Renderer extends Marked.Renderer {
3643

3744
// transform math code blocks into HTML+MathML
3845
// https://github.blog/changelog/2022-06-28-fenced-block-syntax-for-mathematical-expressions/
39-
if (language === "math") {
46+
if (language === "math" && this.allowMath) {
4047
return katex.renderToString(code, { displayMode: true });
4148
}
4249
const grammar =
@@ -100,12 +107,14 @@ export interface RenderOptions {
100107
export function render(markdown: string, opts: RenderOptions = {}): string {
101108
opts.mediaBaseUrl ??= opts.baseUrl;
102109
markdown = emojify(markdown);
103-
markdown = mathify(markdown);
110+
if (opts.allowMath) {
111+
markdown = mathify(markdown);
112+
}
104113

105114
const marked_opts = {
106115
baseUrl: opts.baseUrl,
107116
gfm: true,
108-
renderer: new Renderer(),
117+
renderer: new Renderer(opts),
109118
};
110119

111120
const html = opts.inline

test/test.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,20 @@ Deno.test("Math rendering doesn't throw on invalid katex input", () => {
3030
render(" $&$");
3131
});
3232

33+
Deno.test("When allowMath is not specified, make sure math is not rendered", () => {
34+
const markdown = "This is a test $$y=x^2$$";
35+
const expected = `<p>This is a test $$y=x^2$$</p>\n`;
36+
const html = render(markdown);
37+
assertEquals(html, expected);
38+
});
39+
40+
Deno.test("When allowMath is not specified, make sure math code block is not rendered", () => {
41+
const markdown = "```math\ny=x^2\n```";
42+
const expected = `<pre><code>y=x^2</code></pre>`;
43+
const html = render(markdown);
44+
assertEquals(html, expected);
45+
});
46+
3347
Deno.test("bug #61 generate a tag", () => {
3448
const markdown = "[link](https://example.com)";
3549
const expected =

0 commit comments

Comments
 (0)