Skip to content

Commit b55a05e

Browse files
authored
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.
1 parent cb2aff0 commit b55a05e

File tree

3 files changed

+27
-5
lines changed

3 files changed

+27
-5
lines changed

environment.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ channels:
44
- conda-forge
55
- defaults
66
dependencies:
7-
- python==3.9
7+
- python==3.10
88
- pip
99
- make
1010
- pip:

nene/rendering.py

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
# SPDX-License-Identifier: MIT
44
"""Render outputs with Jinja templates."""
55
import jinja2
6-
import myst_parser.main
6+
import mdit_py_plugins.anchors
7+
import mdit_py_plugins.footnote
8+
from markdown_it import MarkdownIt
79

810

911
def make_jinja_env(templates_dir):
@@ -43,10 +45,29 @@ def markdown_to_html(page):
4345
The converted HTML.
4446
4547
"""
46-
html = myst_parser.main.to_html(page["markdown"])
48+
parser = MarkdownIt("commonmark", {"typographer": True})
49+
parser.enable(["replacements", "smartquotes"])
50+
parser.enable("table")
51+
parser.use(mdit_py_plugins.anchors.anchors_plugin)
52+
parser.use(mdit_py_plugins.footnote.footnote_plugin)
53+
# Remove the starting hr from the footnote block. It's ugly and should be
54+
# handled through CSS by putting a top border on section element.
55+
parser.add_render_rule("footnote_block_open", _render_footnote_block_open)
56+
html = parser.render(page["markdown"])
4757
return html
4858

4959

60+
def _render_footnote_block_open(self, tokens, idx, options, env):
61+
"""Render the footnote opening without the hr tag at the start."""
62+
html = mdit_py_plugins.footnote.index.render_footnote_block_open(
63+
self, tokens, idx, options, env
64+
)
65+
lines = html.split("\n")
66+
if lines[0].strip().startswith("<hr"):
67+
lines = lines[1:]
68+
return "\n".join(lines)
69+
70+
5071
def render_markdown(page, config, site, build, jinja_env):
5172
"""
5273
Render the templates in Markdown content of the page.

setup.cfg

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ license_file = LICENSE.txt
1111
platform = any
1212
keywords = html, markdown
1313
classifiers =
14-
Development Status :: 3 - Alpha
14+
Development Status :: 4 - Beta
1515
Intended Audience :: Developers
1616
License :: OSI Approved :: MIT License
1717
Natural Language :: English
@@ -36,7 +36,8 @@ zip_safe = True
3636
packages = find:
3737
python_requires = >=3.6
3838
install_requires =
39-
myst-parser>=0.15.0
39+
markdown-it-py>=2.0
40+
mdit-py-plugins>=0.3
4041
jinja2>=3.0
4142
pyyaml>=5.4
4243
livereload>=2.6

0 commit comments

Comments
 (0)