diff options
author | Catalin Irimie <cirimie@gitlab.com> | 2020-10-21 02:38:59 +0300 |
---|---|---|
committer | Catalin Irimie <cirimie@gitlab.com> | 2020-10-21 02:38:59 +0300 |
commit | 823b3a6f35d748cc1a46c9bb71a7422d4a26ff1c (patch) | |
tree | 140ffd43fcead3d9a74984ae8f9c776f0ca8754f /lib | |
parent | 313bca78a52d1a098163ddafbdba80afcf4c98a4 (diff) |
Encode Mermaid HTML to htmlsafe values
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 gets rid of the problem.
Diffstat (limited to 'lib')
-rw-r--r-- | lib/filters/convert_mermaid_html.rb | 21 |
1 files changed, 21 insertions, 0 deletions
diff --git a/lib/filters/convert_mermaid_html.rb b/lib/filters/convert_mermaid_html.rb new file mode 100644 index 00000000..770502b1 --- /dev/null +++ b/lib/filters/convert_mermaid_html.rb @@ -0,0 +1,21 @@ +# frozen_string_literal: true + +class ConvertMermaidHtml < Nanoc::Filter + identifier :convert_mermaid_html + + MERMAID_HTML_PATTERN = %r{<div\ class="mermaid">(?<mermaid_content>.*?)</div>}mx + + def run(content, params = {}) + content.gsub(MERMAID_HTML_PATTERN) { generate(Regexp.last_match[:mermaid_content]) } + end + + def generate(content) + fixed_content = content + .gsub('&', '&') + .gsub('<', '<') + .gsub('>', '>') + .gsub('"', '"') + + %(<div class="mermaid">#{fixed_content}</div>) + end +end |