1616this plugin searches for references of the form `[identifier][]` or `[title][identifier]` that were not resolved,
1717and fixes them using the previously stored identifier-URL mapping.
1818"""
19-
2019import contextlib
2120import functools
2221import logging
22+ import os
2323import re
2424from html import escape , unescape
2525from typing import Any , Callable , Dict , List , Match , Optional , Sequence , Tuple , Union
2626from urllib .parse import urlsplit
2727from xml .etree .ElementTree import Element
2828
29- import pathspec
3029from markdown import Markdown
3130from markdown .extensions import Extension
3231from markdown .inlinepatterns import REFERENCE_RE , ReferenceInlineProcessor
@@ -278,35 +277,37 @@ def __init__(self) -> None:
278277 def priority_patterns (self ):
279278 if self ._priority_patterns is None :
280279 self ._priority_patterns = [
281- pathspec .patterns .GitWildMatchPattern (pat )
282- for pat in self .config .get ("priority" )
280+ os .path .join ("/" , pat ) for pat in self .config .get ("priority" )
283281 ]
284282 return self ._priority_patterns
285283
286- def register_anchor (self , page : str , identifier : str ):
284+ def register_anchor (self , url : str , identifier : str ):
287285 """Register that an anchor corresponding to an identifier was encountered when rendering the page.
288286
289287 Arguments:
290- page : The relative URL of the current page. Examples: `'foo/bar/'`, `'foo/index.html'`
288+ url : The relative URL of the current page. Examples: `'foo/bar/'`, `'foo/index.html'`
291289 identifier: The HTML anchor (without '#') as a string.
292290 """
293- if identifier in self ._url_map :
291+
292+ new_url = os .path .join ("/" , f"{ url } #{ identifier } " )
293+ old_url = os .path .join ("/" , self ._url_map .get (identifier , "" )).split ("#" )[0 ]
294+
295+ if identifier in self ._url_map and not old_url == new_url :
294296 rev_patterns = list (enumerate (self .priority_patterns ))[::- 1 ]
295297 old_priority_idx = next (
296- (
297- i
298- for i , pat in rev_patterns
299- if pat .match_file (self ._url_map [identifier ])
300- ),
298+ (i for i , pat in rev_patterns if re .match (pat , old_url )),
301299 len (rev_patterns ),
302300 )
303301 new_priority_idx = next (
304- (i for i , pat in rev_patterns if pat . match_file ( page )),
302+ (i for i , pat in rev_patterns if re . match ( pat , new_url )),
305303 len (rev_patterns ),
306304 )
307305 if new_priority_idx >= old_priority_idx :
308306 return
309- self ._url_map [identifier ] = f"{ page } #{ identifier } "
307+ if "reference" not in new_url :
308+ raise Exception ("URL WTF" , new_url )
309+
310+ self ._url_map [identifier ] = new_url
310311
311312 def register_url (self , identifier : str , url : str ):
312313 """Register that the identifier should be turned into a link to this URL.
@@ -352,12 +353,7 @@ def get_item_url( # noqa: WPS234
352353 Returns:
353354 A site-relative URL.
354355 """
355- url = self ._get_item_url (identifier , fallback )
356- if from_url is not None :
357- parsed = urlsplit (url )
358- if not parsed .scheme and not parsed .netloc :
359- return relative_url (from_url , url )
360- return url
356+ return self ._get_item_url (identifier , fallback )
361357
362358 def on_config (
363359 self , config : Config , ** kwargs
@@ -418,10 +414,10 @@ def on_page_content(
418414 f"{ __name__ } : Mapping identifiers to URLs for page { page .file .src_path } "
419415 )
420416 for item in page .toc .items :
421- self .map_urls (page . url , item )
417+ self .map_urls (page , item )
422418 return html
423419
424- def map_urls (self , base_url : str , anchor : AnchorLink ) -> None :
420+ def map_urls (self , page : Page , anchor : AnchorLink ) -> None :
425421 """Recurse on every anchor to map its ID to its absolute URL.
426422
427423 This method populates `self.url_map` by side-effect.
@@ -430,9 +426,10 @@ def map_urls(self, base_url: str, anchor: AnchorLink) -> None:
430426 base_url: The base URL to use as a prefix for each anchor's relative URL.
431427 anchor: The anchor to process and to recurse on.
432428 """
433- self .register_anchor (base_url , anchor .id )
429+ abs_url = os .path .join ("/" , page .file .url )
430+ self .register_anchor (abs_url , anchor .id )
434431 for child in anchor .children :
435- self .map_urls (base_url , child )
432+ self .map_urls (page , child )
436433
437434 def on_post_page (
438435 self , output : str , page : Page , ** kwargs
0 commit comments