Skip to content

Commit

Permalink
[feat] Add fixed size external-link octicon
Browse files Browse the repository at this point in the history
  • Loading branch information
ftnext committed Jun 27, 2024
1 parent eb052ed commit cfc3735
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
11 changes: 9 additions & 2 deletions src/sphinx_new_tab_link/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from typing import TypedDict

from docutils.nodes import Text
from docutils.nodes import Text, raw
from sphinx.application import Sphinx
from sphinxcontrib.kasane import new_translator_class_for_builder

Expand All @@ -25,7 +25,14 @@ def starttag(self, node, tagname, *args, **atts):
atts["target"] = "_blank"
atts["rel"] = "noopener noreferrer"
if self.builder.config.new_tab_link_show_external_link_icon:
node[0] = Text(node[0].astext() + " (external link icon)")
node[0] = Text(node[0].astext() + " ")
node.append(
raw(
# https://primer.style/foundations/icons/link-external-16/
text='<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16" width="16" height="16"><path d="M3.75 2h3.5a.75.75 0 0 1 0 1.5h-3.5a.25.25 0 0 0-.25.25v8.5c0 .138.112.25.25.25h8.5a.25.25 0 0 0 .25-.25v-3.5a.75.75 0 0 1 1.5 0v3.5A1.75 1.75 0 0 1 12.25 14h-8.5A1.75 1.75 0 0 1 2 12.25v-8.5C2 2.784 2.784 2 3.75 2Zm6.854-1h4.146a.25.25 0 0 1 .25.25v4.146a.25.25 0 0 1-.427.177L13.03 4.03 9.28 7.78a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042l3.75-3.75-1.543-1.543A.25.25 0 0 1 10.604 1Z"></path></svg>',
format="html",
)
)
return super().starttag(node, tagname, *args, **atts)


Expand Down
8 changes: 3 additions & 5 deletions tests/test_show_external_link_icon.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def test_see_external_link_icon(
sphinx_test_tempdir: Path,
rootdir: Path,
):
srcdir = sphinx_test_tempdir / "test-external-link-icon"
srcdir = sphinx_test_tempdir / "external-link-icon"
if not srcdir.exists():
testroot_path = rootdir / "test-external-link-icon"
shutil.copytree(testroot_path, srcdir)
Expand All @@ -22,10 +22,8 @@ def test_see_external_link_icon(
references = soup.find_all("a", {"class": "reference"})

ref = references[0]
assert (
ref.text
== "https://pypi.org/project/sphinx-new-tab-link/ (external link icon)"
)
assert ref.text == "https://pypi.org/project/sphinx-new-tab-link/ "
assert ref.svg
assert ref["href"] == "https://pypi.org/project/sphinx-new-tab-link/"
assert ref["target"] == "_blank"
assert ref["rel"] == ["noopener", "noreferrer"]

0 comments on commit cfc3735

Please sign in to comment.