Skip to content

Commit

Permalink
fix: katex should not be used if opts.allowMath is false (#72)
Browse files Browse the repository at this point in the history
  • Loading branch information
hashrock committed Jul 5, 2023
1 parent d554ced commit e828d19
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
15 changes: 12 additions & 3 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 @@ -100,12 +107,14 @@ export interface RenderOptions {
export function render(markdown: string, opts: RenderOptions = {}): string {
opts.mediaBaseUrl ??= opts.baseUrl;
markdown = emojify(markdown);
markdown = mathify(markdown);
if (opts.allowMath) {
markdown = mathify(markdown);
}

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 e828d19

Please sign in to comment.