diff --git a/nbconvert/exporters/html.py b/nbconvert/exporters/html.py index 03d1462df..30c10b7f0 100644 --- a/nbconvert/exporters/html.py +++ b/nbconvert/exporters/html.py @@ -121,6 +121,15 @@ class HTMLExporter(TemplateExporter): """, ).tag(config=True) + mermaid_js_url = Unicode( + "https://cdnjs.cloudflare.com/ajax/libs/mermaid/10.2.3/mermaid.esm.min.mjs", + help=""" + URL to load MermaidJS from. + + Defaults to loading from cdnjs. + """, + ) + jquery_url = Unicode( "https://cdnjs.cloudflare.com/ajax/libs/jquery/2.0.3/jquery.min.js", help=""" @@ -303,6 +312,7 @@ def resources_include_url(name): resources["include_url"] = resources_include_url resources["require_js_url"] = self.require_js_url resources["mathjax_url"] = self.mathjax_url + resources["mermaid_js_url"] = self.mermaid_js_url resources["jquery_url"] = self.jquery_url resources["jupyter_widgets_base_url"] = self.jupyter_widgets_base_url resources["widget_renderer_url"] = self.widget_renderer_url diff --git a/nbconvert/filters/markdown_mistune.py b/nbconvert/filters/markdown_mistune.py index d7501b659..a7a9f05ed 100644 --- a/nbconvert/filters/markdown_mistune.py +++ b/nbconvert/filters/markdown_mistune.py @@ -176,6 +176,10 @@ def block_code(self, code, info=None): """Handle block code.""" lang = "" lexer = None + + if info and info.startswith("mermaid"): + return self.block_mermaidjs(code) + if info: try: lang = info.strip().split(None, 1)[0] @@ -190,6 +194,14 @@ def block_code(self, code, info=None): formatter = HtmlFormatter() return highlight(code, lexer, formatter) + def block_mermaidjs(self, code, info=None): + """Handle mermaid syntax.""" + return ( + """
\n"""
+            f"""{code.strip()}"""
+            """\n
""" + ) + def block_html(self, html): """Handle block html.""" if self.embed_images: diff --git a/nbconvert/filters/tests/test_markdown.py b/nbconvert/filters/tests/test_markdown.py index a66432829..c271969eb 100644 --- a/nbconvert/filters/tests/test_markdown.py +++ b/nbconvert/filters/tests/test_markdown.py @@ -247,6 +247,19 @@ def test_markdown2rst(self): tokens[index], ) + def test_mermaid_markdown(self): + code = """flowchart LR + chicken --> egg --> chicken""" + case = f"""```mermaid\n {code}\n```""" + + output_check = ( + """
\n"""
+            f"""{code.strip()}"""
+            """\n
""" + ) + + self._try_markdown(markdown2html, case, output_check) + def _try_markdown(self, method, test, tokens): results = method(test) if isinstance(tokens, (str,)): diff --git a/share/templates/classic/index.html.j2 b/share/templates/classic/index.html.j2 index ec575b220..87a8578ac 100644 --- a/share/templates/classic/index.html.j2 +++ b/share/templates/classic/index.html.j2 @@ -19,6 +19,12 @@ {%- block html_head_js_requirejs -%} {%- endblock html_head_js_requirejs -%} +{%- block html_head_js_mermaidjs -%} + +{%- endblock html_head_js_mermaidjs -%} {%- endblock html_head_js -%} {% block jupyter_widgets %} diff --git a/share/templates/lab/index.html.j2 b/share/templates/lab/index.html.j2 index 4a6bb8663..8c5665298 100644 --- a/share/templates/lab/index.html.j2 +++ b/share/templates/lab/index.html.j2 @@ -1,5 +1,6 @@ {%- extends 'base.html.j2' -%} {% from 'mathjax.html.j2' import mathjax %} +{% from 'mermaidjs.html.j2' import mermaid_js %} {% from 'jupyter_widgets.html.j2' import jupyter_widgets %} {%- block header -%} @@ -149,6 +150,10 @@ body[data-format='mobile'] .jp-OutputArea-child .jp-OutputArea-output { {{ mathjax(resources.mathjax_url) }} {%- endblock html_head_js_mathjax -%} +{%- block html_head_js_mermaidjs -%} +{{ mermaid_js(resources.mermaid_js_url) }} +{%- endblock html_head_js_mermaidjs -%} + {%- block html_head_css -%} {%- endblock html_head_css -%} diff --git a/share/templates/lab/mermaidjs.html.j2 b/share/templates/lab/mermaidjs.html.j2 new file mode 100644 index 000000000..06c18479e --- /dev/null +++ b/share/templates/lab/mermaidjs.html.j2 @@ -0,0 +1,114 @@ +{%- macro mermaid_js( +url="https://cdnjs.cloudflare.com/ajax/libs/mermaid/10.0.2/mermaid.esm.min.mjs" +) -%} + + + +{%- endmacro %} diff --git a/share/templates/reveal/index.html.j2 b/share/templates/reveal/index.html.j2 index c30af4b4f..84cb65280 100644 --- a/share/templates/reveal/index.html.j2 +++ b/share/templates/reveal/index.html.j2 @@ -30,6 +30,12 @@ {%- block html_head_js_requirejs -%} {%- endblock html_head_js_requirejs -%} +{%- block html_head_js_mermaidjs -%} + +{%- endblock html_head_js_mermaidjs -%} {%- endblock html_head_js -%} {% block jupyter_widgets %}