Skip to content

Add support for og:image:alt #15

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jul 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ These values are placed in the conf.py of your sphinx project.
* This is not required. Name of the site. This is displayed above the title.
* `ogp_image`
* This is not required. Link to image to show.
* `ogp_image_alt`
* 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.
* `ogp_type`
* 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.
* `ogp_custom_meta_tags`
Expand Down
11 changes: 10 additions & 1 deletion sphinxext/opengraph.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,15 @@ def get_tags(context: Dict[str, Any], doctree: nodes.document, config: Dict[str,
if image_url:
tags += make_tag("og:image", image_url)

# Add image alt text (either provided by config or from site_name)
ogp_image_alt = config["ogp_image_alt"]
if isinstance(ogp_image_alt, str):
tags += make_tag("og:image:alt", ogp_image_alt)
elif ogp_image_alt and site_name:
tags += make_tag("og:image:alt", site_name)
elif ogp_image_alt and htp.text:
tags += make_tag("og:image:alt", htp.text)

# custom tags
tags += '\n'.join(config['ogp_custom_meta_tags'])

Expand All @@ -199,6 +208,7 @@ def setup(app: Sphinx) -> Dict[str, Any]:
app.add_config_value("ogp_site_url", None, "html")
app.add_config_value("ogp_description_length", DEFAULT_DESCRIPTION_LENGTH, "html")
app.add_config_value("ogp_image", None, "html")
app.add_config_value("ogp_image_alt", True, "html")
app.add_config_value("ogp_type", "website", "html")
app.add_config_value("ogp_site_name", None, "html")
app.add_config_value("ogp_custom_meta_tags", [], "html")
Expand All @@ -209,4 +219,3 @@ def setup(app: Sphinx) -> Dict[str, Any]:
"parallel_read_safe": True,
"parallel_write_safe": True,
}

1 change: 1 addition & 0 deletions tests/roots/test-image/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@

html_theme = "basic"

ogp_site_name = "Example's Docs!"
ogp_site_url = "http://example.org/"
ogp_image = "http://example.org/image.png"
5 changes: 5 additions & 0 deletions tests/test_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ def test_image(og_meta_tags):
assert get_tag_content(og_meta_tags, "image") == "http://example.org/image.png"


@pytest.mark.sphinx("html", testroot="image")
def test_image_alt(og_meta_tags):
assert get_tag_content(og_meta_tags, "image:alt") == "Example's Docs!"


@pytest.mark.sphinx("html", testroot="type")
def test_type(og_meta_tags):
assert get_tag_content(og_meta_tags, "type") == "article"
Expand Down