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 7dfccd0
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions sphinx_rtd_theme/layout.html
Original file line number Diff line number Diff line change
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 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' %}
<link rel="canonical" href="{{ pageurl|e|trim(".html") }}" />
{% else %}
<link rel="canonical" href="{{ pageurl|e }}" />
{% endif %}
{%- endif -%}

{#- JAVASCRIPTS #}
Expand Down

0 comments on commit 7dfccd0

Please sign in to comment.