Skip to content

Commit

Permalink
Merge branch 'main' into scan-anchors
Browse files Browse the repository at this point in the history
  • Loading branch information
pawamoy committed Feb 27, 2024
2 parents 37c4169 + 0c1781d commit 2c0fe0e
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/mkdocs_autorefs/plugin.py
Expand Up @@ -59,7 +59,7 @@ def __init__(self) -> None:
super().__init__()
self._url_map: dict[str, str] = {}
self._abs_url_map: dict[str, str] = {}
self.get_fallback_anchor: Callable[[str], str | None] | None = None
self.get_fallback_anchor: Callable[[str], tuple[str, ...]] | None = None

def register_anchor(self, page: str, identifier: str, anchor: str | None = None) -> None:
"""Register that an anchor corresponding to an identifier was encountered when rendering the page.
Expand Down
15 changes: 9 additions & 6 deletions src/mkdocs_autorefs/references.py
Expand Up @@ -29,9 +29,10 @@
# TODO: remove once support for MkDocs <1.5 is dropped
log = logging.getLogger(f"mkdocs.plugins.{__name__}") # type: ignore[assignment]

_ATTR_VALUE = r'"[^"<>]+"|[^"<> ]+' # Possibly with double quotes around
AUTO_REF_RE = re.compile(
r"<span data-(?P<kind>autorefs-identifier|autorefs-optional|autorefs-optional-hover)="
r'("?)(?P<identifier>[^"<>]*)\2>(?P<title>.*?)</span>',
rf"<span data-(?P<kind>autorefs-(?:identifier|optional|optional-hover))=(?P<identifier>{_ATTR_VALUE})"
rf"(?: class=(?P<class>{_ATTR_VALUE}))?(?P<attrs> [^<>]+)?>(?P<title>.*?)</span>",
flags=re.DOTALL,
)
"""A regular expression to match mkdocs-autorefs' special reference markers
Expand Down Expand Up @@ -175,9 +176,11 @@ def fix_ref(url_mapper: Callable[[str], str], unmapped: list[str]) -> Callable:
"""

def inner(match: Match) -> str:
identifier = match["identifier"]
identifier = match["identifier"].strip('"')
title = match["title"]
kind = match["kind"]
attrs = match["attrs"] or ""
classes = (match["class"] or "").strip('"').split()

try:
url = url_mapper(unescape(identifier))
Expand All @@ -193,11 +196,11 @@ def inner(match: Match) -> str:

parsed = urlsplit(url)
external = parsed.scheme or parsed.netloc
classes = ["autorefs", "autorefs-external" if external else "autorefs-internal"]
classes = ["autorefs", "autorefs-external" if external else "autorefs-internal", *classes]
class_attr = " ".join(classes)
if kind == "autorefs-optional-hover":
return f'<a class="{class_attr}" title="{identifier}" href="{escape(url)}">{title}</a>'
return f'<a class="{class_attr}" href="{escape(url)}">{title}</a>'
return f'<a class="{class_attr}" title="{identifier}" href="{escape(url)}"{attrs}>{title}</a>'
return f'<a class="{class_attr}" href="{escape(url)}"{attrs}>{title}</a>'

return inner

Expand Down
8 changes: 8 additions & 0 deletions tests/test_references.py
Expand Up @@ -331,3 +331,11 @@ def test_register_markdown_anchors_with_admonition() -> None:
"alias2": "page#heading-bar",
"alias3": "page#alias3",
}


def test_keep_data_attributes() -> None:
"""Keep HTML data attributes from autorefs spans."""
url_map = {"example": "https://e.com"}
source = '<span data-autorefs-optional="example" class="hi ho" data-foo data-bar="0">e</span>'
output, _ = fix_refs(source, url_map.__getitem__)
assert output == '<a class="autorefs autorefs-external hi ho" href="https://e.com" data-foo data-bar="0">e</a>'

0 comments on commit 2c0fe0e

Please sign in to comment.