10
10
and fixes them using the previously stored identifier-URL mapping.
11
11
"""
12
12
13
+ import contextlib
13
14
import functools
14
15
import logging
15
- from typing import Callable , Dict , Optional
16
+ from typing import Callable , Dict , Optional , Sequence
16
17
17
18
from mkdocs .config import Config
18
19
from mkdocs .plugins import BasePlugin
@@ -68,14 +69,14 @@ def register_url(self, identifier: str, url: str):
68
69
self ._abs_url_map [identifier ] = url
69
70
70
71
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
72
73
) -> str :
73
74
"""Return a site-relative URL with anchor to the identifier, if it's present anywhere.
74
75
75
76
Arguments:
76
77
identifier: The anchor (without '#').
77
78
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.
79
80
80
81
Returns:
81
82
A site-relative URL.
@@ -90,10 +91,12 @@ def get_item_url(
90
91
return self ._abs_url_map [identifier ]
91
92
92
93
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
97
100
raise
98
101
99
102
if from_url is not None :
0 commit comments