@@ -13,35 +13,48 @@ import juice from 'juice';
1313import { SafeAny } from './utils' ;
1414import { MathJaxOutputType } from './plugin-settings' ;
1515
16+ const inlineTokenType = 'math_inline' ;
17+ const blockTokenType = 'math_block' ;
1618
1719interface MarkdownItMathJax3PluginOptions {
1820 outputType : MathJaxOutputType ;
1921}
2022
23+ const pluginOptions : MarkdownItMathJax3PluginOptions = {
24+ outputType : MathJaxOutputType . TeX ,
25+ }
26+
2127interface ConvertOptions {
2228 display : boolean
2329}
2430
25- export default function MarkdownItMathJax3Plugin ( md : MarkdownIt , options : MarkdownItMathJax3PluginOptions ) : void {
31+ export const MarkdownItMathJax3PluginInstance = {
32+ plugin : plugin ,
33+ updateOutputType : ( type : MathJaxOutputType ) => {
34+ pluginOptions . outputType = type ;
35+ } ,
36+ }
37+
38+ function plugin ( md : MarkdownIt ) : void {
2639 // set MathJax as the renderer for markdown-it-simplemath
27- md . inline . ruler . after ( 'escape' , 'math_inline' , mathInline ) ;
28- md . block . ruler . after ( 'blockquote' , 'math_block' , mathBlock , {
40+ md . inline . ruler . after ( 'escape' , inlineTokenType , mathInline ) ;
41+ md . block . ruler . after ( 'blockquote' , blockTokenType , mathBlock , {
2942 alt : [ 'paragraph' , 'reference' , 'blockquote' , 'list' ] ,
3043 } ) ;
31- md . renderer . rules . math_inline = ( tokens : Token [ ] , idx : number ) => {
44+ md . renderer . rules [ inlineTokenType ] = ( tokens : Token [ ] , idx : number ) => {
3245 return renderMath ( tokens [ idx ] . content , {
3346 display : false
34- } , options ) ;
47+ } ) ;
3548 } ;
36- md . renderer . rules . math_block = ( tokens : Token [ ] , idx : number ) => {
49+ md . renderer . rules [ blockTokenType ] = ( tokens : Token [ ] , idx : number ) => {
3750 return renderMath ( tokens [ idx ] . content , {
3851 display : true
39- } , options ) ;
52+ } ) ;
4053 } ;
4154}
4255
43- function renderMath ( content : string , convertOptions : ConvertOptions , options : MarkdownItMathJax3PluginOptions ) : string {
44- if ( options . outputType === MathJaxOutputType . SVG ) {
56+ function renderMath ( content : string , convertOptions : ConvertOptions ) : string {
57+ if ( pluginOptions . outputType === MathJaxOutputType . SVG ) {
4558 const documentOptions = {
4659 InputJax : new TeX ( { packages : AllPackages } ) ,
4760 OutputJax : new SVG ( { fontCache : 'none' } )
@@ -154,7 +167,7 @@ function mathInline(state: StateInline, silent: boolean) {
154167 }
155168
156169 if ( ! silent ) {
157- const token = state . push ( 'math_inline' , 'math' , 0 ) ;
170+ const token = state . push ( inlineTokenType , 'math' , 0 ) ;
158171 token . markup = '$' ;
159172 token . content = state . src . slice ( start , match ) ;
160173 }
@@ -214,7 +227,7 @@ function mathBlock(state: StateBlock, start: number, end: number, silent: boolea
214227
215228 state . line = next + 1 ;
216229
217- const token = state . push ( 'math_block' , 'math' , 0 ) ;
230+ const token = state . push ( blockTokenType , 'math' , 0 ) ;
218231 token . block = true ;
219232 token . content =
220233 ( firstLine && firstLine . trim ( ) ? firstLine + '\n' : '' ) +
0 commit comments