Skip to content

Commit

Permalink
Use markdown-it-py to render Markdown content (#28)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
leouieda committed Apr 12, 2022
1 parent cb2aff0 commit b55a05e
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 5 deletions.
2 changes: 1 addition & 1 deletion environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ channels:
- conda-forge
- defaults
dependencies:
- python==3.9
- python==3.10
- pip
- make
- pip:
Expand Down
25 changes: 23 additions & 2 deletions nene/rendering.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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("<hr"):
lines = lines[1:]
return "\n".join(lines)


def render_markdown(page, config, site, build, jinja_env):
"""
Render the templates in Markdown content of the page.
Expand Down
5 changes: 3 additions & 2 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ license_file = LICENSE.txt
platform = any
keywords = html, markdown
classifiers =
Development Status :: 3 - Alpha
Development Status :: 4 - Beta
Intended Audience :: Developers
License :: OSI Approved :: MIT License
Natural Language :: English
Expand All @@ -36,7 +36,8 @@ zip_safe = True
packages = find:
python_requires = >=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
Expand Down

0 comments on commit b55a05e

Please sign in to comment.