From 72e99685f2c77e6c7290f71c82926c5e50d1221d Mon Sep 17 00:00:00 2001 From: Markus Beil Date: Thu, 4 Jul 2024 22:35:04 +0200 Subject: [PATCH 1/5] warning for multiple same anchors --- src/mkdocs_autorefs/plugin.py | 17 ++++++++++++---- tests/test_references.py | 38 ++++++++++++++++++++--------------- 2 files changed, 35 insertions(+), 20 deletions(-) diff --git a/src/mkdocs_autorefs/plugin.py b/src/mkdocs_autorefs/plugin.py index 52e60fa..c6c22a8 100644 --- a/src/mkdocs_autorefs/plugin.py +++ b/src/mkdocs_autorefs/plugin.py @@ -57,7 +57,7 @@ class AutorefsPlugin(BasePlugin): def __init__(self) -> None: """Initialize the object.""" super().__init__() - self._url_map: dict[str, str] = {} + self._url_map: dict[str, list[str]] = {} self._abs_url_map: dict[str, str] = {} self.get_fallback_anchor: Callable[[str], tuple[str, ...]] | None = None @@ -68,7 +68,12 @@ def register_anchor(self, page: str, identifier: str, anchor: str | None = None) page: The relative URL of the current page. Examples: `'foo/bar/'`, `'foo/index.html'` identifier: The HTML anchor (without '#') as a string. """ - self._url_map[identifier] = f"{page}#{anchor or identifier}" + page_anchor = f"{page}#{anchor or identifier}" + if identifier in self._url_map: + if page_anchor not in self._url_map[identifier]: + self._url_map[identifier].append(page_anchor) + else: + self._url_map[identifier] = [page_anchor] def register_url(self, identifier: str, url: str) -> None: """Register that the identifier should be turned into a link to this URL. @@ -85,7 +90,11 @@ def _get_item_url( fallback: Callable[[str], Sequence[str]] | None = None, ) -> str: try: - return self._url_map[identifier] + if len(self._url_map[identifier]) > 1: + log.warning( + f"Multiple urls found for '{identifier}': {self._url_map[identifier]}. Please specify a custom unique identifier.", + ) + return self._url_map[identifier][0] except KeyError: if identifier in self._abs_url_map: return self._abs_url_map[identifier] @@ -94,7 +103,7 @@ def _get_item_url( for new_identifier in new_identifiers: with contextlib.suppress(KeyError): url = self._get_item_url(new_identifier) - self._url_map[identifier] = url + self._url_map[identifier] = [url] return url raise diff --git a/tests/test_references.py b/tests/test_references.py index 748eacf..dedc320 100644 --- a/tests/test_references.py +++ b/tests/test_references.py @@ -113,7 +113,7 @@ def test_reference_implicit_with_code_inlinehilite_python() -> None: extensions={"pymdownx.inlinehilite": {"style_plain_text": "python"}, "pymdownx.highlight": {}}, url_map={"pathlib.Path": "pathlib.html#Path"}, source="This [`pathlib.Path`][].", - output='

This pathlib.Path.

', + output='

This pathlib.Path.

', ) @@ -326,22 +326,28 @@ def test_register_markdown_anchors() -> None: ## Heading more2 {#heading-custom2} [](){#alias10} + + [](){#aliasSame} + ## Same heading 1 + [](){#aliasSame} + ## Same heading 2 """, ), ) assert plugin._url_map == { - "foo": "page#heading-foo", - "bar": "page#bar", - "alias1": "page#heading-bar", - "alias2": "page#heading-bar", - "alias3": "page#alias3", - "alias4": "page#heading-baz", - "alias5": "page#alias5", - "alias6": "page#alias6", - "alias7": "page#alias7", - "alias8": "page#alias8", - "alias9": "page#heading-custom2", - "alias10": "page#alias10", + "foo": ["page#heading-foo"], + "bar": ["page#bar"], + "alias1": ["page#heading-bar"], + "alias2": ["page#heading-bar"], + "alias3": ["page#alias3"], + "alias4": ["page#heading-baz"], + "alias5": ["page#alias5"], + "alias6": ["page#alias6"], + "alias7": ["page#alias7"], + "alias8": ["page#alias8"], + "alias9": ["page#heading-custom2"], + "alias10": ["page#alias10"], + "aliasSame": ["page#same-heading-1", "page#same-heading-2"] } @@ -366,9 +372,9 @@ def test_register_markdown_anchors_with_admonition() -> None: ), ) assert plugin._url_map == { - "alias1": "page#alias1", - "alias2": "page#heading-bar", - "alias3": "page#alias3", + "alias1": ["page#alias1"], + "alias2": ["page#heading-bar"], + "alias3": ["page#alias3"], } From 3f42987102d7626c2175f60fbeccbc9613e44ac4 Mon Sep 17 00:00:00 2001 From: Markus Beil Date: Thu, 4 Jul 2024 22:44:47 +0200 Subject: [PATCH 2/5] fixed code quality check --- tests/test_references.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_references.py b/tests/test_references.py index dedc320..30e7822 100644 --- a/tests/test_references.py +++ b/tests/test_references.py @@ -347,7 +347,7 @@ def test_register_markdown_anchors() -> None: "alias8": ["page#alias8"], "alias9": ["page#heading-custom2"], "alias10": ["page#alias10"], - "aliasSame": ["page#same-heading-1", "page#same-heading-2"] + "aliasSame": ["page#same-heading-1", "page#same-heading-2"], } From 9092f733b0639a1ec60f9c827770c6e9eb33b106 Mon Sep 17 00:00:00 2001 From: Markus Beil Date: Thu, 4 Jul 2024 22:49:20 +0200 Subject: [PATCH 3/5] fix checks again --- tests/test_references.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/test_references.py b/tests/test_references.py index 30e7822..82da870 100644 --- a/tests/test_references.py +++ b/tests/test_references.py @@ -113,7 +113,7 @@ def test_reference_implicit_with_code_inlinehilite_python() -> None: extensions={"pymdownx.inlinehilite": {"style_plain_text": "python"}, "pymdownx.highlight": {}}, url_map={"pathlib.Path": "pathlib.html#Path"}, source="This [`pathlib.Path`][].", - output='

This pathlib.Path.

', + output='

This pathlib.Path.

', ) @@ -325,12 +325,12 @@ def test_register_markdown_anchors() -> None: [](){#alias9} ## Heading more2 {#heading-custom2} - [](){#alias10} - [](){#aliasSame} ## Same heading 1 [](){#aliasSame} ## Same heading 2 + + [](){#alias10} """, ), ) From 08eeec6456b1ea2f4b6371b5f4e30c5ea7bf7d7c Mon Sep 17 00:00:00 2001 From: Markus Beil Date: Fri, 5 Jul 2024 16:48:32 +0200 Subject: [PATCH 4/5] review changes --- src/mkdocs_autorefs/plugin.py | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/src/mkdocs_autorefs/plugin.py b/src/mkdocs_autorefs/plugin.py index c6c22a8..e7428ff 100644 --- a/src/mkdocs_autorefs/plugin.py +++ b/src/mkdocs_autorefs/plugin.py @@ -90,11 +90,7 @@ def _get_item_url( fallback: Callable[[str], Sequence[str]] | None = None, ) -> str: try: - if len(self._url_map[identifier]) > 1: - log.warning( - f"Multiple urls found for '{identifier}': {self._url_map[identifier]}. Please specify a custom unique identifier.", - ) - return self._url_map[identifier][0] + urls = self._url_map[identifier] except KeyError: if identifier in self._abs_url_map: return self._abs_url_map[identifier] @@ -106,6 +102,13 @@ def _get_item_url( self._url_map[identifier] = [url] return url raise + else: + if len(urls) > 1: + log.warning( + f"Multiple URLs found for '{identifier}': {urls}. " + "Please use unique identifiers, or unique Markdown anchors (see our docs).", + ) + return urls[0] def get_item_url( self, From eeeb4ece8abe7a702a377c74dc7bc526d91ca009 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timoth=C3=A9e=20Mazzucotelli?= Date: Fri, 5 Jul 2024 18:08:02 +0200 Subject: [PATCH 5/5] Update src/mkdocs_autorefs/plugin.py --- src/mkdocs_autorefs/plugin.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/mkdocs_autorefs/plugin.py b/src/mkdocs_autorefs/plugin.py index e7428ff..a52f878 100644 --- a/src/mkdocs_autorefs/plugin.py +++ b/src/mkdocs_autorefs/plugin.py @@ -106,7 +106,7 @@ def _get_item_url( if len(urls) > 1: log.warning( f"Multiple URLs found for '{identifier}': {urls}. " - "Please use unique identifiers, or unique Markdown anchors (see our docs).", + "Make sure to use unique headings, identifiers, or Markdown anchors (see our docs).", ) return urls[0]