File tree 2 files changed +13
-4
lines changed 2 files changed +13
-4
lines changed Original file line number Diff line number Diff line change 16
16
from markdown import Markdown
17
17
18
18
AUTO_REF_RE = re .compile (
19
- r"<span data-(?P<kind>autorefs-identifier|autorefs- optional|autorefs- optional-hover)="
20
- r'("?)(?P<identifier>[^"<>]* )\2>(?P<title>.*?)</span>' ,
19
+ r"<span data-(?P<kind>autorefs-(?: identifier|optional|optional-hover) )="
20
+ r'("?)(?P<identifier>[^"<>]+ )\2(?P<attrs> [^<>]+)? >(?P<title>.*?)</span>' ,
21
21
flags = re .DOTALL ,
22
22
)
23
23
"""A regular expression to match mkdocs-autorefs' special reference markers
@@ -157,6 +157,7 @@ def inner(match: Match) -> str:
157
157
identifier = match ["identifier" ]
158
158
title = match ["title" ]
159
159
kind = match ["kind" ]
160
+ attrs = match ["attrs" ] or ""
160
161
161
162
try :
162
163
url = url_mapper (unescape (identifier ))
@@ -175,8 +176,8 @@ def inner(match: Match) -> str:
175
176
classes = ["autorefs" , "autorefs-external" if external else "autorefs-internal" ]
176
177
class_attr = " " .join (classes )
177
178
if kind == "autorefs-optional-hover" :
178
- return f'<a class="{ class_attr } " title="{ identifier } " href="{ escape (url )} ">{ title } </a>'
179
- return f'<a class="{ class_attr } " href="{ escape (url )} ">{ title } </a>'
179
+ return f'<a class="{ class_attr } " title="{ identifier } " href="{ escape (url )} "{ attrs } >{ title } </a>'
180
+ return f'<a class="{ class_attr } " href="{ escape (url )} "{ attrs } >{ title } </a>'
180
181
181
182
return inner
182
183
Original file line number Diff line number Diff line change @@ -224,3 +224,11 @@ def test_external_references() -> None:
224
224
output , unmapped = fix_refs (source , url_map .__getitem__ )
225
225
assert output == '<a class="autorefs autorefs-external" href="https://example.com">example</a>'
226
226
assert unmapped == []
227
+
228
+
229
+ def test_keep_data_attributes () -> None :
230
+ """Keep HTML data attributes from autorefs spans."""
231
+ url_map = {"example" : "https://e.com" }
232
+ source = '<span data-autorefs-optional="example" data-foo data-bar="0">e</span>'
233
+ output , _ = fix_refs (source , url_map .__getitem__ )
234
+ assert output == '<a class="autorefs autorefs-external" href="https://e.com" data-foo data-bar="0">e</a>'
You can’t perform that action at this time.
0 commit comments