Skip to content

Commit

Permalink
Fix dirhtml canonical url (#727)
Browse files Browse the repository at this point in the history
  • Loading branch information
davidism authored Oct 15, 2023
1 parent 079d829 commit 258d554
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/furo/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from pygments.formatters import HtmlFormatter
from pygments.style import Style
from pygments.token import Text
from sphinx.builders.dirhtml import DirectoryHTMLBuilder
from sphinx.builders.html import StandaloneHTMLBuilder
from sphinx.environment.adapters.toctree import TocTree
from sphinx.errors import ConfigError
Expand Down Expand Up @@ -178,6 +179,27 @@ def _add_asset_hashes(static: List[str], add_digest_to: List[str]) -> None:
static[index].filename = _asset_hash(asset) # type: ignore[attr-defined]


def _fix_canonical_url(
app: sphinx.application.Sphinx, pagename: str, context: Dict[str, Any]
) -> None:
"""Fix the canonical URL when using the dirhtml builder.
Sphinx builds a canonical URL if ``html_baseurl`` config is set. However,
it builds a URL ending with ".html" when using the dirhtml builder, which is
incorrect. Detect this and generate the correct URL for each page.
"""
if (
not app.config.html_baseurl
or not isinstance(app.builder, DirectoryHTMLBuilder)
or not context["pageurl"]
or not context["pageurl"].endswith(".html")
):
return

target = app.builder.get_target_uri(pagename)
context["pageurl"] = app.config.html_baseurl + target


def _html_page_context(
app: sphinx.application.Sphinx,
pagename: str,
Expand All @@ -196,6 +218,8 @@ def _html_page_context(
["scripts/furo.js"],
)

_fix_canonical_url(app, pagename, context)

# Basic constants
context["furo_version"] = __version__

Expand Down

0 comments on commit 258d554

Please sign in to comment.