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

Workaround for dirhtml builder with wrong canonical URLs #1407

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 21 additions & 4 deletions sphinx_rtd_theme/layout.html
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,29 @@

{#- CANONICAL URL (deprecated) #}
{%- if theme_canonical_url and not pageurl %}
<link rel="canonical" href="{{ theme_canonical_url }}{{ pagename }}.html"/>
<link rel="canonical" href="{{ theme_canonical_url }}{{ pagename }}{% if builder == 'dirhtml' %}/{% else %}.html{% endif %}"/>
{%- endif -%}

{#- CANONICAL URL #}
{%- if pageurl %}
<link rel="canonical" href="{{ pageurl|e }}" />
{#-
CANONICAL URL
NB! pageurl is currently a non-documented template context variable!
pageurl implementation: https://www.sphinx-doc.org/en/master/_modules/sphinx/builders/html.html
benjaoming marked this conversation as resolved.
Show resolved Hide resolved
benjaoming marked this conversation as resolved.
Show resolved Hide resolved
-#}
{%- if pageurl -%}
{#-
pageurl implementation wrongly: adds .html for the dirhtml builder's pageurl
Workaround for: https://github.com/sphinx-doc/sphinx/issues/9730
Once a fix is released in Sphinx, put an upper bound on the Sphinx version for the workaround
-#}
{%- if builder == 'dirhtml' and pageurl.endswith('index.html') %}
{#- This expression trims away index.html entirely #}
<link rel="canonical" href="{{ (pageurl|e)[:-10] }}" />
{%- elif builder == 'dirhtml' and pageurl.endswith('.html') %}
{#- This expression trims away .html and adds a / #}
<link rel="canonical" href="{{ (pageurl|e)[:-5] }}/" />
{%- else %}
<link rel="canonical" href="{{ pageurl|e }}" />
{%- endif -%}
{%- endif -%}

{#- JAVASCRIPTS #}
Expand Down