diff --git a/mdformat_myst/plugin.py b/mdformat_myst/plugin.py index e42ea16..13d796e 100644 --- a/mdformat_myst/plugin.py +++ b/mdformat_myst/plugin.py @@ -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 @@ -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) @@ -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") - # 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 @@ -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) @@ -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} diff --git a/pyproject.toml b/pyproject.toml index 0feb572..81cad4f 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -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]