Hooks for extensions

Does the current JavaScript reference implementation of CommonMark have any hooks to interface with plugins or extensions? If not, is there any functions that give me the locations of all code blocks and code spans?

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:

  1. Detect and remove all fragments for code spans and fenced code blocks.
  2. 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.)
  3. Inject the code spans and fenced code blocks back to the document. Process all markdowns.
  4. 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.

The reference implementation does not have hooks for extensions, but another implementation, markdown-it, does. And it looks as if they have a few math-related plugins.

1 Like