Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix URLs for dirhtml builds #1699

Merged
merged 2 commits into from
Feb 16, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions src/pydata_sphinx_theme/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import requests
from requests.exceptions import ConnectionError, HTTPError, RetryError
from sphinx.application import Sphinx
from sphinx.builders.dirhtml import DirectoryHTMLBuilder
from sphinx.errors import ExtensionError

from . import edit_this_page, logo, pygment, short_link, toctree, translator, utils
Expand Down Expand Up @@ -54,7 +55,7 @@
"The default value for `navigation_with_keys` will change to `False` in "
"the next release. If you wish to preserve the old behavior for your site, "
"set `navigation_with_keys=True` in the `html_theme_options` dict in your "
"`conf.py` file. Be aware that `navigation_with_keys = True` has negative "

Check warning on line 58 in src/pydata_sphinx_theme/__init__.py

View check run for this annotation

Codecov / codecov/patch

src/pydata_sphinx_theme/__init__.py#L58

Added line #L58 was not covered by tests
"accessibility implications: "
"https://github.com/pydata/pydata-sphinx-theme/issues/1492"
)
Expand Down Expand Up @@ -262,6 +263,30 @@
context["theme_version"] = __version__


def _fix_canonical_url(
app: Sphinx, pagename: str, templatename: str, context: dict, doctree
) -> 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.

Workaround for https://github.com/sphinx-doc/sphinx/issues/9730; can be removed
when that is fixed, released, and available in our minimum supported Sphinx version.
"""
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 setup(app: Sphinx) -> Dict[str, str]:
"""Setup the Sphinx application."""
here = Path(__file__).parent.resolve()
Expand All @@ -273,6 +298,7 @@

app.connect("builder-inited", translator.setup_translators)
app.connect("builder-inited", update_config)
app.connect("html-page-context", _fix_canonical_url)
app.connect("html-page-context", edit_this_page.setup_edit_url)
app.connect("html-page-context", toctree.add_toctree_functions)
app.connect("html-page-context", update_and_remove_templates)
Expand Down
Loading