File tree Expand file tree Collapse file tree 2 files changed +26
-3
lines changed Expand file tree Collapse file tree 2 files changed +26
-3
lines changed Original file line number Diff line number Diff line change @@ -15,6 +15,13 @@ Marked.marked.use(mangle());
1515Marked . marked . use ( gfmHeadingId ( ) ) ;
1616
1717class 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 {
100107export 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
Original file line number Diff line number Diff 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+
3347Deno . test ( "bug #61 generate a tag" , ( ) => {
3448 const markdown = "[link](https://example.com)" ;
3549 const expected =
You can’t perform that action at this time.
0 commit comments