Skip to content

Commit 2009f85

Browse files
committed
fix: Patch MkDocs pages to provide an __str__ method returning their URL
At the time of this change, mkdocstrings versions still use autorefs `current_page` attribute as an URL (string), fortunately so in f-strings (therefore casted with `str(page)`), meaning we can get away with it by simply providing an `__str__` method on these page instances.
1 parent 9615d13 commit 2009f85

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

src/mkdocs_autorefs/_internal/plugin.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
from mkdocs.config.base import Config
2121
from mkdocs.config.config_options import Choice, Type
2222
from mkdocs.plugins import BasePlugin, event_priority
23+
from mkdocs.structure.pages import Page
2324

2425
from mkdocs_autorefs._internal.backlinks import Backlink, BacklinkCrumb
2526
from mkdocs_autorefs._internal.references import AutorefsExtension, fix_refs, relative_url
@@ -31,7 +32,6 @@
3132
from mkdocs.config.defaults import MkDocsConfig
3233
from mkdocs.structure.files import Files
3334
from mkdocs.structure.nav import Section
34-
from mkdocs.structure.pages import Page
3535
from mkdocs.structure.toc import AnchorLink
3636

3737
try:
@@ -382,6 +382,12 @@ def on_config(self, config: MkDocsConfig) -> MkDocsConfig | None:
382382
_log.debug("Adding AutorefsExtension to the list")
383383
config.markdown_extensions.append(AutorefsExtension(self)) # type: ignore[arg-type]
384384

385+
# YORE: Bump 2: Remove block.
386+
# mkdocstrings still uses the `page` attribute as a string.
387+
# Fortunately, it does so in f-strings, so we can simply patch the `__str__` method
388+
# to render the URL.
389+
Page.__str__ = lambda page: page.url # type: ignore[method-assign,attr-defined]
390+
385391
if self.config.link_titles == "auto":
386392
if getattr(config.theme, "name", None) == "material" and "navigation.instant.preview" in config.theme.get(
387393
"features",

0 commit comments

Comments
 (0)