44import logging
55import posixpath
66import warnings
7- from typing import TYPE_CHECKING , Any , Callable , Iterator , MutableMapping
7+ from typing import TYPE_CHECKING , Any , Callable , Iterator , MutableMapping , Sequence
88from urllib .parse import unquote as urlunquote
99from urllib .parse import urljoin , urlsplit , urlunsplit
1010
1111import markdown
12- import markdown .extensions
13- import markdown .postprocessors
1412import markdown .treeprocessors
1513from markdown .util import AMP_SUBSTITUTE
1614
2220if TYPE_CHECKING :
2321 from xml .etree import ElementTree as etree
2422
23+ import markdown .postprocessors
24+
2525 from mkdocs .config .defaults import MkDocsConfig
2626 from mkdocs .structure .files import File , Files
2727 from mkdocs .structure .toc import TableOfContents
2828
29- _unescape : Callable [[str ], str ]
30- try :
31- _unescape = markdown .treeprocessors .UnescapeTreeprocessor ().unescape # type: ignore
32- except AttributeError :
33- _unescape = markdown .postprocessors .UnescapePostprocessor ().run
34-
3529
3630log = logging .getLogger (__name__ )
3731
@@ -437,6 +431,7 @@ def _register(self, md: markdown.Markdown) -> None:
437431
438432class _ExtractTitleTreeprocessor (markdown .treeprocessors .Treeprocessor ):
439433 title : str | None = None
434+ postprocessors : Sequence [markdown .postprocessors .Postprocessor ] = ()
440435
441436 def run (self , root : etree .Element ) -> etree .Element :
442437 for el in root :
@@ -446,13 +441,18 @@ def run(self, root: etree.Element) -> etree.Element:
446441 el = copy .copy (el )
447442 del el [- 1 ]
448443 # Extract the text only, recursively.
449- self .title = _unescape ('' .join (el .itertext ()))
444+ title = '' .join (el .itertext ())
445+ # Unescape per Markdown implementation details.
446+ for pp in self .postprocessors :
447+ title = pp .run (title )
448+ self .title = title
450449 break
451450 return root
452451
453452 def _register (self , md : markdown .Markdown ) -> None :
453+ self .postprocessors = tuple (md .postprocessors )
454454 md .treeprocessors .register (
455455 self ,
456456 "mkdocs_extract_title" ,
457- priority = 1 , # Close to the end.
457+ priority = - 1 , # After the end.
458458 )
0 commit comments