Skip to content

Commit

Permalink
fix: disable katex when allowMath is false in code block
Browse files Browse the repository at this point in the history
  • Loading branch information
hashrock committed Jul 4, 2023
1 parent ef9b3d5 commit e2ef087
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
11 changes: 9 additions & 2 deletions mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@ Marked.marked.use(mangle());
Marked.marked.use(gfmHeadingId());

class Renderer extends Marked.Renderer {
allowMath: boolean;

constructor(options: Marked.marked.MarkedOptions & RenderOptions = {}) {
super(options);
this.allowMath = options.allowMath ?? false;
}

heading(
text: string,
level: 1 | 2 | 3 | 4 | 5 | 6,
Expand All @@ -36,7 +43,7 @@ class Renderer extends Marked.Renderer {

// transform math code blocks into HTML+MathML
// https://github.blog/changelog/2022-06-28-fenced-block-syntax-for-mathematical-expressions/
if (language === "math") {
if (language === "math" && this.allowMath) {
return katex.renderToString(code, { displayMode: true });
}
const grammar =
Expand Down Expand Up @@ -107,7 +114,7 @@ export function render(markdown: string, opts: RenderOptions = {}): string {
const marked_opts = {
baseUrl: opts.baseUrl,
gfm: true,
renderer: new Renderer(),
renderer: new Renderer(opts),
};

const html = opts.inline
Expand Down
14 changes: 14 additions & 0 deletions test/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,20 @@ Deno.test("Math rendering doesn't throw on invalid katex input", () => {
render(" $&$");
});

Deno.test("When allowMath is not specified, make sure math is not rendered", () => {
const markdown = "This is a test $$y=x^2$$";
const expected = `<p>This is a test $$y=x^2$$</p>\n`;
const html = render(markdown);
assertEquals(html, expected);
});

Deno.test("When allowMath is not specified, make sure math code block is not rendered", () => {
const markdown = "```math\ny=x^2\n```";
const expected = `<pre><code>y=x^2</code></pre>`;
const html = render(markdown);
assertEquals(html, expected);
});

Deno.test("bug #61 generate a tag", () => {
const markdown = "[link](https://example.com)";
const expected =
Expand Down

0 comments on commit e2ef087

Please sign in to comment.