Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Canonical URL: don't set it if is empty #93

Merged
merged 1 commit into from
Oct 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 %}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we want to check for and not html_baseurl? Seems that would output 2x canonical URL still.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We are doing that check in readthedocs/readthedocs.org#7540.
We could do the check here again, but seems redundant

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we could also do all the checks here, but by doing it in .org we can set the value in html_baseurl and rely on sphinx itself to set the canonical url instead of us doing it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Gotcha, makes sense.

{%- 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',
)