Skip to content
This repository was archived by the owner on Apr 8, 2025. It is now read-only.

Commit 02f7750

Browse files
committed
Canonical URL: don't set it if is empty
Fix #82 Ref #83 We need to release the extension first, make sure all builds are using that version and then release readthedocs/readthedocs.org#7540
1 parent e768510 commit 02f7750

File tree

4 files changed

+51
-16
lines changed

4 files changed

+51
-16
lines changed

readthedocs_ext/_templates/readthedocs-insert.html.tmpl

+15-13
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,23 @@
11

22
<!-- RTD Extra Head -->
33

4-
{%- if pagename == "index" %}
5-
{%- set canonical_page = "" %}
6-
{%- elif pagename.endswith("/index") %}
7-
{%- set canonical_page = pagename[:-("/index"|length)] + "/" %}
8-
{%- else %}
9-
{%- set ending = "/" if builder == "readthedocsdirhtml" else ".html" %}
10-
{%- set canonical_page = pagename + ending %}
4+
{%- if canonical_url %}
5+
{%- if pagename == "index" %}
6+
{%- set canonical_page = "" %}
7+
{%- elif pagename.endswith("/index") %}
8+
{%- set canonical_page = pagename[:-("/index"|length)] + "/" %}
9+
{%- else %}
10+
{%- set ending = "/" if builder == "readthedocsdirhtml" else ".html" %}
11+
{%- set canonical_page = pagename + ending %}
12+
{%- endif %}
13+
14+
<!--
15+
Always link to the latest version, as canonical.
16+
http://docs.readthedocs.org/en/latest/canonical.html
17+
-->
18+
<link rel="canonical" href="{{ canonical_url|e }}{{ canonical_page }}" />
1119
{%- endif %}
1220

13-
<!--
14-
Always link to the latest version, as canonical.
15-
http://docs.readthedocs.org/en/latest/canonical.html
16-
-->
17-
<link rel="canonical" href="{{ canonical_url }}{{ canonical_page }}" />
18-
1921
<link rel="stylesheet" href="{{ rtd_css_url }}" type="text/css" />
2022

2123
<script type="application/json" id="READTHEDOCS_DATA">{{ rtd_data | tojson }}</script>

tests/pyexample-json/conf.py

+1
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,4 @@
2222
html_theme = 'alabaster'
2323
html_static_path = ['_static']
2424
htmlhelp_basename = 'pyexampledoc'
25+
html_baseurl = 'https://example.com/'

tests/pyexample/conf.py

+1
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,5 @@
2929
'user_analytics_code': '',
3030
'global_analytics_code': "malic''ious",
3131
'commit': 'deadd00d',
32+
'canonical_url': 'https://example.com/"',
3233
}

tests/test_integration.py

+34-3
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,24 @@
11
import unittest
22

3-
from .util import sphinx_build, build_output
3+
import pytest
4+
from sphinx import version_info
5+
6+
from .util import build_output, sphinx_build
47

58

69
class LanguageIntegrationTests(unittest.TestCase):
710

8-
def _run_test(self, test_dir, test_file, test_string, builder='html'):
11+
def _run_test(self, test_dir, test_file, test_string, builder='html', assert_in=True):
912
with build_output(test_dir, test_file, builder) as data:
1013
if not isinstance(test_string, list):
1114
test_strings = [test_string]
1215
else:
1316
test_strings = test_string
1417
for string in test_strings:
15-
self.assertIn(string, data)
18+
if assert_in:
19+
self.assertIn(string, data)
20+
else:
21+
self.assertNotIn(string, data)
1622

1723

1824
class IntegrationTests(LanguageIntegrationTests):
@@ -90,3 +96,28 @@ def test_escape_js_vars(self):
9096
with build_output('pyexample', '_build/html/index.html', builder='html') as data:
9197
self.assertNotIn("malic''ious", data)
9298
self.assertIn('malic\\u0027\\u0027ious', data)
99+
100+
def test_escape_canonical_url(self):
101+
self._run_test(
102+
'pyexample',
103+
'_build/html/index.html',
104+
'<link rel="canonical" href="https://example.com/&#34;" />',
105+
builder='html',
106+
)
107+
108+
@pytest.mark.skipif(version_info < (1, 8), reason='Requires sphinx>=1.8')
109+
def test_canonical_url(self):
110+
self._run_test(
111+
'pyexample-json',
112+
'_build/html/index.html',
113+
'Always link to the latest version, as canonical.',
114+
builder='html',
115+
assert_in=False,
116+
)
117+
118+
self._run_test(
119+
'pyexample-json',
120+
'_build/html/index.html',
121+
'<link rel="canonical" href="https://example.com/index.html" />',
122+
builder='html',
123+
)

0 commit comments

Comments
 (0)