Skip to content

Commit 6698401

Browse files
Add support for og:image:alt (#15)
* Add support for og:image:alt * Include document's title as alt source * Update sphinxext/opengraph.py Co-authored-by: Vasista Vovveti <vasistavovveti@gmail.com> * Remove line break for consistency Co-authored-by: Vasista Vovveti <vasistavovveti@gmail.com>
1 parent 952f485 commit 6698401

File tree

4 files changed

+18
-1
lines changed

4 files changed

+18
-1
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ These values are placed in the conf.py of your sphinx project.
2626
* This is not required. Name of the site. This is displayed above the title.
2727
* `ogp_image`
2828
* This is not required. Link to image to show.
29+
* `ogp_image_alt`
30+
* This is not required. Alt text for image. Defaults to using `ogp_site_name` or the document's title as alt text, if available. Set to `False` if you want to turn off alt text completely.
2931
* `ogp_type`
3032
* This sets the ogp type attribute, for more information on the types available please take a look at https://ogp.me/#types. By default it is set to `website`, which should be fine for most use cases.
3133
* `ogp_custom_meta_tags`

sphinxext/opengraph.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,15 @@ def get_tags(context: Dict[str, Any], doctree: nodes.document, config: Dict[str,
184184
if image_url:
185185
tags += make_tag("og:image", image_url)
186186

187+
# Add image alt text (either provided by config or from site_name)
188+
ogp_image_alt = config["ogp_image_alt"]
189+
if isinstance(ogp_image_alt, str):
190+
tags += make_tag("og:image:alt", ogp_image_alt)
191+
elif ogp_image_alt and site_name:
192+
tags += make_tag("og:image:alt", site_name)
193+
elif ogp_image_alt and htp.text:
194+
tags += make_tag("og:image:alt", htp.text)
195+
187196
# custom tags
188197
tags += '\n'.join(config['ogp_custom_meta_tags'])
189198

@@ -199,6 +208,7 @@ def setup(app: Sphinx) -> Dict[str, Any]:
199208
app.add_config_value("ogp_site_url", None, "html")
200209
app.add_config_value("ogp_description_length", DEFAULT_DESCRIPTION_LENGTH, "html")
201210
app.add_config_value("ogp_image", None, "html")
211+
app.add_config_value("ogp_image_alt", True, "html")
202212
app.add_config_value("ogp_type", "website", "html")
203213
app.add_config_value("ogp_site_name", None, "html")
204214
app.add_config_value("ogp_custom_meta_tags", [], "html")
@@ -209,4 +219,3 @@ def setup(app: Sphinx) -> Dict[str, Any]:
209219
"parallel_read_safe": True,
210220
"parallel_write_safe": True,
211221
}
212-

tests/roots/test-image/conf.py

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

66
html_theme = "basic"
77

8+
ogp_site_name = "Example's Docs!"
89
ogp_site_url = "http://example.org/"
910
ogp_image = "http://example.org/image.png"

tests/test_options.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@ def test_image(og_meta_tags):
3131
assert get_tag_content(og_meta_tags, "image") == "http://example.org/image.png"
3232

3333

34+
@pytest.mark.sphinx("html", testroot="image")
35+
def test_image_alt(og_meta_tags):
36+
assert get_tag_content(og_meta_tags, "image:alt") == "Example's Docs!"
37+
38+
3439
@pytest.mark.sphinx("html", testroot="type")
3540
def test_type(og_meta_tags):
3641
assert get_tag_content(og_meta_tags, "type") == "article"

0 commit comments

Comments
 (0)