Skip to content

Commit

Permalink
fix: Patch MkDocs pages to provide an __str__ method returning thei…
Browse files Browse the repository at this point in the history
…r 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.
  • Loading branch information
pawamoy committed Feb 24, 2025
1 parent 9615d13 commit 2009f85
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/mkdocs_autorefs/_internal/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from mkdocs.config.base import Config
from mkdocs.config.config_options import Choice, Type
from mkdocs.plugins import BasePlugin, event_priority
from mkdocs.structure.pages import Page

from mkdocs_autorefs._internal.backlinks import Backlink, BacklinkCrumb
from mkdocs_autorefs._internal.references import AutorefsExtension, fix_refs, relative_url
Expand All @@ -31,7 +32,6 @@
from mkdocs.config.defaults import MkDocsConfig
from mkdocs.structure.files import Files
from mkdocs.structure.nav import Section
from mkdocs.structure.pages import Page
from mkdocs.structure.toc import AnchorLink

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

# YORE: Bump 2: Remove block.
# mkdocstrings still uses the `page` attribute as a string.
# Fortunately, it does so in f-strings, so we can simply patch the `__str__` method
# to render the URL.
Page.__str__ = lambda page: page.url # type: ignore[method-assign,attr-defined]

if self.config.link_titles == "auto":
if getattr(config.theme, "name", None) == "material" and "navigation.instant.preview" in config.theme.get(
"features",
Expand Down

0 comments on commit 2009f85

Please sign in to comment.