Skip to content

Commit f510aea

Browse files
authored
FIX: Social media twitter card tags (#101)
1 parent 597ee06 commit f510aea

File tree

3 files changed

+21
-17
lines changed

3 files changed

+21
-17
lines changed

noxfile.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@
1919
@nox.session
2020
def docs(session):
2121
"""Build the documentation. Use `-- live` to build with a live server."""
22-
session.install("-e", ".")
2322
session.install("-r", "docs/requirements.txt")
23+
session.install("-e", ".")
2424
if "live" in session.posargs:
2525
session.install("ipython")
2626
session.install("sphinx-autobuild")
@@ -34,5 +34,6 @@ def docs(session):
3434
@nox.session
3535
def test(session):
3636
"""Run the test suite."""
37-
session.install(".")
37+
session.install("-e", ".")
38+
session.install("-r", "dev-requirements.txt")
3839
session.run(*(["pytest"] + session.posargs))

sphinxext/opengraph/__init__.py

+7-8
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,6 @@ def get_tags(
136136
config_social = DEFAULT_SOCIAL_CONFIG.copy()
137137
social_card_user_options = app.config.ogp_social_cards or {}
138138
config_social.update(social_card_user_options)
139-
140-
# This will only be False if the user explicitly sets it
141139
if (
142140
not (image_url or ogp_use_first_image)
143141
and config_social.get("enable") is not False
@@ -185,6 +183,13 @@ def get_tags(
185183
image_path = str(image_path).replace(os.path.sep, "/").strip("/")
186184
image_url = f"{url}/{image_path}"
187185

186+
# If the social card objects have been added we add special metadata for them
187+
# These are the dimensions *in pixels* of the card
188+
# They were chosen by looking at the image pixel dimensions on disk
189+
tags["og:image:width"] = "1146"
190+
tags["og:image:height"] = "600"
191+
meta_tags["twitter:card"] = "summary_large_image"
192+
188193
fields.pop("og:image:alt", None)
189194

190195
first_image = None
@@ -224,12 +229,6 @@ def get_tags(
224229
elif ogp_image_alt is None and title:
225230
tags["og:image:alt"] = title
226231

227-
if "ogp_social_card_tags" in context:
228-
# Add social media metadata if we've activated preview cards
229-
tags["og:image:width"] = meta["width"]
230-
tags["og:image:height"] = meta["height"]
231-
meta_tags["twitter:card"] = "summary_large_image"
232-
233232
# arbitrary tags and overrides
234233
tags.update({k: v for k, v in fields.items() if k.startswith("og:")})
235234

tests/test_options.py

+11-7
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33
import conftest
44

55

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

99

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

1414

1515
def get_meta_description(tags):
@@ -99,17 +99,21 @@ def test_image_alt(og_meta_tags):
9999

100100

101101
@pytest.mark.sphinx("html", testroot="simple")
102-
def test_image_social_cards(og_meta_tags):
102+
def test_image_social_cards(meta_tags):
103103
"""Social cards should automatically be added if no og:image is given."""
104104
# Asserting `in` instead of `==` because of the hash that is generated
105105
assert (
106106
"http://example.org/en/latest/_images/social_previews/summary_index"
107-
in get_tag_content(og_meta_tags, "image")
107+
in get_tag_content(meta_tags, "image")
108108
)
109109
# Image alt text should be taken from page content.
110110
assert (
111111
"Lorem ipsum dolor sit amet, consectetur adipiscing elit."
112-
in get_tag_content(og_meta_tags, "image:alt")
112+
in get_tag_content(meta_tags, "image:alt")
113+
)
114+
# Make sure the extra tags are in the HTML
115+
assert "summary_large_image" in get_tag_content(
116+
meta_tags, "card", kind="name", prefix="twitter"
113117
)
114118

115119

0 commit comments

Comments
 (0)