Skip to content

Commit e12a64c

Browse files
authored
👌 IMPROVE: Use mdformat-footnote (#19)
1 parent 3d47f3c commit e12a64c

File tree

2 files changed

+8
-32
lines changed

2 files changed

+8
-32
lines changed

‎mdformat_myst/plugin.py‎

Lines changed: 6 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
from __future__ import annotations
22

33
import re
4-
from textwrap import indent
54

65
from markdown_it import MarkdownIt
76
import mdformat.plugins
87
from mdformat.renderer import RenderContext, RenderTreeNode
98
from mdit_py_plugins.dollarmath import dollarmath_plugin
10-
from mdit_py_plugins.footnote import footnote_plugin
119
from mdit_py_plugins.myst_blocks import myst_block_plugin
1210
from mdit_py_plugins.myst_role import myst_role_plugin
1311

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

31+
# Enable mdformat-footnote plugin
32+
footnote_plugin = mdformat.plugins.PARSER_EXTENSIONS["footnote"]
33+
if footnote_plugin not in mdit.options["parser_extension"]:
34+
mdit.options["parser_extension"].append(footnote_plugin)
35+
footnote_plugin.update_mdit(mdit)
36+
3337
# Enable MyST role markdown-it extension
3438
mdit.use(myst_role_plugin)
3539

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

43-
# Enable footnote markdown-it extension
44-
mdit.use(footnote_plugin)
45-
# MyST has inline footnotes disabled
46-
mdit.disable("footnote_inline")
47-
4847
# Trick `mdformat`s AST validation by removing HTML rendering of code
4948
# blocks and fences. Directives are parsed as code fences and we
5049
# modify them in ways that don't break MyST AST but do break
@@ -86,27 +85,6 @@ def _math_block_label_renderer(node: RenderTreeNode, context: RenderContext) ->
8685
return f"$${node.content}$$ ({node.info})"
8786

8887

89-
def _footnote_ref_renderer(node: RenderTreeNode, context: RenderContext) -> str:
90-
return f"[^{node.meta['label']}]"
91-
92-
93-
def _footnote_renderer(node: RenderTreeNode, context: RenderContext) -> str:
94-
first_line = f"[^{node.meta['label']}]:"
95-
elements = []
96-
for child in node.children:
97-
if child.type == "footnote_anchor":
98-
continue
99-
elements.append(child.render(context))
100-
body = indent("\n\n".join(elements), " " * 4)
101-
# if the first body element is a paragraph, we can start on the first line,
102-
# otherwise we start on the second line
103-
if body and node.children and node.children[0].type != "paragraph":
104-
body = "\n" + body
105-
else:
106-
body = " " + body.lstrip()
107-
return first_line + body
108-
109-
11088
def _render_children(node: RenderTreeNode, context: RenderContext) -> str:
11189
return "\n\n".join(child.render(context) for child in node.children)
11290

@@ -150,9 +128,6 @@ def _escape_text(text: str, node: RenderTreeNode, context: RenderContext) -> str
150128
"math_inline": _math_inline_renderer,
151129
"math_block_label": _math_block_label_renderer,
152130
"math_block": _math_block_renderer,
153-
"footnote": _footnote_renderer,
154-
"footnote_ref": _footnote_ref_renderer,
155-
"footnote_block": _render_children,
156131
"fence": fence,
157132
}
158133
POSTPROCESSORS = {"paragraph": _escape_paragraph, "text": _escape_text}

‎pyproject.toml‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ requires=[
2525
"mdit-py-plugins >=0.3.0,<0.4.0",
2626
"mdformat-tables >=0.4.0",
2727
"mdformat-frontmatter >=0.3.2",
28-
"ruamel.yaml >=0.16.0"
28+
"mdformat-footnote >=0.1.1",
29+
"ruamel.yaml >=0.16.0",
2930
]
3031

3132
[tool.flit.metadata.requires-extra]

0 commit comments

Comments
 (0)