Skip to content

Commit ada5be9

Browse files
ItayZivTheTripleV
andauthored
Fix first_image not resolving correctly in subdirectories (#61)
Co-authored-by: TheTripleV <vasistavovveti@gmail.com>
1 parent d99815b commit ada5be9

File tree

29 files changed

+94
-51
lines changed

29 files changed

+94
-51
lines changed

sphinxext/opengraph/__init__.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -130,12 +130,19 @@ def get_tags(
130130

131131
if image_url:
132132
# temporarily disable relative image paths with field lists
133-
if image_url and "og:image" not in fields:
133+
if "og:image" not in fields:
134134
image_url_parsed = urlparse(image_url)
135135
if not image_url_parsed.scheme:
136-
# Relative image path detected. Make absolute.
137-
image_url = urljoin(config["ogp_site_url"], image_url_parsed.path)
138-
tags["og:image"] = image_url
136+
# Relative image path detected, relative to the source. Make absolute.
137+
if config["ogp_image"]:
138+
# ogp_image is defined as being relative to the site root.
139+
# This workaround is to keep that functionality from breaking.
140+
root = config["ogp_site_url"]
141+
else:
142+
root = page_url
143+
144+
image_url = urljoin(root, image_url_parsed.path)
145+
tags["og:image"] = image_url
139146

140147
# Add image alt text (either provided by config or from site_name)
141148
if isinstance(ogp_image_alt, str):

tests/roots/test-arbitrary-tags/conf.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@
55

66
html_theme = "basic"
77

8-
ogp_site_url = "http://example.org/"
8+
ogp_site_url = "http://example.org/en/latest/"
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
:og:video: http://example.org/video.mp4
1+
:og:video: http://example.org/en/latest/video.mp4
22
:og:video:type: video/mp4
33

44
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse at lorem ornare, fringilla massa nec, venenatis mi. Donec erat sapien, tincidunt nec rhoncus nec, scelerisque id diam. Orci varius natoque penatibus et magnis dis parturient mauris.

tests/roots/test-custom-tags/conf.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
html_theme = "basic"
77

8-
ogp_site_url = "http://example.org/"
8+
ogp_site_url = "http://example.org/en/latest/"
99

1010
ogp_custom_meta_tags = [
1111
'<meta property="og:ignore_canonical" content="true" />',

tests/roots/test-description-length/conf.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@
55

66
html_theme = "basic"
77

8-
ogp_site_url = "http://example.org/"
8+
ogp_site_url = "http://example.org/en/latest/"
99
ogp_description_length = 50

tests/roots/test-double-spacing/conf.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@
55

66
html_theme = "basic"
77

8-
ogp_site_url = "http://example.org/"
8+
ogp_site_url = "http://example.org/en/latest/"

tests/roots/test-first-image-no-image/conf.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
html_theme = "basic"
77

88
ogp_site_name = "Example's Docs!"
9-
ogp_site_url = "http://example.org/"
10-
ogp_image = "http://example.org/image33.png"
9+
ogp_site_url = "http://example.org/en/latest/"
10+
ogp_image = "http://example.org/en/latest/image33.png"
1111
ogp_image_alt = "TEST"
1212
ogp_use_first_image = True

tests/roots/test-first-image/conf.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
html_theme = "basic"
77

88
ogp_site_name = "Example's Docs!"
9-
ogp_site_url = "http://example.org/"
10-
ogp_image = "http://example.org/image33.png"
9+
ogp_site_url = "http://example.org/en/latest/"
10+
ogp_image = "http://example.org/en/latest/image33.png"
1111
ogp_image_alt = "TEST"
1212
ogp_use_first_image = True
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
.. image:: http://example.org/image2.png
1+
.. image:: http://example.org/en/latest/image2.png
22
:alt: Test image alt text

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@
66
html_theme = "basic"
77

88
ogp_site_name = "Example's Docs!"
9-
ogp_site_url = "http://example.org/"
9+
ogp_site_url = "http://example.org/en/latest/"
1010
ogp_use_first_image = True

tests/roots/test-image/conf.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@
66
html_theme = "basic"
77

88
ogp_site_name = "Example's Docs!"
9-
ogp_site_url = "http://example.org/"
10-
ogp_image = "http://example.org/image.png"
9+
ogp_site_url = "http://example.org/en/latest/"
10+
ogp_image = "http://example.org/en/latest/image.png"

tests/roots/test-list/conf.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@
55

66
html_theme = "basic"
77

8-
ogp_site_url = "http://example.org/"
8+
ogp_site_url = "http://example.org/en/latest/"
8.18 KB
Loading

tests/roots/test-local-image/conf.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
extensions = ["sphinxext.opengraph"]
2+
3+
master_doc = "index"
4+
exclude_patterns = ["_build"]
5+
6+
html_theme = "basic"
7+
8+
ogp_site_name = "Example's Docs!"
9+
ogp_site_url = "http://example.org/en/latest/"
10+
ogp_image = "_static/sample.jpg"

tests/roots/test-local-image/index.rst

Whitespace-only changes.

tests/roots/test-nested-lists/conf.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@
55

66
html_theme = "basic"
77

8-
ogp_site_url = "http://example.org/"
8+
ogp_site_url = "http://example.org/en/latest/"

tests/roots/test-overrides-complex/conf.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@
66
html_theme = "basic"
77

88
ogp_site_name = "Example's Docs!"
9-
ogp_site_url = "http://example.org/"
9+
ogp_site_url = "http://example.org/en/latest/"
1010
ogp_image_alt = "Example Alt Text"

tests/roots/test-overrides-simple/conf.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@
66
html_theme = "basic"
77

88
ogp_site_name = "Example's Docs!"
9-
ogp_site_url = "http://example.org/"
10-
ogp_image = "http://example.org/image.png"
9+
ogp_site_url = "http://example.org/en/latest/"
10+
ogp_image = "http://example.org/en/latest/image.png"
1111
ogp_type = "book"

tests/roots/test-overrides-simple/index.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
:og:description: Overridden description
22
:og:title: Overridden Title
33
:og:type: article
4-
:og:image: http://example.org/overridden-image.png
4+
:og:image: http://example.org/en/latest/overridden-image.png
55

66
Lorem Ipsum
77
===========

tests/roots/test-quotation-marks/conf.py

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

88
smartquotes = False
99

10-
ogp_site_url = "http://example.org/"
10+
ogp_site_url = "http://example.org/en/latest/"

tests/roots/test-simple/conf.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@
55

66
html_theme = "basic"
77

8-
ogp_site_url = "http://example.org/"
8+
ogp_site_url = "http://example.org/en/latest/"

tests/roots/test-sitename/conf.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@
55

66
html_theme = "basic"
77

8-
ogp_site_url = "http://example.org/"
8+
ogp_site_url = "http://example.org/en/latest/"
99
ogp_site_name = "Example's Docs!"

tests/roots/test-skip-admonitions/conf.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@
55

66
html_theme = "basic"
77

8-
ogp_site_url = "http://example.org/"
8+
ogp_site_url = "http://example.org/en/latest/"

tests/roots/test-skip-code-block/conf.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@
55

66
html_theme = "basic"
77

8-
ogp_site_url = "http://example.org/"
8+
ogp_site_url = "http://example.org/en/latest/"
99
ogp_description_length = 100

tests/roots/test-skip-comments/conf.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@
55

66
html_theme = "basic"
77

8-
ogp_site_url = "http://example.org/"
8+
ogp_site_url = "http://example.org/en/latest/"

tests/roots/test-skip-raw/conf.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@
55

66
html_theme = "basic"
77

8-
ogp_site_url = "http://example.org/"
8+
ogp_site_url = "http://example.org/en/latest/"
99
ogp_description_length = 100

tests/roots/test-skip-title/conf.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@
55

66
html_theme = "basic"
77

8-
ogp_site_url = "http://example.org/"
8+
ogp_site_url = "http://example.org/en/latest/"

tests/roots/test-type/conf.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@
55

66
html_theme = "basic"
77

8-
ogp_site_url = "http://example.org/"
8+
ogp_site_url = "http://example.org/en/latest/"
99
ogp_type = "article"

tests/test_options.py

Lines changed: 45 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -29,28 +29,30 @@ def test_simple(og_meta_tags):
2929
@pytest.mark.sphinx("html", testroot="simple")
3030
def test_site_url(og_meta_tags):
3131
# Uses the same directory as simple, because it already contains url for a minimal config
32-
assert get_tag_content(og_meta_tags, "url") == "http://example.org/index.html"
32+
assert (
33+
get_tag_content(og_meta_tags, "url")
34+
== "http://example.org/en/latest/index.html"
35+
)
3336

3437

3538
@pytest.mark.sphinx("dirhtml", testroot="simple")
3639
def test_dirhtml_url(og_meta_tags):
37-
assert get_tag_content(og_meta_tags, "url") == "http://example.org/index/"
40+
assert get_tag_content(og_meta_tags, "url") == "http://example.org/en/latest/index/"
3841

3942

4043
@pytest.mark.sphinx("html", testroot="image")
4144
def test_image(og_meta_tags):
42-
assert get_tag_content(og_meta_tags, "image") == "http://example.org/image.png"
43-
44-
45-
@pytest.mark.sphinx("html", testroot="image-rel-paths")
46-
def test_image_rel_paths(og_meta_tags, og_meta_tags_sub):
4745
assert (
4846
get_tag_content(og_meta_tags, "image")
49-
== "http://example.org/_images/sample.jpg"
47+
== "http://example.org/en/latest/image.png"
5048
)
49+
50+
51+
@pytest.mark.sphinx("html", testroot="local-image")
52+
def test_local_image(og_meta_tags):
5153
assert (
52-
get_tag_content(og_meta_tags_sub, "image")
53-
== "http://example.org/_images/sample.jpg"
54+
get_tag_content(og_meta_tags, "image")
55+
== "http://example.org/en/latest/_static/sample.jpg"
5456
)
5557

5658

@@ -76,16 +78,34 @@ def test_site_name(og_meta_tags):
7678

7779
@pytest.mark.sphinx("html", testroot="first-image")
7880
def test_first_image(og_meta_tags):
79-
assert get_tag_content(og_meta_tags, "image") == "http://example.org/image2.png"
81+
assert (
82+
get_tag_content(og_meta_tags, "image")
83+
== "http://example.org/en/latest/image2.png"
84+
)
8085
assert get_tag_content(og_meta_tags, "image:alt") == "Test image alt text"
8186

8287

8388
@pytest.mark.sphinx("html", testroot="first-image-no-image")
8489
def test_first_image_no_image(og_meta_tags):
85-
assert get_tag_content(og_meta_tags, "image") == "http://example.org/image33.png"
90+
assert (
91+
get_tag_content(og_meta_tags, "image")
92+
== "http://example.org/en/latest/image33.png"
93+
)
8694
assert get_tag_content(og_meta_tags, "image:alt") == "TEST"
8795

8896

97+
@pytest.mark.sphinx("html", testroot="image-rel-paths")
98+
def test_image_rel_paths(og_meta_tags, og_meta_tags_sub):
99+
assert (
100+
get_tag_content(og_meta_tags, "image")
101+
== "http://example.org/en/latest/_images/sample.jpg"
102+
)
103+
assert (
104+
get_tag_content(og_meta_tags_sub, "image")
105+
== "http://example.org/en/latest/_images/sample.jpg"
106+
)
107+
108+
89109
@pytest.mark.sphinx("html", testroot="skip-admonitions")
90110
def test_skip_admonitions(og_meta_tags):
91111
assert get_tag_content(og_meta_tags, "description") == "This is text."
@@ -172,7 +192,7 @@ def test_overrides_simple(og_meta_tags):
172192
assert get_tag_content(og_meta_tags, "type") == "article"
173193
assert (
174194
get_tag_content(og_meta_tags, "image")
175-
== "http://example.org/overridden-image.png"
195+
== "http://example.org/en/latest/overridden-image.png"
176196
)
177197
# Make sure alt text still works even when overriding the image
178198
assert get_tag_content(og_meta_tags, "image:alt") == "Example's Docs!"
@@ -181,37 +201,43 @@ def test_overrides_simple(og_meta_tags):
181201
@pytest.mark.sphinx("html", testroot="overrides-complex")
182202
def test_overrides_complex(og_meta_tags):
183203
assert len(get_tag_content(og_meta_tags, "description")) == 10
184-
assert get_tag_content(og_meta_tags, "image") == "http://example.org/img/sample.jpg"
204+
assert (
205+
get_tag_content(og_meta_tags, "image")
206+
== "http://example.org/en/latest/img/sample.jpg"
207+
)
185208
assert get_tag_content(og_meta_tags, "image:alt") == "Overridden Alt Text"
186209

187210

188211
@pytest.mark.sphinx("html", testroot="arbitrary-tags")
189212
def test_arbitrary_tags(og_meta_tags):
190-
assert get_tag_content(og_meta_tags, "video") == "http://example.org/video.mp4"
213+
assert (
214+
get_tag_content(og_meta_tags, "video")
215+
== "http://example.org/en/latest/video.mp4"
216+
)
191217
assert get_tag_content(og_meta_tags, "video:type") == "video/mp4"
192218

193219

194220
# use same as simple, as configuration is identical to overriden
195221
@pytest.mark.sphinx("html", testroot="simple")
196222
def test_rtd_override(app: Sphinx, monkeypatch):
197223
monkeypatch.setenv("READTHEDOCS", "True")
198-
app.config.html_baseurl = "https://failure.com"
224+
app.config.html_baseurl = "https://failure.com/en/latest/"
199225

200226
app.build()
201227
tags = conftest._og_meta_tags(app)
202228

203-
assert get_tag_content(tags, "url") == "http://example.org/index.html"
229+
assert get_tag_content(tags, "url") == "http://example.org/en/latest/index.html"
204230

205231

206232
@pytest.mark.sphinx("html", testroot="rtd-default")
207233
def test_rtd_valid(app: Sphinx, monkeypatch):
208234
monkeypatch.setenv("READTHEDOCS", "True")
209-
app.config.html_baseurl = "https://failure.com"
235+
app.config.html_baseurl = "https://failure.com/en/latest/"
210236

211237
app.build()
212238
tags = conftest._og_meta_tags(app)
213239

214-
assert get_tag_content(tags, "url") == "https://failure.com/index.html"
240+
assert get_tag_content(tags, "url") == "https://failure.com/en/latest/index.html"
215241

216242

217243
# use rtd-default, as we are not changing configuration, but RTD variables

0 commit comments

Comments
 (0)