1010and fixes them using the previously stored identifier-URL mapping.
1111"""
1212
13+ import contextlib
1314import functools
1415import logging
15- from typing import Callable , Dict , Optional
16+ from typing import Callable , Dict , Optional , Sequence
1617
1718from mkdocs .config import Config
1819from mkdocs .plugins import BasePlugin
@@ -68,14 +69,14 @@ def register_url(self, identifier: str, url: str):
6869 self ._abs_url_map [identifier ] = url
6970
7071 def get_item_url (
71- self , identifier : str , from_url : Optional [str ] = None , fallback : Optional [Callable [[str ], Optional [str ]]] = None
72+ self , identifier : str , from_url : Optional [str ] = None , fallback : Optional [Callable [[str ], Sequence [str ]]] = None
7273 ) -> str :
7374 """Return a site-relative URL with anchor to the identifier, if it's present anywhere.
7475
7576 Arguments:
7677 identifier: The anchor (without '#').
7778 from_url: The URL of the base page, from which we link towards the targeted pages.
78- fallback: An optional function to suggest an alternative anchor to try on failure.
79+ fallback: An optional function to suggest alternative anchors to try on failure.
7980
8081 Returns:
8182 A site-relative URL.
@@ -90,10 +91,12 @@ def get_item_url(
9091 return self ._abs_url_map [identifier ]
9192
9293 if fallback :
93- new_identifier = fallback (identifier )
94- if new_identifier :
95- return self .get_item_url (new_identifier , from_url )
96-
94+ new_identifiers = fallback (identifier )
95+ for new_identifier in new_identifiers :
96+ with contextlib .suppress (KeyError ):
97+ url = self .get_item_url (new_identifier , from_url )
98+ self ._url_map [identifier ] = url # update the map to avoid doing all this again
99+ return url
97100 raise
98101
99102 if from_url is not None :
0 commit comments