Skip to content

Commit 96d1170

Browse files
authored
Sphinx: use html_baseurl for setting the canonical URL (#7540)
Fixes readthedocs/readthedocs-sphinx-ext#83
1 parent 64bba91 commit 96d1170

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

docs/guides/canonical.rst

+10
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,16 @@ Check your :guilabel:`Admin` > :guilabel:`Domains` page for the domains that we
3131
Implementation
3232
--------------
3333

34+
If you are using :doc:`Sphinx </intro/getting-started-with-sphinx>`,
35+
Read the Docs will set the value of the html_baseurl_ setting (if isn't already set) to your canonical domain.
36+
37+
.. _html_baseurl: https://www.sphinx-doc.org/page/usage/configuration.html#confval-html_baseurl
38+
39+
If you are using :doc:`MkDocs </intro/getting-started-with-mkdocs>`,
40+
you can use the site_url_ setting.
41+
42+
.. _site_url: https://www.mkdocs.org/user-guide/configuration/#site_url
43+
3444
If you look at the source code for documentation built after you set your canonical URL,
3545
you should see a bit of HTML like this:
3646

readthedocs/doc_builder/templates/doc_builder/conf.py.tmpl

+8
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,14 @@ context = {
120120
'commit': {% if project.repo_type == 'git' %}'{{ commit|slice:"8" }}'{% else %}'{{ commit }}'{% endif %},
121121
}
122122

123+
# For sphinx >=1.8 we can use html_baseurl to set the canonical URL.
124+
# https://www.sphinx-doc.org/en/master/usage/configuration.html#confval-html_baseurl
125+
if version_info >= (1, 8):
126+
if not globals().get('html_baseurl'):
127+
html_baseurl = context['canonical_url']
128+
context['canonical_url'] = None
129+
130+
123131
{# Provide block for extending context data from child template #}
124132
{% block extra_context %}{% endblock %}
125133

0 commit comments

Comments
 (0)