Skip to content

Commit 9f1a8c8

Browse files
authored
Fix first_image not resolving correctly when in subdirectories and ogp_image is defined. (#63)
1 parent ada5be9 commit 9f1a8c8

File tree

2 files changed

+7
-3
lines changed

2 files changed

+7
-3
lines changed

sphinxext/opengraph/__init__.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ def get_tags(
119119

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

122+
first_image = None
122123
if ogp_use_first_image:
123124
first_image = doctree.next_node(nodes.image)
124125
if (
@@ -127,19 +128,21 @@ def get_tags(
127128
):
128129
image_url = first_image["uri"]
129130
ogp_image_alt = first_image.get("alt", None)
131+
else:
132+
first_image = None
130133

131134
if image_url:
132135
# temporarily disable relative image paths with field lists
133136
if "og:image" not in fields:
134137
image_url_parsed = urlparse(image_url)
135138
if not image_url_parsed.scheme:
136139
# Relative image path detected, relative to the source. Make absolute.
137-
if config["ogp_image"]:
140+
if first_image:
141+
root = page_url
142+
else: # ogp_image is set
138143
# ogp_image is defined as being relative to the site root.
139144
# This workaround is to keep that functionality from breaking.
140145
root = config["ogp_site_url"]
141-
else:
142-
root = page_url
143146

144147
image_url = urljoin(root, image_url_parsed.path)
145148
tags["og:image"] = image_url

tests/roots/test-image-rel-paths/conf.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@
77

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

0 commit comments

Comments
 (0)