Skip to content

Commit

Permalink
Change to always use canonical URL when ogp_site_url is unconfigured
Browse files Browse the repository at this point in the history
  • Loading branch information
ItayZiv committed Feb 25, 2022
1 parent edc4662 commit 80279c4
Showing 1 changed file with 8 additions and 18 deletions.
26 changes: 8 additions & 18 deletions sphinxext/opengraph/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import docutils.nodes as nodes
from sphinx.application import Sphinx
from sphinx.errors import ConfigError

from .descriptionparser import get_description
from .titleparser import get_title
Expand Down Expand Up @@ -67,26 +68,15 @@ def get_tags(
# type tag
tags["og:type"] = config["ogp_type"]

if os.getenv("READTHEDOCS") and config["ogp_site_url"] is None:
# readthedocs uses html_baseurl for sphinx > 1.8
parse_result = urlparse(config["html_baseurl"])

# url tag
# Get the canonical URL if ogp_site_url is not configured
if config["ogp_site_url"] is None:
# we require that either ogp_site_url or html_baseurl be configured
if config["html_baseurl"] is None:
raise EnvironmentError("ReadTheDocs did not provide a valid canonical URL!")

# Grab root url from canonical url
config["ogp_site_url"] = urlunparse(
(
parse_result.scheme,
parse_result.netloc,
parse_result.path,
"",
"",
"",
)
)
raise ConfigError("A URL has not been configured and is required as per the OpenGraph Specification!")

config["ogp_site_url"] = config["html_baseurl"]

# url tag
# Get the URL of the specific page
if context["builder"] == "dirhtml":
page_url = urljoin(config["ogp_site_url"], context["pagename"] + "/")
Expand Down

0 comments on commit 80279c4

Please sign in to comment.