@@ -22,11 +22,18 @@ export default function markdownitrules(mdit, options) {
2222 mdit . renderer . rules . code_inline = function ( tokens , idx ) {
2323 const code = tokens [ idx ] . content ;
2424 const inlineWrap = ( s ) => `\\(${ s } \\)` ;
25+ if ( isLaTeX ( code ) ) {
26+ return inlineWrap ( code ) ;
27+ }
2528 return inlineWrap ( window . AMparseMath ( code , true ) ) ;
2629 } ;
2730
2831 mdit . renderer . rules . asciimath_block = function ( tokens , idx ) {
2932 const code = tokens [ idx ] . content ;
33+ const blockWrap = ( s ) => `\\[${ s } \\]` ;
34+ if ( isLaTeX ( code ) ) {
35+ return blockWrap ( code ) ;
36+ }
3037 return applyFilters ( code , true ) ;
3138 } ;
3239
@@ -41,6 +48,24 @@ export default function markdownitrules(mdit, options) {
4148 return applyFilters ( code , false ) ;
4249 } ;
4350
51+ function isLaTeX ( code ) {
52+ // Do not attempt to apply ASCIIMath to blocks which are already LaTeX.
53+ const islatex = [
54+ '^{' ,
55+ '_{' ,
56+ '\\left' ,
57+ '\\right' ,
58+ '\\begin' ,
59+ ] ;
60+ if ( islatex . some ( s => code . includes ( s ) ) ) {
61+ return true ;
62+ } ;
63+ // Use of a general control code.
64+ // (Yes, this duplicates \left, \right, \begin above...)
65+ const regex = / \\ [ a - z A - Z ] + / ;
66+ return regex . test ( code ) ;
67+ } ;
68+
4469 function splitBlock ( code ) {
4570 return code . split ( / \r ? \n / ) . map ( line => line . trim ( ) ) . filter ( line => line !== '' ) ;
4671 }
0 commit comments