Skip to content

Commit 39db59d

Browse files
authored
feat: Add HTML classes to references
PR #16: #16 Co-authored-by: Oleh Prypin <[email protected]>
1 parent f6b861c commit 39db59d

File tree

2 files changed

+27
-10
lines changed

2 files changed

+27
-10
lines changed

src/mkdocs_autorefs/references.py

+7-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import re
44
from html import escape, unescape
55
from typing import Any, Callable, List, Match, Tuple, Union
6+
from urllib.parse import urlsplit
67
from xml.etree.ElementTree import Element
78

89
from markdown import Markdown
@@ -164,9 +165,13 @@ def inner(match: Match): # noqa: WPS212,WPS430
164165
return f"[{identifier}][]"
165166
return f"[{title}][{identifier}]"
166167

168+
parsed = urlsplit(url)
169+
external = parsed.scheme or parsed.netloc
170+
classes = ["autorefs", "autorefs-external" if external else "autorefs-internal"]
171+
class_attr = " ".join(classes)
167172
if kind == "autorefs-optional-hover":
168-
return f'<a title="{identifier}" href="{escape(url)}">{title}</a>'
169-
return f'<a href="{escape(url)}">{title}</a>'
173+
return f'<a class="{class_attr}" title="{identifier}" href="{escape(url)}">{title}</a>'
174+
return f'<a class="{class_attr}" href="{escape(url)}">{title}</a>'
170175

171176
return inner
172177

tests/test_references.py

+20-8
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ def test_reference_implicit():
6161
run_references_test(
6262
url_map={"Foo": "foo.html#Foo"},
6363
source="This [Foo][].",
64-
output='<p>This <a href="foo.html#Foo">Foo</a>.</p>',
64+
output='<p>This <a class="autorefs autorefs-internal" href="foo.html#Foo">Foo</a>.</p>',
6565
)
6666

6767

@@ -70,7 +70,7 @@ def test_reference_explicit_with_markdown_text():
7070
run_references_test(
7171
url_map={"Foo": "foo.html#Foo"},
7272
source="This [**Foo**][Foo].",
73-
output='<p>This <a href="foo.html#Foo"><strong>Foo</strong></a>.</p>',
73+
output='<p>This <a class="autorefs autorefs-internal" href="foo.html#Foo"><strong>Foo</strong></a>.</p>',
7474
)
7575

7676

@@ -79,7 +79,7 @@ def test_reference_implicit_with_code():
7979
run_references_test(
8080
url_map={"Foo": "foo.html#Foo"},
8181
source="This [`Foo`][].",
82-
output='<p>This <a href="foo.html#Foo"><code>Foo</code></a>.</p>',
82+
output='<p>This <a class="autorefs autorefs-internal" href="foo.html#Foo"><code>Foo</code></a>.</p>',
8383
)
8484

8585

@@ -88,7 +88,7 @@ def test_reference_with_punctuation():
8888
run_references_test(
8989
url_map={'Foo&"bar': 'foo.html#Foo&"bar'},
9090
source='This [Foo&"bar][].',
91-
output='<p>This <a href="foo.html#Foo&amp;&quot;bar">Foo&amp;"bar</a>.</p>',
91+
output='<p>This <a class="autorefs autorefs-internal" href="foo.html#Foo&amp;&quot;bar">Foo&amp;"bar</a>.</p>',
9292
)
9393

9494

@@ -98,7 +98,7 @@ def test_reference_to_relative_path():
9898
from_url="sub/sub/page.html",
9999
url_map={"zz": "foo.html#zz"},
100100
source="This [zz][].",
101-
output='<p>This <a href="../../foo.html#zz">zz</a>.</p>',
101+
output='<p>This <a class="autorefs autorefs-internal" href="../../foo.html#zz">zz</a>.</p>',
102102
)
103103

104104

@@ -174,7 +174,7 @@ def test_custom_required_reference():
174174
url_map = {"ok": "ok.html#ok"}
175175
source = "<span data-autorefs-identifier=bar>foo</span> <span data-autorefs-identifier=ok>ok</span>"
176176
output, unmapped = fix_refs(source, url_map.__getitem__) # noqa: WPS609
177-
assert output == '[foo][bar] <a href="ok.html#ok">ok</a>'
177+
assert output == '[foo][bar] <a class="autorefs autorefs-internal" href="ok.html#ok">ok</a>'
178178
assert unmapped == ["bar"]
179179

180180

@@ -183,7 +183,7 @@ def test_custom_optional_reference():
183183
url_map = {"ok": "ok.html#ok"}
184184
source = '<span data-autorefs-optional="bar">foo</span> <span data-autorefs-optional=ok>ok</span>'
185185
output, unmapped = fix_refs(source, url_map.__getitem__) # noqa: WPS609
186-
assert output == 'foo <a href="ok.html#ok">ok</a>'
186+
assert output == 'foo <a class="autorefs autorefs-internal" href="ok.html#ok">ok</a>'
187187
assert unmapped == [] # noqa: WPS520
188188

189189

@@ -192,5 +192,17 @@ def test_custom_optional_hover_reference():
192192
url_map = {"ok": "ok.html#ok"}
193193
source = '<span data-autorefs-optional-hover="bar">foo</span> <span data-autorefs-optional-hover=ok>ok</span>'
194194
output, unmapped = fix_refs(source, url_map.__getitem__) # noqa: WPS609
195-
assert output == '<span title="bar">foo</span> <a title="ok" href="ok.html#ok">ok</a>'
195+
assert (
196+
output
197+
== '<span title="bar">foo</span> <a class="autorefs autorefs-internal" title="ok" href="ok.html#ok">ok</a>'
198+
)
199+
assert unmapped == [] # noqa: WPS520
200+
201+
202+
def test_external_references():
203+
"""Check that external references are marked as such."""
204+
url_map = {"example": "https://example.com"}
205+
source = '<span data-autorefs-optional="example">example</span>'
206+
output, unmapped = fix_refs(source, url_map.__getitem__) # noqa: WPS609
207+
assert output == '<a class="autorefs autorefs-external" href="https://example.com">example</a>'
196208
assert unmapped == [] # noqa: WPS520

0 commit comments

Comments
 (0)