Skip to content

Commit 0978285

Browse files
committed
Respect that dirhtml builder does not have .html in canonical URLs
1 parent ce20cfc commit 0978285

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

sphinx_rtd_theme/layout.html

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,21 @@
5151

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

5757
{#- CANONICAL URL #}
58+
{# NB! pageurl is currently a non-documented template context variable! #}
59+
{# pageurl implementation: https://www.sphinx-doc.org/en/master/_modules/sphinx/builders/html.html #}
5860
{%- if pageurl %}
59-
<link rel="canonical" href="{{ pageurl|e }}" />
61+
{# pageurl implementation wrongly adds .html on the dirhtml builder #}
62+
{# Workaround for: https://github.com/sphinx-doc/sphinx/issues/9730 #}
63+
{# Once a fix is released in Sphinx, put an upper bound on the Sphinx version for the workaround #}
64+
{% if builder == 'dirhtml' %}
65+
<link rel="canonical" href="{{ pageurl|e|trim(".html") }}" />
66+
{% else %}
67+
<link rel="canonical" href="{{ pageurl|e }}" />
68+
{% endif %}
6069
{%- endif -%}
6170

6271
{#- JAVASCRIPTS #}

0 commit comments

Comments
 (0)