Skip to content

Commit

Permalink
Respect that dirhtml builder does not have .html in canonical URLs
Browse files Browse the repository at this point in the history
  • Loading branch information
benjaoming committed Jan 11, 2023
1 parent ce20cfc commit 0978285
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions sphinx_rtd_theme/layout.html
Expand Up @@ -51,12 +51,21 @@

{#- 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 #}
{# NB! pageurl is currently a non-documented template context variable! #}
{# pageurl implementation: https://www.sphinx-doc.org/en/master/_modules/sphinx/builders/html.html #}
{%- if pageurl %}
<link rel="canonical" href="{{ pageurl|e }}" />
{# pageurl implementation wrongly adds .html on the dirhtml builder #}
{# 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' %}
<link rel="canonical" href="{{ pageurl|e|trim(".html") }}" />
{% else %}
<link rel="canonical" href="{{ pageurl|e }}" />
{% endif %}
{%- endif -%}

{#- JAVASCRIPTS #}
Expand Down

0 comments on commit 0978285

Please sign in to comment.