14
14
import functools
15
15
import logging
16
16
from typing import Callable , Dict , Optional , Sequence
17
+ from urllib .parse import urlsplit
17
18
18
19
from mkdocs .config import Config
19
20
from mkdocs .plugins import BasePlugin
@@ -68,6 +69,25 @@ def register_url(self, identifier: str, url: str):
68
69
"""
69
70
self ._abs_url_map [identifier ] = url
70
71
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
+
71
91
def get_item_url ( # noqa: WPS234
72
92
self ,
73
93
identifier : str ,
@@ -83,27 +103,12 @@ def get_item_url( # noqa: WPS234
83
103
84
104
Returns:
85
105
A site-relative URL.
86
-
87
- Raises:
88
- KeyError: If there isn't an item by this identifier anywhere on the site.
89
106
"""
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 )
105
108
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 )
107
112
return url
108
113
109
114
def on_config (self , config : Config , ** kwargs ) -> Config : # noqa: W0613,R0201 (unused arguments, cannot be static)
0 commit comments