Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 6 additions & 31 deletions mdformat_myst/plugin.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
from __future__ import annotations

import re
from textwrap import indent

from markdown_it import MarkdownIt
import mdformat.plugins
from mdformat.renderer import RenderContext, RenderTreeNode
from mdit_py_plugins.dollarmath import dollarmath_plugin
from mdit_py_plugins.footnote import footnote_plugin
from mdit_py_plugins.myst_blocks import myst_block_plugin
from mdit_py_plugins.myst_role import myst_role_plugin

Expand All @@ -30,6 +28,12 @@ def update_mdit(mdit: MarkdownIt) -> None:
mdit.options["parser_extension"].append(frontmatter_plugin)
frontmatter_plugin.update_mdit(mdit)

# Enable mdformat-footnote plugin
footnote_plugin = mdformat.plugins.PARSER_EXTENSIONS["footnote"]
if footnote_plugin not in mdit.options["parser_extension"]:
mdit.options["parser_extension"].append(footnote_plugin)
footnote_plugin.update_mdit(mdit)

# Enable MyST role markdown-it extension
mdit.use(myst_role_plugin)

Expand All @@ -40,11 +44,6 @@ def update_mdit(mdit: MarkdownIt) -> None:
# Enable dollarmath markdown-it extension
mdit.use(dollarmath_plugin)

# Enable footnote markdown-it extension
mdit.use(footnote_plugin)
# MyST has inline footnotes disabled
mdit.disable("footnote_inline")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Out of interest, does mdformat-footnote have footnote_inline enabled?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


# Trick `mdformat`s AST validation by removing HTML rendering of code
# blocks and fences. Directives are parsed as code fences and we
# modify them in ways that don't break MyST AST but do break
Expand Down Expand Up @@ -86,27 +85,6 @@ def _math_block_label_renderer(node: RenderTreeNode, context: RenderContext) ->
return f"$${node.content}$$ ({node.info})"


def _footnote_ref_renderer(node: RenderTreeNode, context: RenderContext) -> str:
return f"[^{node.meta['label']}]"


def _footnote_renderer(node: RenderTreeNode, context: RenderContext) -> str:
first_line = f"[^{node.meta['label']}]:"
elements = []
for child in node.children:
if child.type == "footnote_anchor":
continue
elements.append(child.render(context))
body = indent("\n\n".join(elements), " " * 4)
# if the first body element is a paragraph, we can start on the first line,
# otherwise we start on the second line
if body and node.children and node.children[0].type != "paragraph":
body = "\n" + body
else:
body = " " + body.lstrip()
return first_line + body


def _render_children(node: RenderTreeNode, context: RenderContext) -> str:
return "\n\n".join(child.render(context) for child in node.children)

Expand Down Expand Up @@ -150,9 +128,6 @@ def _escape_text(text: str, node: RenderTreeNode, context: RenderContext) -> str
"math_inline": _math_inline_renderer,
"math_block_label": _math_block_label_renderer,
"math_block": _math_block_renderer,
"footnote": _footnote_renderer,
"footnote_ref": _footnote_ref_renderer,
"footnote_block": _render_children,
"fence": fence,
}
POSTPROCESSORS = {"paragraph": _escape_paragraph, "text": _escape_text}
3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ requires=[
"mdit-py-plugins >=0.3.0,<0.4.0",
"mdformat-tables >=0.4.0",
"mdformat-frontmatter >=0.3.2",
"ruamel.yaml >=0.16.0"
"mdformat-footnote >=0.1.1",
"ruamel.yaml >=0.16.0",
]

[tool.flit.metadata.requires-extra]
Expand Down