Skip to content

Encode Mermaid HTML to htmlsafe values

Catalin Irimie requested to merge cat-preconvert-mermaid-html into master

There's an inconsistency with some filters, for example admonition and introduced_in, that call Nokogiri::HTML#fragment on the input and write the output, which means code that looks like tags gets treated as actual HTML tags.

This breaks mermaid diagrams because those tags are not valid and the Mermaid JS parser trips over them, converting them to the html-encoded values is what was currently happening for ->> strings and it's working, so pre-converting all of them should get rid of the problem.

Internal Slack thread with the original discussion.

Example:

irb(main):079:0> Nokogiri::HTML.fragment("Gitaly->>Workhorse <streamed data>").inner_html
=> "Gitaly-&gt;&gt;Workhorse <streamed data></streamed>"

If we pre-convert those, they remain unchanged:

irb(main):106:0> Nokogiri::HTML.fragment("Gitaly-&gt;&gt;Workhorse &lt;streamed data&gt;").inner_html
=> "Gitaly-&gt;&gt;Workhorse &lt;streamed data&gt;"

Also timings, locally, before/after the new regexp from scratch, compiling all files:

# before
Site compiled in 70.79s.
# after
Site compiled in 71.28s.

Merge request reports