Skip to content

Commit 0c1781d

Browse files
pawamoyoprypin
andauthored
feat: Preserve HTML data attributes (from spans to anchors)
Issue-#41: #41 PR-#42: #42 Co-authored-by: Oleh Prypin <oleh@pryp.in>
1 parent 24325ff commit 0c1781d

File tree

2 files changed

+17
-6
lines changed

2 files changed

+17
-6
lines changed

src/mkdocs_autorefs/references.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,10 @@
1616
if TYPE_CHECKING:
1717
from markdown import Markdown
1818

19+
_ATTR_VALUE = r'"[^"<>]+"|[^"<> ]+' # Possibly with double quotes around
1920
AUTO_REF_RE = re.compile(
20-
r"<span data-(?P<kind>autorefs-identifier|autorefs-optional|autorefs-optional-hover)="
21-
r'("?)(?P<identifier>[^"<>]*)\2>(?P<title>.*?)</span>',
21+
rf"<span data-(?P<kind>autorefs-(?:identifier|optional|optional-hover))=(?P<identifier>{_ATTR_VALUE})"
22+
rf"(?: class=(?P<class>{_ATTR_VALUE}))?(?P<attrs> [^<>]+)?>(?P<title>.*?)</span>",
2223
flags=re.DOTALL,
2324
)
2425
"""A regular expression to match mkdocs-autorefs' special reference markers
@@ -162,9 +163,11 @@ def fix_ref(url_mapper: Callable[[str], str], unmapped: list[str]) -> Callable:
162163
"""
163164

164165
def inner(match: Match) -> str:
165-
identifier = match["identifier"]
166+
identifier = match["identifier"].strip('"')
166167
title = match["title"]
167168
kind = match["kind"]
169+
attrs = match["attrs"] or ""
170+
classes = (match["class"] or "").strip('"').split()
168171

169172
try:
170173
url = url_mapper(unescape(identifier))
@@ -180,11 +183,11 @@ def inner(match: Match) -> str:
180183

181184
parsed = urlsplit(url)
182185
external = parsed.scheme or parsed.netloc
183-
classes = ["autorefs", "autorefs-external" if external else "autorefs-internal"]
186+
classes = ["autorefs", "autorefs-external" if external else "autorefs-internal", *classes]
184187
class_attr = " ".join(classes)
185188
if kind == "autorefs-optional-hover":
186-
return f'<a class="{class_attr}" title="{identifier}" href="{escape(url)}">{title}</a>'
187-
return f'<a class="{class_attr}" href="{escape(url)}">{title}</a>'
189+
return f'<a class="{class_attr}" title="{identifier}" href="{escape(url)}"{attrs}>{title}</a>'
190+
return f'<a class="{class_attr}" href="{escape(url)}"{attrs}>{title}</a>'
188191

189192
return inner
190193

tests/test_references.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,3 +247,11 @@ def test_external_references() -> None:
247247
output, unmapped = fix_refs(source, url_map.__getitem__)
248248
assert output == '<a class="autorefs autorefs-external" href="https://example.com">example</a>'
249249
assert unmapped == []
250+
251+
252+
def test_keep_data_attributes() -> None:
253+
"""Keep HTML data attributes from autorefs spans."""
254+
url_map = {"example": "https://e.com"}
255+
source = '<span data-autorefs-optional="example" class="hi ho" data-foo data-bar="0">e</span>'
256+
output, _ = fix_refs(source, url_map.__getitem__)
257+
assert output == '<a class="autorefs autorefs-external hi ho" href="https://e.com" data-foo data-bar="0">e</a>'

0 commit comments

Comments
 (0)