Skip to content

Commit

Permalink
FIX: Social media twitter card tags (#101)
Browse files Browse the repository at this point in the history
  • Loading branch information
choldgraf authored Apr 13, 2023
1 parent 597ee06 commit f510aea
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 17 deletions.
5 changes: 3 additions & 2 deletions noxfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
@nox.session
def docs(session):
"""Build the documentation. Use `-- live` to build with a live server."""
session.install("-e", ".")
session.install("-r", "docs/requirements.txt")
session.install("-e", ".")
if "live" in session.posargs:
session.install("ipython")
session.install("sphinx-autobuild")
Expand All @@ -34,5 +34,6 @@ def docs(session):
@nox.session
def test(session):
"""Run the test suite."""
session.install(".")
session.install("-e", ".")
session.install("-r", "dev-requirements.txt")
session.run(*(["pytest"] + session.posargs))
15 changes: 7 additions & 8 deletions sphinxext/opengraph/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,6 @@ def get_tags(
config_social = DEFAULT_SOCIAL_CONFIG.copy()
social_card_user_options = app.config.ogp_social_cards or {}
config_social.update(social_card_user_options)

# This will only be False if the user explicitly sets it
if (
not (image_url or ogp_use_first_image)
and config_social.get("enable") is not False
Expand Down Expand Up @@ -185,6 +183,13 @@ def get_tags(
image_path = str(image_path).replace(os.path.sep, "/").strip("/")
image_url = f"{url}/{image_path}"

# If the social card objects have been added we add special metadata for them
# These are the dimensions *in pixels* of the card
# They were chosen by looking at the image pixel dimensions on disk
tags["og:image:width"] = "1146"
tags["og:image:height"] = "600"
meta_tags["twitter:card"] = "summary_large_image"

fields.pop("og:image:alt", None)

first_image = None
Expand Down Expand Up @@ -224,12 +229,6 @@ def get_tags(
elif ogp_image_alt is None and title:
tags["og:image:alt"] = title

if "ogp_social_card_tags" in context:
# Add social media metadata if we've activated preview cards
tags["og:image:width"] = meta["width"]
tags["og:image:height"] = meta["height"]
meta_tags["twitter:card"] = "summary_large_image"

# arbitrary tags and overrides
tags.update({k: v for k, v in fields.items() if k.startswith("og:")})

Expand Down
18 changes: 11 additions & 7 deletions tests/test_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
import conftest


def get_tag(tags, tag_type):
return [tag for tag in tags if tag.get("property") == f"og:{tag_type}"][0]
def get_tag(tags, tag_type, kind="property", prefix="og"):
return [tag for tag in tags if tag.get(kind) == f"{prefix}:{tag_type}"][0]


def get_tag_content(tags, tag_type):
def get_tag_content(tags, tag_type, kind="property", prefix="og"):
# Gets the content of a specific ogp tag
return get_tag(tags, tag_type).get("content", "")
return get_tag(tags, tag_type, kind, prefix).get("content", "")


def get_meta_description(tags):
Expand Down Expand Up @@ -99,17 +99,21 @@ def test_image_alt(og_meta_tags):


@pytest.mark.sphinx("html", testroot="simple")
def test_image_social_cards(og_meta_tags):
def test_image_social_cards(meta_tags):
"""Social cards should automatically be added if no og:image is given."""
# Asserting `in` instead of `==` because of the hash that is generated
assert (
"http://example.org/en/latest/_images/social_previews/summary_index"
in get_tag_content(og_meta_tags, "image")
in get_tag_content(meta_tags, "image")
)
# Image alt text should be taken from page content.
assert (
"Lorem ipsum dolor sit amet, consectetur adipiscing elit."
in get_tag_content(og_meta_tags, "image:alt")
in get_tag_content(meta_tags, "image:alt")
)
# Make sure the extra tags are in the HTML
assert "summary_large_image" in get_tag_content(
meta_tags, "card", kind="name", prefix="twitter"
)


Expand Down

0 comments on commit f510aea

Please sign in to comment.