22/*
33Like markdown-it-simplemath, this is a stripped down, simplified version of:
44https://github.com/runarberg/markdown-it-math
5+
56It differs in that it takes (a subset of) LaTeX as input and relies on KaTeX
67for rendering output.
78*/
@@ -133,11 +134,22 @@ function math_block(state, start, end, silent){
133134 return true ;
134135}
135136
136- module . exports = function math_plugin ( md , options ) {
137+ export default function math_plugin ( md , options ) {
137138 // Default options
138139
139140 options = options || { } ;
140141
142+ var escapeHtml = function ( html ) {
143+ var tagsToReplace = {
144+ '&' : '&' ,
145+ '<' : '<' ,
146+ '>' : '>'
147+ } ;
148+ return html . replace ( / [ & < > ] / g, function ( tag ) {
149+ return tagsToReplace [ tag ] || tag ;
150+ } ) ;
151+ } ;
152+
141153 // set KaTeX as the renderer for markdown-it-simplemath
142154 var katexInline = function ( latex ) {
143155 options . displayMode = false ;
@@ -146,12 +158,13 @@ module.exports = function math_plugin(md, options) {
146158 }
147159 catch ( error ) {
148160 if ( options . throwOnError ) { console . log ( error ) ; }
149- return latex ;
161+ return escapeHtml ( latex ) ;
150162 }
151163 } ;
152164
153165 var inlineRenderer = function ( tokens , idx , options , env , { sDom } ) {
154- const html = katexInline ( tokens [ idx ] . content ) ;
166+ var html = katexInline ( tokens [ idx ] . content )
167+
155168 sDom . openTag ( 'span' , { __html : html } )
156169 sDom . closeTag ( )
157170 return sDom
@@ -164,12 +177,12 @@ module.exports = function math_plugin(md, options) {
164177 }
165178 catch ( error ) {
166179 if ( options . throwOnError ) { console . log ( error ) ; }
167- return latex ;
180+ return escapeHtml ( latex ) ;
168181 }
169182 }
170183
171184 var blockRenderer = function ( tokens , idx , options , env , { sDom } ) {
172- const html = katexBlock ( tokens [ idx ] . content ) ;
185+ var html = katexBlock ( tokens [ idx ] . content ) ;
173186 sDom . openTag ( 'p' , { __html : html } )
174187 sDom . closeTag ( )
175188 sDom . appendText ( '\n' )
0 commit comments