Skip to content

Commit f6b861c

Browse files
authored
fix: Don't compute relative URLs of already relative ones
PR #15: #15 Co-authored-by: Oleh Prypin <[email protected]>
1 parent 85f3920 commit f6b861c

File tree

3 files changed

+38
-19
lines changed

3 files changed

+38
-19
lines changed

config/flake8.ini

+2
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,8 @@ ignore =
9393
WPS326
9494
# explicit string concatenation
9595
WPS336
96+
# methods order in class
97+
WPS338
9698
# raw strings
9799
WPS360
98100
# noqa overuse

src/mkdocs_autorefs/plugin.py

+24-19
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import functools
1515
import logging
1616
from typing import Callable, Dict, Optional, Sequence
17+
from urllib.parse import urlsplit
1718

1819
from mkdocs.config import Config
1920
from mkdocs.plugins import BasePlugin
@@ -68,6 +69,25 @@ def register_url(self, identifier: str, url: str):
6869
"""
6970
self._abs_url_map[identifier] = url
7071

72+
def _get_item_url( # noqa: WPS234
73+
self,
74+
identifier: str,
75+
fallback: Optional[Callable[[str], Sequence[str]]] = None,
76+
) -> str:
77+
try:
78+
return self._url_map[identifier]
79+
except KeyError:
80+
if identifier in self._abs_url_map:
81+
return self._abs_url_map[identifier]
82+
if fallback:
83+
new_identifiers = fallback(identifier)
84+
for new_identifier in new_identifiers:
85+
with contextlib.suppress(KeyError):
86+
url = self._get_item_url(new_identifier)
87+
self._url_map[identifier] = url
88+
return url
89+
raise
90+
7191
def get_item_url( # noqa: WPS234
7292
self,
7393
identifier: str,
@@ -83,27 +103,12 @@ def get_item_url( # noqa: WPS234
83103
84104
Returns:
85105
A site-relative URL.
86-
87-
Raises:
88-
KeyError: If there isn't an item by this identifier anywhere on the site.
89106
"""
90-
try:
91-
url = self._url_map[identifier]
92-
except KeyError:
93-
if identifier in self._abs_url_map:
94-
return self._abs_url_map[identifier]
95-
96-
if fallback:
97-
new_identifiers = fallback(identifier)
98-
for new_identifier in new_identifiers:
99-
with contextlib.suppress(KeyError):
100-
url = self.get_item_url(new_identifier, from_url)
101-
self._url_map[identifier] = url # update the map to avoid doing all this again
102-
return url
103-
raise
104-
107+
url = self._get_item_url(identifier, fallback)
105108
if from_url is not None:
106-
return relative_url(from_url, url)
109+
parsed = urlsplit(url)
110+
if not parsed.scheme and not parsed.netloc:
111+
return relative_url(from_url, url)
107112
return url
108113

109114
def on_config(self, config: Config, **kwargs) -> Config: # noqa: W0613,R0201 (unused arguments, cannot be static)

tests/test_plugin.py

+12
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,15 @@ def test_url_registration_with_fallback():
4545
plugin.get_item_url("foobar", fallback=lambda _: ("baaaa",))
4646
with pytest.raises(KeyError):
4747
plugin.get_item_url("foobar", fallback=lambda _: ())
48+
49+
50+
def test_dont_make_relative_urls_relative_again():
51+
"""Check that URLs are not made relative more than once."""
52+
plugin = AutorefsPlugin()
53+
plugin.register_anchor(identifier="foo.bar.baz", page="foo/bar/baz.html")
54+
55+
for _ in range(2):
56+
assert (
57+
plugin.get_item_url("hello", from_url="baz/bar/foo.html", fallback=lambda _: ("foo.bar.baz",))
58+
== "../../foo/bar/baz.html#foo.bar.baz"
59+
)

0 commit comments

Comments
 (0)