Skip to content

Commit

Permalink
feat: add mdformat-wikilink (via copy_paste for ^3.8)
Browse files Browse the repository at this point in the history
  • Loading branch information
KyleKing committed Mar 20, 2024
1 parent fb0935e commit 5f686f1
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 1 deletion.
1 change: 1 addition & 0 deletions mdformat_mkdocs/mdit_plugins/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@

from ._content_tabs import CONTENT_TAB_MARKERS, content_tabs_plugin
from ._mkdocs_admon import MKDOCS_ADMON_MARKERS, mkdocs_admon_plugin
from ._wikilink import wikilink_plugin
32 changes: 32 additions & 0 deletions mdformat_mkdocs/mdit_plugins/_wikilink.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
"""Copied from `mdformat-wikilink`.
https://github.com/tmr232/mdformat-wikilink/blob/d095be227a3cefc18d6fd823d9eb6fdfeaa5c895/src/mdformat_wikilink/mdit_wikilink_plugin.py
"""

import re

from markdown_it import MarkdownIt
from markdown_it.rules_inline import StateInline

LINK_PATTERN = re.compile(r"\[\[([^[|\]\n])+(\|[^]\n]+)?]]")


def _wikilink_inline(state: StateInline, silent: bool) -> bool:
match = LINK_PATTERN.match(state.src[state.pos :])
if not match:
return False

if silent:
return True

token = state.push("wikilink", "", 0)
token.content = match.group()

state.pos += match.end()

return True


def wikilink_plugin(md: MarkdownIt) -> None:
md.inline.ruler.push("wikilink", _wikilink_inline)
9 changes: 8 additions & 1 deletion mdformat_mkdocs/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@
from typing import Mapping

from markdown_it import MarkdownIt
from mdformat.renderer import RenderContext, RenderTreeNode
from mdformat.renderer.typing import Postprocess, Render
from mdformat_admon import RENDERERS as ADMON_RENDERS

from ._normalize_list import normalize_list as unbounded_normalize_list
from ._postprocess_inline import postprocess_inline
from .mdit_plugins import content_tabs_plugin, mkdocs_admon_plugin
from .mdit_plugins import content_tabs_plugin, mkdocs_admon_plugin, wikilink_plugin

_ALIGN_SEMANTIC_BREAKS_IN_LISTS = False
"""user-specified flag for toggling semantic breaks.
Expand All @@ -36,6 +37,7 @@ 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(wikilink_plugin)

global _ALIGN_SEMANTIC_BREAKS_IN_LISTS # noqa: PLW0603
_ALIGN_SEMANTIC_BREAKS_IN_LISTS = mdit.options["mdformat"].get(
Expand All @@ -44,6 +46,10 @@ def update_mdit(mdit: MarkdownIt) -> None:
)


def _render_wikilink(node: RenderTreeNode, context: RenderContext) -> str: # noqa: ARG001
return node.content


# A mapping from `RenderTreeNode.type` to a `Render` function that can
# render the given `RenderTreeNode` type. These override the default
# `Render` funcs defined in `mdformat.renderer.DEFAULT_RENDERERS`.
Expand All @@ -52,6 +58,7 @@ def update_mdit(mdit: MarkdownIt) -> None:
"admonition_mkdocs_title": ADMON_RENDERS["admonition_title"],
"content_tab_mkdocs": ADMON_RENDERS["admonition"],
"content_tab_mkdocs_title": ADMON_RENDERS["admonition_title"],
"wikilink": _render_wikilink,
}


Expand Down

0 comments on commit 5f686f1

Please sign in to comment.