Skip to content

Commit 6980a48

Browse files
committed
Convert relative paths to absolute in og:image.
Always using absolute/full URLs for og:image as long as `config["ogp_site_url"]` is set. Fixes #43
1 parent 16a291d commit 6980a48

File tree

7 files changed

+49
-4
lines changed

7 files changed

+49
-4
lines changed

sphinxext/opengraph/__init__.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
from typing import Any, Dict
22
from urllib.parse import urljoin, urlparse, urlunparse
3-
from pathlib import Path, PosixPath
3+
from pathlib import Path
44

55
import docutils.nodes as nodes
66
from sphinx.application import Sphinx
7-
from sphinx.util import logger
87

98
from .descriptionparser import get_description
109
from .titleparser import get_title
@@ -111,6 +110,10 @@ def get_tags(
111110
ogp_image_alt = first_image.get("alt", None)
112111

113112
if image_url:
113+
image_url_parsed = urlparse(image_url)
114+
if not image_url_parsed.scheme:
115+
# Relative image path detected. Make absolute.
116+
image_url = urljoin(config["ogp_site_url"], image_url_parsed.path)
114117
tags += make_tag("og:image", image_url)
115118

116119
# Add image alt text (either provided by config or from site_name)

tests/conftest.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,11 @@ def content(app):
1919
yield app
2020

2121

22-
def _meta_tags(content):
23-
c = (content.outdir / "index.html").read_text()
22+
def _meta_tags(content, subdir=None):
23+
if subdir is None:
24+
c = (content.outdir / "index.html").read_text()
25+
else:
26+
c = (content.outdir / subdir / "index.html").read_text()
2427
return BeautifulSoup(c, "html.parser").find_all("meta")
2528

2629

@@ -42,5 +45,14 @@ def og_meta_tags(content):
4245
]
4346

4447

48+
@pytest.fixture()
49+
def og_meta_tags_sub(content):
50+
return [
51+
tag
52+
for tag in _meta_tags(content, "sub")
53+
if tag.get("property", "").startswith("og:")
54+
]
55+
56+
4557
def pytest_configure(config):
4658
config.addinivalue_line("markers", "sphinx")
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/"
10+
ogp_use_first_image = True
8.18 KB
Loading
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.. image:: img/sample.jpg
2+
:alt: Test image alt text
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
========
2+
Sub Page
3+
========
4+
5+
.. image:: ../img/sample.jpg
6+
:alt: Test image alt text

tests/test_options.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,18 @@ def test_image(og_meta_tags):
3737
assert get_tag_content(og_meta_tags, "image") == "http://example.org/image.png"
3838

3939

40+
@pytest.mark.sphinx("html", testroot="image-rel-paths")
41+
def test_image_rel_paths(og_meta_tags, og_meta_tags_sub):
42+
assert (
43+
get_tag_content(og_meta_tags, "image")
44+
== "http://example.org/_images/sample.jpg"
45+
)
46+
assert (
47+
get_tag_content(og_meta_tags_sub, "image")
48+
== "http://example.org/_images/sample.jpg"
49+
)
50+
51+
4052
@pytest.mark.sphinx("html", testroot="image")
4153
def test_image_alt(og_meta_tags):
4254
assert get_tag_content(og_meta_tags, "image:alt") == "Example's Docs!"

0 commit comments

Comments
 (0)