js syntax highlighting for markdown (not for codeblocks in markdown)

John MacFarlane jgm at berkeley.edu
Fri Apr 24 12:06:40 EDT 2015


+++ Tobias Bengfort [Apr 24 15 11:14 ]:
>I am not sure if source mapping is the best approach. It sounds strange
>to me that the highlighter would need the AST *and* the original input.
>
>Wouldn't it be easier to include enough information in the AST to be
>able to reconstruct the original input (e.g. whitespace)? It should then
>be easy to add a second renderer for highlighting.

In principle one might be able to include enough information in the
AST to reconstruct the exact source input, but this would make the
AST immensely more complex.  For example, for a block quote we'd have
to store the exact pattern of leading `>`s on every line, including any
initial indentation.  For any character that could be escaped we'd
need to store whether it was escaped in the source.  I could go on and
on.  I think that source mapping is a much simpler approach.  It also
has applications beyond highlighting (e.g., for "click the formatted
text and an edit box appears" style online editing).

One thing to note is that the source text for a particular element
can be discontinuous.  Take, for example:

    > This is *emphasized
    > text*.

Here the emphasized node corresponds to columns 10-20 in line 1
and columns 2-6 in line 2.  That's why my original approach of storing
start and end positions for each node is not enough.  So, the revisions
needed to commonmark.js are bigger than I thought, but still feasible.



More information about the Markdown-Discuss mailing list