From b55a05e3022bebc6032f1090e15336c2d5e35f77 Mon Sep 17 00:00:00 2001 From: Leonardo Uieda Date: Tue, 12 Apr 2022 10:02:02 +0100 Subject: [PATCH] Use markdown-it-py to render Markdown content (#28) Replace the MyST parser, which actually uses markdown-it-py. MyST is meant to be used with sphinx and didn't really offer that much in the way of customizing the Markdown rendering process. With markdown-it-py we can use some of their plugins and even write some of our own to support things like syntax highlighting for code blocks (with language specification, like in GitHub flavored markdown). For now, just replace the rendering and add a few plugins (typographic substitutions, footnotes, tables, and anchors for headings. --- environment.yml | 2 +- nene/rendering.py | 25 +++++++++++++++++++++++-- setup.cfg | 5 +++-- 3 files changed, 27 insertions(+), 5 deletions(-) diff --git a/environment.yml b/environment.yml index a529333..5588083 100644 --- a/environment.yml +++ b/environment.yml @@ -4,7 +4,7 @@ channels: - conda-forge - defaults dependencies: - - python==3.9 + - python==3.10 - pip - make - pip: diff --git a/nene/rendering.py b/nene/rendering.py index b0e3187..48a011d 100644 --- a/nene/rendering.py +++ b/nene/rendering.py @@ -3,7 +3,9 @@ # SPDX-License-Identifier: MIT """Render outputs with Jinja templates.""" import jinja2 -import myst_parser.main +import mdit_py_plugins.anchors +import mdit_py_plugins.footnote +from markdown_it import MarkdownIt def make_jinja_env(templates_dir): @@ -43,10 +45,29 @@ def markdown_to_html(page): The converted HTML. """ - html = myst_parser.main.to_html(page["markdown"]) + parser = MarkdownIt("commonmark", {"typographer": True}) + parser.enable(["replacements", "smartquotes"]) + parser.enable("table") + parser.use(mdit_py_plugins.anchors.anchors_plugin) + parser.use(mdit_py_plugins.footnote.footnote_plugin) + # Remove the starting hr from the footnote block. It's ugly and should be + # handled through CSS by putting a top border on section element. + parser.add_render_rule("footnote_block_open", _render_footnote_block_open) + html = parser.render(page["markdown"]) return html +def _render_footnote_block_open(self, tokens, idx, options, env): + """Render the footnote opening without the hr tag at the start.""" + html = mdit_py_plugins.footnote.index.render_footnote_block_open( + self, tokens, idx, options, env + ) + lines = html.split("\n") + if lines[0].strip().startswith("=3.6 install_requires = - myst-parser>=0.15.0 + markdown-it-py>=2.0 + mdit-py-plugins>=0.3 jinja2>=3.0 pyyaml>=5.4 livereload>=2.6