Skip to content

Commit

Permalink
Canonical URL: don't set it if is empty
Browse files Browse the repository at this point in the history
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
  • Loading branch information
stsewd committed Oct 6, 2020
1 parent e768510 commit 02f7750
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 16 deletions.
28 changes: 15 additions & 13 deletions readthedocs_ext/_templates/readthedocs-insert.html.tmpl
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@

<!-- RTD Extra Head -->

{%- if pagename == "index" %}
{%- set canonical_page = "" %}
{%- elif pagename.endswith("/index") %}
{%- set canonical_page = pagename[:-("/index"|length)] + "/" %}
{%- else %}
{%- set ending = "/" if builder == "readthedocsdirhtml" else ".html" %}
{%- set canonical_page = pagename + ending %}
{%- if canonical_url %}
{%- if pagename == "index" %}
{%- set canonical_page = "" %}
{%- elif pagename.endswith("/index") %}
{%- set canonical_page = pagename[:-("/index"|length)] + "/" %}
{%- else %}
{%- set ending = "/" if builder == "readthedocsdirhtml" else ".html" %}
{%- set canonical_page = pagename + ending %}
{%- endif %}

<!--
Always link to the latest version, as canonical.
http://docs.readthedocs.org/en/latest/canonical.html
-->
<link rel="canonical" href="{{ canonical_url|e }}{{ canonical_page }}" />
{%- endif %}

<!--
Always link to the latest version, as canonical.
http://docs.readthedocs.org/en/latest/canonical.html
-->
<link rel="canonical" href="{{ canonical_url }}{{ canonical_page }}" />

<link rel="stylesheet" href="{{ rtd_css_url }}" type="text/css" />

<script type="application/json" id="READTHEDOCS_DATA">{{ rtd_data | tojson }}</script>
Expand Down
1 change: 1 addition & 0 deletions tests/pyexample-json/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@
html_theme = 'alabaster'
html_static_path = ['_static']
htmlhelp_basename = 'pyexampledoc'
html_baseurl = 'https://example.com/'
1 change: 1 addition & 0 deletions tests/pyexample/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,5 @@
'user_analytics_code': '',
'global_analytics_code': "malic''ious",
'commit': 'deadd00d',
'canonical_url': 'https://example.com/"',
}
37 changes: 34 additions & 3 deletions tests/test_integration.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
import unittest

from .util import sphinx_build, build_output
import pytest
from sphinx import version_info

from .util import build_output, sphinx_build


class LanguageIntegrationTests(unittest.TestCase):

def _run_test(self, test_dir, test_file, test_string, builder='html'):
def _run_test(self, test_dir, test_file, test_string, builder='html', assert_in=True):
with build_output(test_dir, test_file, builder) as data:
if not isinstance(test_string, list):
test_strings = [test_string]
else:
test_strings = test_string
for string in test_strings:
self.assertIn(string, data)
if assert_in:
self.assertIn(string, data)
else:
self.assertNotIn(string, data)


class IntegrationTests(LanguageIntegrationTests):
Expand Down Expand Up @@ -90,3 +96,28 @@ def test_escape_js_vars(self):
with build_output('pyexample', '_build/html/index.html', builder='html') as data:
self.assertNotIn("malic''ious", data)
self.assertIn('malic\\u0027\\u0027ious', data)

def test_escape_canonical_url(self):
self._run_test(
'pyexample',
'_build/html/index.html',
'<link rel="canonical" href="https://example.com/&#34;" />',
builder='html',
)

@pytest.mark.skipif(version_info < (1, 8), reason='Requires sphinx>=1.8')
def test_canonical_url(self):
self._run_test(
'pyexample-json',
'_build/html/index.html',
'Always link to the latest version, as canonical.',
builder='html',
assert_in=False,
)

self._run_test(
'pyexample-json',
'_build/html/index.html',
'<link rel="canonical" href="https://example.com/index.html" />',
builder='html',
)

0 comments on commit 02f7750

Please sign in to comment.