Skip to content

Commit 7619c28

Browse files
authored
feat: Allow external tools to insert references that are OK to skip
E.g. mkdocstrings-crystal can proactively insert links (by writing out the exact HTML that's prescribed here) that in the end can turn out to not be present anywhere, and since those are auto-inserted, they are not actionable for the user, so there should be no warning for them. So mkdocstrings-crystal will switch to `<span data-autorefs-optional="...">`. Also migrate the "required" case to new data- keys, because this is not specific to mkdocstrings now. PR #7: #7
1 parent 2d3182d commit 7619c28

3 files changed

Lines changed: 26 additions & 3 deletions

File tree

src/mkdocs_autorefs/plugin.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ def on_post_page(self, output: str, page: Page, **kwargs) -> str: # noqa: W0613
148148
149149
Hook for the [`on_post_page` event](https://www.mkdocs.org/user-guide/plugins/#on_post_page).
150150
In this hook, we try to fix unresolved references of the form `[title][identifier]` or `[identifier][]`.
151-
Doing that allows the user of `mkdocstrings` to cross-reference objects in their documentation strings.
151+
Doing that allows the user of `autorefs` to cross-reference objects in their documentation strings.
152152
It uses the native Markdown syntax so it's easy to remember and use.
153153
154154
We log a warning for each reference that we couldn't map to an URL, but try to be smart and ignore identifiers

src/mkdocs_autorefs/references.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@
1010
from markdown.inlinepatterns import REFERENCE_RE, ReferenceInlineProcessor
1111
from markdown.util import INLINE_PLACEHOLDER_RE
1212

13-
AUTO_REF_RE = re.compile(r'<span data-mkdocstrings-identifier=("?)(?P<identifier>[^"<>]*)\1>(?P<title>.*?)</span>')
13+
AUTO_REF_RE = re.compile(
14+
r"<span data-(?P<kind>autorefs-identifier|autorefs-optional|mkdocstrings-identifier)="
15+
r'("?)(?P<identifier>[^"<>]*)\2>(?P<title>.*?)</span>'
16+
)
1417
"""A regular expression to match mkdocs-autorefs' special reference markers
1518
in the [`on_post_page` hook][mkdocs_autorefs.plugin.AutorefsPlugin.on_post_page].
1619
"""
@@ -94,7 +97,7 @@ def makeTag(self, identifier: str, text: str) -> Element: # noqa: N802,W0221 (p
9497
A new element.
9598
"""
9699
el = Element("span")
97-
el.set("data-mkdocstrings-identifier", identifier)
100+
el.set("data-autorefs-identifier", identifier)
98101
el.text = text
99102
return el
100103

@@ -152,6 +155,8 @@ def inner(match: Match):
152155
try:
153156
url = relative_url(from_url, url_mapper(unescape(identifier)))
154157
except KeyError:
158+
if match["kind"] == "autorefs-optional":
159+
return title
155160
unmapped.append(identifier)
156161
if title == identifier:
157162
return f"[{identifier}][]"

tests/test_references.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,3 +154,21 @@ def test_ignore_reference_with_special_char():
154154
source="This [*a b*][].",
155155
output="<p>This [<em>a b</em>][].</p>",
156156
)
157+
158+
159+
def test_custom_required_reference():
160+
"""Check that external HTML-based references are expanded or reported missing."""
161+
url_map = {"ok": "ok.html#ok"}
162+
source = "<span data-autorefs-identifier=bar>foo</span> <span data-autorefs-identifier=ok>ok</span>"
163+
output, unmapped = fix_refs(source, "page.html", url_map.__getitem__)
164+
assert output == '[foo][bar] <a href="ok.html#ok">ok</a>'
165+
assert unmapped == ["bar"]
166+
167+
168+
def test_custom_optional_reference():
169+
"""Check that optional HTML-based references are expanded and never reported missing."""
170+
url_map = {"ok": "ok.html#ok"}
171+
source = '<span data-autorefs-optional="bar">foo</span> <span data-autorefs-optional=ok>ok</span>'
172+
output, unmapped = fix_refs(source, "page.html", url_map.__getitem__)
173+
assert output == 'foo <a href="ok.html#ok">ok</a>'
174+
assert unmapped == []

0 commit comments

Comments
 (0)