Skip to content

Commit 98c36cb

Browse files
committed
Support READTHEDOCS_CANONICAL_URL
1 parent b9645d5 commit 98c36cb

File tree

1 file changed

+11
-7
lines changed

1 file changed

+11
-7
lines changed

sphinxext/opengraph/__init__.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from typing import Any, Dict
2-
from urllib.parse import urljoin, urlparse, urlunparse
2+
from urllib.parse import urljoin, urlparse, urlsplit, urlunparse
33
from pathlib import Path
44

55
import docutils.nodes as nodes
@@ -86,12 +86,16 @@ def get_tags(
8686
# type tag
8787
tags["og:type"] = config["ogp_type"]
8888

89-
if os.getenv("READTHEDOCS") and not config["ogp_site_url"]:
90-
# readthedocs uses html_baseurl for sphinx > 1.8
91-
parse_result = urlparse(config["html_baseurl"])
92-
93-
if config["html_baseurl"] is None:
94-
raise OSError("ReadTheDocs did not provide a valid canonical URL!")
89+
if not config["ogp_site_url"] and os.getenv("READTHEDOCS"):
90+
if config["html_baseurl"] is not None:
91+
# readthedocs uses ``html_baseurl`` for Sphinx > 1.8
92+
parse_result = urlsplit(config["html_baseurl"])
93+
else:
94+
# readthedocs addons no longer configures ``html_baseurl``
95+
if rtd_canonical_url := os.getenv("READTHEDOCS_CANONICAL_URL"):
96+
parse_result = urlsplit(rtd_canonical_url)
97+
else:
98+
raise OSError("ReadTheDocs did not provide a valid canonical URL!")
9599

96100
# Grab root url from canonical url
97101
config["ogp_site_url"] = urlunparse(

0 commit comments

Comments
 (0)