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

Use canonical URLs when ogp_site_url is not configured #56

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 9 additions & 17 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,17 @@ 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!"
)
ItayZiv marked this conversation as resolved.
Show resolved Hide resolved
)

# url tag
config["ogp_site_url"] = config["html_baseurl"]

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