11from __future__ import annotations
22
33import re
4- from textwrap import indent
54
65from markdown_it import MarkdownIt
76import mdformat .plugins
87from mdformat .renderer import RenderContext , RenderTreeNode
98from mdit_py_plugins .dollarmath import dollarmath_plugin
10- from mdit_py_plugins .footnote import footnote_plugin
119from mdit_py_plugins .myst_blocks import myst_block_plugin
1210from 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-
11088def _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}
158133POSTPROCESSORS = {"paragraph" : _escape_paragraph , "text" : _escape_text }
0 commit comments