From eb35b6610327590f0a60e1eed38a052abce98649 Mon Sep 17 00:00:00 2001 From: ftnext Date: Sun, 6 Oct 2024 21:32:00 +0900 Subject: [PATCH] [bugfix] :link-icon: is same as :ref: for internal links --- src/sphinx_new_tab_link/roles.py | 17 ++++++++++++++--- tests/test_roles.py | 5 +++-- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/src/sphinx_new_tab_link/roles.py b/src/sphinx_new_tab_link/roles.py index a9f8e6b..782675b 100644 --- a/src/sphinx_new_tab_link/roles.py +++ b/src/sphinx_new_tab_link/roles.py @@ -1,5 +1,7 @@ from __future__ import annotations +from urllib.parse import urlparse + from docutils import nodes from sphinx.util.docutils import ReferenceRole @@ -17,6 +19,15 @@ class IconLinkRole(ReferenceRole): """ def run(self) -> tuple[list[nodes.Node], list[nodes.system_message]]: - node = nodes.reference(text=self.title, refuri=self.target) - reference_with_icon = add_icon_to_reference(node) - return [reference_with_icon], [] + parse_result = urlparse(self.target) + if parse_result.scheme: + node = nodes.reference( + text=self.title, refuri=self.target, internal=False + ) + reference_with_icon = add_icon_to_reference(node) + return [reference_with_icon], [] + else: + node = nodes.reference( + text=self.title, refuri=f"#{self.target}", internal=True + ) + return [node], [] diff --git a/tests/test_roles.py b/tests/test_roles.py index 8ee0d91..445007a 100644 --- a/tests/test_roles.py +++ b/tests/test_roles.py @@ -33,11 +33,12 @@ def test_see_external_link_with_icon(built_html_path: Path) -> None: assert ref.text == "httpbin " -@pytest.mark.skip("TODO bugfix #19") def test_see_internal_link_without_icon(built_html_path: Path) -> None: references = extract_references(built_html_path) - assert_reference_is_not_external(references[2]) + ref = references[2] + assert_reference_is_not_external(ref) + assert not ref.svg def test_regression_1(built_html_path: Path) -> None: