diff --git a/src/mkdocs_autorefs/references.py b/src/mkdocs_autorefs/references.py
index a4d2ab6..7d2dbae 100644
--- a/src/mkdocs_autorefs/references.py
+++ b/src/mkdocs_autorefs/references.py
@@ -16,9 +16,10 @@
if TYPE_CHECKING:
from markdown import Markdown
+_ATTR_VALUE = r'"[^"<>]+"|[^"<> ]+' # Possibly with double quotes around
AUTO_REF_RE = re.compile(
- r"autorefs-identifier|autorefs-optional|autorefs-optional-hover)="
- r'("?)(?P[^"<>]*)\2>(?P.*?)',
+ rf"autorefs-(?:identifier|optional|optional-hover))=(?P{_ATTR_VALUE})"
+ rf"(?: class=(?P{_ATTR_VALUE}))?(?P [^<>]+)?>(?P.*?)",
flags=re.DOTALL,
)
"""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:
"""
def inner(match: Match) -> str:
- identifier = match["identifier"]
+ identifier = match["identifier"].strip('"')
title = match["title"]
kind = match["kind"]
+ attrs = match["attrs"] or ""
+ classes = (match["class"] or "").strip('"').split()
try:
url = url_mapper(unescape(identifier))
@@ -180,11 +183,11 @@ def inner(match: Match) -> str:
parsed = urlsplit(url)
external = parsed.scheme or parsed.netloc
- classes = ["autorefs", "autorefs-external" if external else "autorefs-internal"]
+ classes = ["autorefs", "autorefs-external" if external else "autorefs-internal", *classes]
class_attr = " ".join(classes)
if kind == "autorefs-optional-hover":
- return f'{title}'
- return f'{title}'
+ return f'{title}'
+ return f'{title}'
return inner
diff --git a/tests/test_references.py b/tests/test_references.py
index 02b3f50..5047754 100644
--- a/tests/test_references.py
+++ b/tests/test_references.py
@@ -247,3 +247,11 @@ def test_external_references() -> None:
output, unmapped = fix_refs(source, url_map.__getitem__)
assert output == 'example'
assert unmapped == []
+
+
+def test_keep_data_attributes() -> None:
+ """Keep HTML data attributes from autorefs spans."""
+ url_map = {"example": "https://e.com"}
+ source = 'e'
+ output, _ = fix_refs(source, url_map.__getitem__)
+ assert output == 'e'