Skip to content

Commit

Permalink
Fix first_image not resolving correctly when in subdirectories and og…
Browse files Browse the repository at this point in the history
…p_image is defined. (#63)
  • Loading branch information
ItayZiv authored Mar 29, 2022
1 parent ada5be9 commit 9f1a8c8
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
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

0 comments on commit 9f1a8c8

Please sign in to comment.