Skip to content

Commit

Permalink
Prevent creation of multiple tags with the same property
Browse files Browse the repository at this point in the history
  • Loading branch information
ItayZiv committed Jan 27, 2022
1 parent 13fb48a commit 0f3e4a8
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 2 deletions.
9 changes: 7 additions & 2 deletions sphinxext/opengraph/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,16 @@
"heif": "image/heif",
"tiff": "image/tiff",
}
TAG_LIST = []


def make_tag(property: str, content: str) -> str:
# Parse quotation, so they won't break html tags if smart quotes are disabled
content = content.replace('"', """)
return f'<meta property="{property}" content="{content}" />\n '
if property not in TAG_LIST:
TAG_LIST.append(property)
content = content.replace('"', "&quot;")
return f'<meta property="{property}" content="{content}" />\n '
return ""


def make_arbitrary_tags(fields: Dict[str, Any]) -> str:
Expand All @@ -49,6 +53,7 @@ def get_tags(
doctree: nodes.document,
config: Dict[str, Any],
) -> str:
TAG_LIST.clear()
# Get field lists for per-poge overrides
fields = context["meta"]
if fields is None:
Expand Down
1 change: 1 addition & 0 deletions tests/roots/test-arbitrary-tags/index.rst
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
:og:video: http://example.org/video.mp4
:og:video:type: video/mp4
:og:url: http://example.com/

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse at lorem ornare, fringilla massa nec, venenatis mi. Donec erat sapien, tincidunt nec rhoncus nec, scelerisque id diam. Orci varius natoque penatibus et magnis dis parturient mauris.
2 changes: 2 additions & 0 deletions tests/test_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,8 @@ def test_overrides_complex(og_meta_tags):
def test_arbitrary_tags(og_meta_tags):
assert get_tag_content(og_meta_tags, "video") == "http://example.org/video.mp4"
assert get_tag_content(og_meta_tags, "video:type") == "video/mp4"
assert get_tag_content(og_meta_tags, "url") == "http://example.org/index.html"
assert len([tag for tag in og_meta_tags if tag.get("property") == "og:url"]) == 1


# use same as simple, as configuration is identical to overriden
Expand Down

0 comments on commit 0f3e4a8

Please sign in to comment.