File tree Expand file tree Collapse file tree 7 files changed +49
-4
lines changed
roots/test-image-rel-paths Expand file tree Collapse file tree 7 files changed +49
-4
lines changed Original file line number Diff line number Diff line change 11from typing import Any , Dict
22from urllib .parse import urljoin , urlparse , urlunparse
3- from pathlib import Path , PosixPath
3+ from pathlib import Path
44
55import docutils .nodes as nodes
66from sphinx .application import Sphinx
7- from sphinx .util import logger
87
98from .descriptionparser import get_description
109from .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)
Original file line number Diff line number Diff 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+
4557def pytest_configure (config ):
4658 config .addinivalue_line ("markers" , "sphinx" )
Original file line number Diff line number Diff line change 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
Original file line number Diff line number Diff line change 1+ .. image :: img/sample.jpg
2+ :alt: Test image alt text
Original file line number Diff line number Diff line change 1+ ========
2+ Sub Page
3+ ========
4+
5+ .. image :: ../img/sample.jpg
6+ :alt: Test image alt text
Original file line number Diff line number Diff 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" )
4153def test_image_alt (og_meta_tags ):
4254 assert get_tag_content (og_meta_tags , "image:alt" ) == "Example's Docs!"
You can’t perform that action at this time.
0 commit comments