Skip to content

Commit

Permalink
Sphinx: use html_baseurl for setting the canonical URL (#7540)
Browse files Browse the repository at this point in the history
  • Loading branch information
stsewd authored Jan 5, 2021
1 parent 64bba91 commit 96d1170
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
10 changes: 10 additions & 0 deletions docs/guides/canonical.rst
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,16 @@ Check your :guilabel:`Admin` > :guilabel:`Domains` page for the domains that we
Implementation
--------------

If you are using :doc:`Sphinx </intro/getting-started-with-sphinx>`,
Read the Docs will set the value of the html_baseurl_ setting (if isn't already set) to your canonical domain.

.. _html_baseurl: https://www.sphinx-doc.org/page/usage/configuration.html#confval-html_baseurl

If you are using :doc:`MkDocs </intro/getting-started-with-mkdocs>`,
you can use the site_url_ setting.

.. _site_url: https://www.mkdocs.org/user-guide/configuration/#site_url

If you look at the source code for documentation built after you set your canonical URL,
you should see a bit of HTML like this:

Expand Down
8 changes: 8 additions & 0 deletions readthedocs/doc_builder/templates/doc_builder/conf.py.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,14 @@ context = {
'commit': {% if project.repo_type == 'git' %}'{{ commit|slice:"8" }}'{% else %}'{{ commit }}'{% endif %},
}

# For sphinx >=1.8 we can use html_baseurl to set the canonical URL.
# https://www.sphinx-doc.org/en/master/usage/configuration.html#confval-html_baseurl
if version_info >= (1, 8):
if not globals().get('html_baseurl'):
html_baseurl = context['canonical_url']
context['canonical_url'] = None


{# Provide block for extending context data from child template #}
{% block extra_context %}{% endblock %}

Expand Down

0 comments on commit 96d1170

Please sign in to comment.