Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added warning for multiple same anchors #50

Merged
merged 5 commits into from
Jul 5, 2024
Merged

Conversation

beilm
Copy link
Contributor

@beilm beilm commented Jul 4, 2024

  • Changed url_map to dict[str, list[str]] to allow multiple entries per identifier
  • Changed get_item_url to return always the first url found and add a warning because of multiple entries

@pawamoy
Copy link
Member

pawamoy commented Jul 5, 2024

Hi @beilm, thanks a lot for the PR!

LGTM! Just wanted to change a bit the warning and try/except flow, but GitHub rejected me 🤔

% git push
Enumerating objects: 9, done.
Counting objects: 100% (9/9), done.
Delta compression using up to 6 threads
Compressing objects: 100% (4/4), done.
Writing objects: 100% (5/5), 576 bytes | 288.00 KiB/s, done.
Total 5 (delta 3), reused 0 (delta 0), pack-reused 0 (from 0)
remote: Resolving deltas: 100% (3/3), completed with 3 local objects.
To github.com:beilm/autorefs.git
 ! [remote rejected] beilm/main -> beilm/main (permission denied)
error: failed to push some refs to 'github.com:beilm/autorefs.git'

Here's the diff if you'd like to apply it:

% git diff
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 @@ class AutorefsPlugin(BasePlugin):
         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 @@ class AutorefsPlugin(BasePlugin):
                         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,

Copy link
Member

@pawamoy pawamoy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK LGTM, thanks a lot! Will try to release soon.

@pawamoy pawamoy merged commit c630354 into mkdocstrings:main Jul 5, 2024
25 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants