diff --git a/src/mkdocs_autorefs/references.py b/src/mkdocs_autorefs/references.py index 32e8f5b..b02c0d4 100644 --- a/src/mkdocs_autorefs/references.py +++ b/src/mkdocs_autorefs/references.py @@ -3,6 +3,7 @@ import re from html import escape, unescape from typing import Any, Callable, List, Match, Tuple, Union +from urllib.parse import urlsplit from xml.etree.ElementTree import Element from markdown import Markdown @@ -164,9 +165,13 @@ def inner(match: Match): # noqa: WPS212,WPS430 return f"[{identifier}][]" return f"[{title}][{identifier}]" + parsed = urlsplit(url) + external = parsed.scheme or parsed.netloc + classes = ["autorefs", "autorefs-external" if external else "autorefs-internal"] + 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 188b223..136cf14 100644 --- a/tests/test_references.py +++ b/tests/test_references.py @@ -61,7 +61,7 @@ def test_reference_implicit(): run_references_test( url_map={"Foo": "foo.html#Foo"}, source="This [Foo][].", - output='
This Foo.
', + output='This Foo.
', ) @@ -70,7 +70,7 @@ def test_reference_explicit_with_markdown_text(): run_references_test( url_map={"Foo": "foo.html#Foo"}, source="This [**Foo**][Foo].", - output='This Foo.
', + output='This Foo.
', ) @@ -79,7 +79,7 @@ def test_reference_implicit_with_code(): run_references_test( url_map={"Foo": "foo.html#Foo"}, source="This [`Foo`][].", - output='This Foo
.
This Foo
.
This Foo&"bar.
', + output='This Foo&"bar.
', ) @@ -98,7 +98,7 @@ def test_reference_to_relative_path(): from_url="sub/sub/page.html", url_map={"zz": "foo.html#zz"}, source="This [zz][].", - output='This zz.
', + output='This zz.
', ) @@ -174,7 +174,7 @@ def test_custom_required_reference(): url_map = {"ok": "ok.html#ok"} source = "foo ok" output, unmapped = fix_refs(source, url_map.__getitem__) # noqa: WPS609 - assert output == '[foo][bar] ok' + assert output == '[foo][bar] ok' assert unmapped == ["bar"] @@ -183,7 +183,7 @@ def test_custom_optional_reference(): url_map = {"ok": "ok.html#ok"} source = 'foo ok' output, unmapped = fix_refs(source, url_map.__getitem__) # noqa: WPS609 - assert output == 'foo ok' + assert output == 'foo ok' assert unmapped == [] # noqa: WPS520 @@ -192,5 +192,17 @@ def test_custom_optional_hover_reference(): url_map = {"ok": "ok.html#ok"} source = 'foo ok' output, unmapped = fix_refs(source, url_map.__getitem__) # noqa: WPS609 - assert output == 'foo ok' + assert ( + output + == 'foo ok' + ) + assert unmapped == [] # noqa: WPS520 + + +def test_external_references(): + """Check that external references are marked as such.""" + url_map = {"example": "https://example.com"} + source = 'example' + output, unmapped = fix_refs(source, url_map.__getitem__) # noqa: WPS609 + assert output == 'example' assert unmapped == [] # noqa: WPS520