Skip to content

Commit

Permalink
Merge pull request #27 from KyleKing/fix-25-anchor-links
Browse files Browse the repository at this point in the history
  • Loading branch information
KyleKing committed May 2, 2024
2 parents 9b419b8 + 8a90107 commit 55f5b1b
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 5 deletions.
64 changes: 60 additions & 4 deletions mdformat_mkdocs/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from __future__ import annotations

import argparse
from functools import partial
from functools import lru_cache, partial
from typing import Mapping

from markdown_it import MarkdownIt
Expand Down Expand Up @@ -52,8 +52,8 @@ def add_cli_options(parser: argparse.ArgumentParser) -> None:

def update_mdit(mdit: MarkdownIt) -> None:
"""No changes to markdown parsing are necessary."""
mdit.use(mkdocs_admon_plugin)
mdit.use(content_tabs_plugin)
mdit.use(mkdocs_admon_plugin)

global _ALIGN_SEMANTIC_BREAKS_IN_LISTS # noqa: PLW0603
_ALIGN_SEMANTIC_BREAKS_IN_LISTS = mdit.options["mdformat"].get(
Expand All @@ -74,12 +74,67 @@ def _render_node_content(node: RenderTreeNode, context: RenderContext) -> str:
return node.content


def _render_with_default_renderer(
node: RenderTreeNode,
context: RenderContext,
syntax_type: str,
) -> str:
"""Attempt to render using the mdformat DEFAULT.
Adapted from:
https://github.com/hukkin/mdformat-gfm/blob/bd3c3392830fc4805d51582adcd1ae0d0630aed4/src/mdformat_gfm/plugin.py#L35-L46
"""
text = DEFAULT_RENDERERS.get(syntax_type, _render_node_content)(node, context)
for postprocessor in context.postprocessors.get(syntax_type, ()):
text = postprocessor(text, node, context)
return text


@lru_cache(maxsize=1)
def _match_plugin_renderer(syntax_type: str) -> Render | None:
from mdformat.plugins import PARSER_EXTENSIONS # noqa: PLC0415

for name, plugin in PARSER_EXTENSIONS.items():
# Ignore this plugin (mkdocs) to avoid recursion. Name is set in pyproject.toml
if name != "mkdocs" and plugin.RENDERERS.get(syntax_type):
return plugin.RENDERERS[syntax_type]
return None


def _render_cross_reference(node: RenderTreeNode, context: RenderContext) -> str:
"""Render a MKDocs crossreference link."""
if _IGNORE_MISSING_REFERENCES:
return _render_node_content(node, context)
link = DEFAULT_RENDERERS.get("link", _render_node_content)
return link(node, context)
# Default to treating the matched content as a link
return _render_with_default_renderer(node, context, "link")


def _render_links_and_mkdocs_anchors(
node: RenderTreeNode,
context: RenderContext,
) -> str:
"""Intercepts rendering of [MKDocs AutoRefs 'markdown anchors'](https://mkdocs.github.io/autorefs/#markdown-anchors).
Replaces `[...](<>)` with `[...]()` to produce output like:
```md
[](){#some-anchor-name}
```
If no match, defers to other plugins or the default
"""
syntax_type = node.type

rendered = _render_with_default_renderer(node, context, syntax_type)
if rendered.endswith("](<>)"):
return rendered[:-3] + ")"

# Run other plugin renders if they exist
if plugin_render := _match_plugin_renderer(syntax_type):
return plugin_render(node, context)
return rendered


# A mapping from `RenderTreeNode.type` to a `Render` function that can
Expand All @@ -91,6 +146,7 @@ def _render_cross_reference(node: RenderTreeNode, context: RenderContext) -> str
"content_tab_mkdocs": ADMON_RENDERS["admonition"],
"content_tab_mkdocs_title": ADMON_RENDERS["admonition_title"],
MKDOCSTRINGS_CROSSREFERENCE_PREFIX: _render_cross_reference,
"link": _render_links_and_mkdocs_anchors,
}


Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ readme = "README.md"
requires-python = ">=3.8.0"

[project.entry-points."mdformat.parser_extension"]
mkdocs = "mdformat_mkdocs"
mkdocs = "mdformat_mkdocs" # Do not change name without modifying '_match_plugin_renderer'

[project.optional-dependencies]
dev = ["pre-commit"]
Expand Down
7 changes: 7 additions & 0 deletions tests/format/fixtures/text.md
Original file line number Diff line number Diff line change
Expand Up @@ -1540,3 +1540,10 @@ Unsupported versions of 0-indexed markdown list (Within ordered list)
4\. next
0. next
.

Anchor links (https://github.com/KyleKing/mdformat-mkdocs/issues/25)
.
[](){#some-anchor-name}
.
[](){#some-anchor-name}
.

0 comments on commit 55f5b1b

Please sign in to comment.