1414import functools
1515import logging
1616from typing import Callable , Dict , Optional , Sequence
17+ from urllib .parse import urlsplit
1718
1819from mkdocs .config import Config
1920from mkdocs .plugins import BasePlugin
@@ -68,6 +69,25 @@ def register_url(self, identifier: str, url: str):
6869 """
6970 self ._abs_url_map [identifier ] = url
7071
72+ def _get_item_url ( # noqa: WPS234
73+ self ,
74+ identifier : str ,
75+ fallback : Optional [Callable [[str ], Sequence [str ]]] = None ,
76+ ) -> str :
77+ try :
78+ return self ._url_map [identifier ]
79+ except KeyError :
80+ if identifier in self ._abs_url_map :
81+ return self ._abs_url_map [identifier ]
82+ if fallback :
83+ new_identifiers = fallback (identifier )
84+ for new_identifier in new_identifiers :
85+ with contextlib .suppress (KeyError ):
86+ url = self ._get_item_url (new_identifier )
87+ self ._url_map [identifier ] = url
88+ return url
89+ raise
90+
7191 def get_item_url ( # noqa: WPS234
7292 self ,
7393 identifier : str ,
@@ -83,27 +103,12 @@ def get_item_url( # noqa: WPS234
83103
84104 Returns:
85105 A site-relative URL.
86-
87- Raises:
88- KeyError: If there isn't an item by this identifier anywhere on the site.
89106 """
90- try :
91- url = self ._url_map [identifier ]
92- except KeyError :
93- if identifier in self ._abs_url_map :
94- return self ._abs_url_map [identifier ]
95-
96- if fallback :
97- new_identifiers = fallback (identifier )
98- for new_identifier in new_identifiers :
99- with contextlib .suppress (KeyError ):
100- url = self .get_item_url (new_identifier , from_url )
101- self ._url_map [identifier ] = url # update the map to avoid doing all this again
102- return url
103- raise
104-
107+ url = self ._get_item_url (identifier , fallback )
105108 if from_url is not None :
106- return relative_url (from_url , url )
109+ parsed = urlsplit (url )
110+ if not parsed .scheme and not parsed .netloc :
111+ return relative_url (from_url , url )
107112 return url
108113
109114 def on_config (self , config : Config , ** kwargs ) -> Config : # noqa: W0613,R0201 (unused arguments, cannot be static)
0 commit comments