Skip to content

Commit 0288bdd

Browse files
authored
feat: Add optional-hover ref type
This additionally adds the full identifier as hover text, which is useful for certain references that could be generated by mkdocstrings. PR #10: #10
1 parent 79ccc37 commit 0288bdd

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

src/mkdocs_autorefs/references.py

+7-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from markdown.util import INLINE_PLACEHOLDER_RE
1212

1313
AUTO_REF_RE = re.compile(
14-
r"<span data-(?P<kind>autorefs-identifier|autorefs-optional|mkdocstrings-identifier)="
14+
r"<span data-(?P<kind>autorefs-identifier|autorefs-optional|autorefs-optional-hover)="
1515
r'("?)(?P<identifier>[^"<>]*)\2>(?P<title>.*?)</span>'
1616
)
1717
"""A regular expression to match mkdocs-autorefs' special reference markers
@@ -150,17 +150,22 @@ def fix_ref(url_mapper: Callable[[str], str], unmapped: List[str]) -> Callable:
150150
def inner(match: Match):
151151
identifier = match["identifier"]
152152
title = match["title"]
153+
kind = match["kind"]
153154

154155
try:
155156
url = url_mapper(unescape(identifier))
156157
except KeyError:
157-
if match["kind"] == "autorefs-optional":
158+
if kind == "autorefs-optional":
158159
return title
160+
elif kind == "autorefs-optional-hover":
161+
return f'<span title="{identifier}">{title}</span>'
159162
unmapped.append(identifier)
160163
if title == identifier:
161164
return f"[{identifier}][]"
162165
return f"[{title}][{identifier}]"
163166

167+
if kind == "autorefs-optional-hover":
168+
return f'<a title="{identifier}" href="{escape(url)}">{title}</a>'
164169
return f'<a href="{escape(url)}">{title}</a>'
165170

166171
return inner

tests/test_references.py

+9
Original file line numberDiff line numberDiff line change
@@ -185,3 +185,12 @@ def test_custom_optional_reference():
185185
output, unmapped = fix_refs(source, url_map.__getitem__)
186186
assert output == 'foo <a href="ok.html#ok">ok</a>'
187187
assert unmapped == []
188+
189+
190+
def test_custom_optional_hover_reference():
191+
"""Check that optional-hover HTML-based references are expanded and never reported missing."""
192+
url_map = {"ok": "ok.html#ok"}
193+
source = '<span data-autorefs-optional-hover="bar">foo</span> <span data-autorefs-optional-hover=ok>ok</span>'
194+
output, unmapped = fix_refs(source, url_map.__getitem__)
195+
assert output == '<span title="bar">foo</span> <a title="ok" href="ok.html#ok">ok</a>'
196+
assert unmapped == []

0 commit comments

Comments
 (0)