Background: I want to implement a live preview of documents with MathJax and markdowns. If I remember correctly, currently some people handle the interleaves of MathJax and markdowns in the following manner:
- Detect and remove all fragments for code spans and fenced code blocks.
- Remove all MathJax expressions (delimited by the preferred delimiters) in a greedy manner. (Previous discussions on this site show that the usual delimiters for inline math may cause problems, but that’s fine because MathJax can be configured to use other delimiters that don’t interfere with the CommonMark spec.)
- Inject the code spans and fenced code blocks back to the document. Process all markdowns.
- Process those MathJax fragments and inject them into the result from the previous step.
If the document has no code spans or code blocks, or if we ignore step 1, the current JS implementation of CommonMark is already able to interop with MathJax. But step 1 must take precedence. So, I need to know the locations of the code spans/blocks.