Skip to content

Commit f10020b

Browse files
committed
Add support for custom tags
1 parent 4ad83fb commit f10020b

File tree

5 files changed

+29
-0
lines changed

5 files changed

+29
-0
lines changed

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ These values are placed in the conf.py of your sphinx project.
2828
* This is not required. Link to image to show.
2929
* `ogp_type`
3030
* 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.
31+
* `ogp_custom_meta_tags`
32+
* This is not required. List of custom html snippets to insert.
3133

3234
## Example Config
3335

@@ -45,4 +47,9 @@ ogp_site_url = "http://example.org/"
4547
ogp_image = "http://example.org/image.png"
4648
ogp_description_length = 300
4749
ogp_type = "article"
50+
51+
ogp_custom_meta_tags = [
52+
'<meta property="og:ignore_canonical" content="true" />',
53+
]
54+
4855
```

sphinxext/opengraph.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,9 @@ 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+
# custom tags
188+
tags += '\n'.join(config['ogp_custom_meta_tags'])
189+
187190
return tags
188191

189192

@@ -198,6 +201,7 @@ def setup(app: Sphinx) -> Dict[str, Any]:
198201
app.add_config_value("ogp_image", None, "html")
199202
app.add_config_value("ogp_type", "website", "html")
200203
app.add_config_value("ogp_site_name", None, "html")
204+
app.add_config_value("ogp_custom_meta_tags", [], "html")
201205

202206
app.connect('html-page-context', html_page_context)
203207

tests/roots/test-custom-tags/conf.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
extensions = ["sphinxext.opengraph"]
2+
3+
master_doc = "index"
4+
exclude_patterns = ["_build"]
5+
6+
html_theme = "basic"
7+
8+
ogp_site_url = "http://example.org/"
9+
10+
ogp_custom_meta_tags = [
11+
'<meta property="og:ignore_canonical" content="true" />',
12+
]
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
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.

tests/test_options.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,3 +86,8 @@ def test_nested_list_punctuation(og_meta_tags):
8686
def test_skip_comments(og_meta_tags):
8787
assert get_tag_content(og_meta_tags, "description") == "This is text."
8888

89+
90+
@pytest.mark.sphinx("html", testroot="custom-tags")
91+
def test_custom_tags(og_meta_tags):
92+
assert get_tag_content(og_meta_tags, "ignore_canonical") == "true"
93+

0 commit comments

Comments
 (0)