Skip to content

Fix first_image not resolving correctly when in subdirectories and ogp_image is defined. #63

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

Merged
merged 3 commits into from
Mar 29, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
9 changes: 6 additions & 3 deletions sphinxext/opengraph/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ def get_tags(

fields.pop("og:image:alt", None)

first_image = None
if ogp_use_first_image:
first_image = doctree.next_node(nodes.image)
if (
Expand All @@ -127,19 +128,21 @@ def get_tags(
):
image_url = first_image["uri"]
ogp_image_alt = first_image.get("alt", None)
else:
first_image = None

if image_url:
# temporarily disable relative image paths with field lists
if "og:image" not in fields:
image_url_parsed = urlparse(image_url)
if not image_url_parsed.scheme:
# Relative image path detected, relative to the source. Make absolute.
if config["ogp_image"]:
if first_image:
root = page_url
else: # ogp_image is set
# ogp_image is defined as being relative to the site root.
# This workaround is to keep that functionality from breaking.
root = config["ogp_site_url"]
else:
root = page_url

image_url = urljoin(root, image_url_parsed.path)
tags["og:image"] = image_url
Expand Down
1 change: 1 addition & 0 deletions tests/roots/test-image-rel-paths/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@

ogp_site_name = "Example's Docs!"
ogp_site_url = "http://example.org/en/latest/"
ogp_image = "_static/image33.png"
ogp_use_first_image = True