Skip to content

Commit c630354

Browse files
beilmpawamoy
andauthored
feat: Warn when multiple URLs are found for the same identifier
Issue-35: #35 PR-50: #50 Co-authored-by: Timothée Mazzucotelli <[email protected]>
1 parent e142023 commit c630354

File tree

2 files changed

+37
-19
lines changed

2 files changed

+37
-19
lines changed

src/mkdocs_autorefs/plugin.py

+16-4
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ class AutorefsPlugin(BasePlugin):
5757
def __init__(self) -> None:
5858
"""Initialize the object."""
5959
super().__init__()
60-
self._url_map: dict[str, str] = {}
60+
self._url_map: dict[str, list[str]] = {}
6161
self._abs_url_map: dict[str, str] = {}
6262
self.get_fallback_anchor: Callable[[str], tuple[str, ...]] | None = None
6363

@@ -68,7 +68,12 @@ def register_anchor(self, page: str, identifier: str, anchor: str | None = None)
6868
page: The relative URL of the current page. Examples: `'foo/bar/'`, `'foo/index.html'`
6969
identifier: The HTML anchor (without '#') as a string.
7070
"""
71-
self._url_map[identifier] = f"{page}#{anchor or identifier}"
71+
page_anchor = f"{page}#{anchor or identifier}"
72+
if identifier in self._url_map:
73+
if page_anchor not in self._url_map[identifier]:
74+
self._url_map[identifier].append(page_anchor)
75+
else:
76+
self._url_map[identifier] = [page_anchor]
7277

7378
def register_url(self, identifier: str, url: str) -> None:
7479
"""Register that the identifier should be turned into a link to this URL.
@@ -85,7 +90,7 @@ def _get_item_url(
8590
fallback: Callable[[str], Sequence[str]] | None = None,
8691
) -> str:
8792
try:
88-
return self._url_map[identifier]
93+
urls = self._url_map[identifier]
8994
except KeyError:
9095
if identifier in self._abs_url_map:
9196
return self._abs_url_map[identifier]
@@ -94,9 +99,16 @@ def _get_item_url(
9499
for new_identifier in new_identifiers:
95100
with contextlib.suppress(KeyError):
96101
url = self._get_item_url(new_identifier)
97-
self._url_map[identifier] = url
102+
self._url_map[identifier] = [url]
98103
return url
99104
raise
105+
else:
106+
if len(urls) > 1:
107+
log.warning(
108+
f"Multiple URLs found for '{identifier}': {urls}. "
109+
"Make sure to use unique headings, identifiers, or Markdown anchors (see our docs).",
110+
)
111+
return urls[0]
100112

101113
def get_item_url(
102114
self,

tests/test_references.py

+21-15
Original file line numberDiff line numberDiff line change
@@ -325,23 +325,29 @@ def test_register_markdown_anchors() -> None:
325325
[](){#alias9}
326326
## Heading more2 {#heading-custom2}
327327
328+
[](){#aliasSame}
329+
## Same heading 1
330+
[](){#aliasSame}
331+
## Same heading 2
332+
328333
[](){#alias10}
329334
""",
330335
),
331336
)
332337
assert plugin._url_map == {
333-
"foo": "page#heading-foo",
334-
"bar": "page#bar",
335-
"alias1": "page#heading-bar",
336-
"alias2": "page#heading-bar",
337-
"alias3": "page#alias3",
338-
"alias4": "page#heading-baz",
339-
"alias5": "page#alias5",
340-
"alias6": "page#alias6",
341-
"alias7": "page#alias7",
342-
"alias8": "page#alias8",
343-
"alias9": "page#heading-custom2",
344-
"alias10": "page#alias10",
338+
"foo": ["page#heading-foo"],
339+
"bar": ["page#bar"],
340+
"alias1": ["page#heading-bar"],
341+
"alias2": ["page#heading-bar"],
342+
"alias3": ["page#alias3"],
343+
"alias4": ["page#heading-baz"],
344+
"alias5": ["page#alias5"],
345+
"alias6": ["page#alias6"],
346+
"alias7": ["page#alias7"],
347+
"alias8": ["page#alias8"],
348+
"alias9": ["page#heading-custom2"],
349+
"alias10": ["page#alias10"],
350+
"aliasSame": ["page#same-heading-1", "page#same-heading-2"],
345351
}
346352

347353

@@ -366,9 +372,9 @@ def test_register_markdown_anchors_with_admonition() -> None:
366372
),
367373
)
368374
assert plugin._url_map == {
369-
"alias1": "page#alias1",
370-
"alias2": "page#heading-bar",
371-
"alias3": "page#alias3",
375+
"alias1": ["page#alias1"],
376+
"alias2": ["page#heading-bar"],
377+
"alias3": ["page#alias3"],
372378
}
373379

374380

0 commit comments

Comments
 (0)