Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ and Octicons v19.42.0. Sphinx 7.2 or later is now required.
a paragraph's classes instead of replacing them; paragraphs nested deeper (in admonitions, lists, nested tab sets)
no longer get it ({pr}`278`).

### Fixed

- 🐛 `card` and `grid-item-card` `:link:` values ending in `_` now resolve
reStructuredText hyperlink targets, including external targets defined in `rst_epilog`
by {user}`draarivpatel-ui` in {issue}`164`

### New

- ✨ `accordion` directive: groups `dropdown`s so that opening one closes the others,
Expand Down
27 changes: 21 additions & 6 deletions sphinx_design/cards.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,13 +220,28 @@ def _create_link(
_rawtext = options.get("link-alt") or link_raw
link: nodes.Element
if link_type == "url":
link = nodes.reference(
_rawtext,
"",
nodes.inline(_rawtext, _rawtext),
refuri=link_target,
classes=_classes,
is_rst_reference = (
link_raw.endswith("_")
and not link_raw.startswith("#")
and not re.match(r"^[a-zA-Z][a-zA-Z0-9+.-]*:", link_raw)
)
if is_rst_reference:
link = nodes.reference(
_rawtext,
"",
nodes.inline(_rawtext, _rawtext),
name=link_raw[:-1],
refname=nodes.fully_normalize_name(link_raw[:-1]),
classes=_classes,
)
else:
link = nodes.reference(
_rawtext,
"",
nodes.inline(_rawtext, _rawtext),
refuri=link_target,
classes=_classes,
)
else:
ref_options = {
# TODO the presence of classes raises an error if the link cannot be found
Expand Down
25 changes: 25 additions & 0 deletions sphinx_design/extension.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ def setup_extension(app: Sphinx) -> None:
"config-inited", partial(setup_custom_directives, directive_map=directive_map)
)
app.connect("doctree-resolved", _resolve_new_tab_refs)
app.connect("doctree-resolved", _resolve_card_rst_link_refs)


@contextmanager
Expand Down Expand Up @@ -106,6 +107,30 @@ def _resolve_new_tab_refs(app: Sphinx, doctree: nodes.document, docname: str) ->
ref["rel"] = "noreferrer noopener"


def _resolve_card_rst_link_refs(
app: Sphinx, doctree: nodes.document, docname: str
) -> None:
"""Resolve card references to reStructuredText external targets.

A ``refname`` reference normally resolves to the matching target node with
``refid``. When that target itself is external (``refuri``), mirror the
behaviour of an inline reStructuredText reference and link directly to the
external URI instead.
"""
for ref in doctree.findall(nodes.reference):
if "sd-stretched-link" not in ref.get("classes", []) or "refid" not in ref:
continue
target = doctree.ids.get(ref["refid"])
if target is None or "refuri" not in target:
continue
ref["refuri"] = target["refuri"]
del ref["refid"]
classes = [c for c in ref.get("classes", []) if c != "internal"]
if "external" not in classes:
classes.append("external")
ref["classes"] = classes


def add_static_assets(app: Sphinx) -> None:
"""Register the extension's static assets (HTML-format builders only)."""
if app.builder.format != "html":
Expand Down
20 changes: 20 additions & 0 deletions tests/test_card_link.py
Original file line number Diff line number Diff line change
Expand Up @@ -503,3 +503,23 @@ def test_card_link_without_new_tab(fmt, sphinx_builder):
for tag in _stretched_link_tags(html):
assert "target=" not in tag, tag
assert "rel=" not in tag, tag


def test_card_rst_hyperlink_target(sphinx_builder):
"""A ``:link:`` ending in an underscore resolves as a reStructuredText target."""
rst = """
Heading
-------

.. _hyperlink-target: https://example.com/external

.. grid:: 1

.. grid-item-card:: A grid card
:link: hyperlink-target_

Card body
"""
builder = _build(sphinx_builder, "rst", rst, "")
html = (builder.out_path / "index.html").read_text(encoding="utf8")
assert "https://example.com/external" in _stretched_link_hrefs(html)