Skip to content

Commit

Permalink
Add support for custom tags
Browse files Browse the repository at this point in the history
  • Loading branch information
TheTripleV committed Jul 10, 2020
1 parent 4ad83fb commit f10020b
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 0 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ These values are placed in the conf.py of your sphinx project.
* This is not required. Link to image to show.
* `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`
* This is not required. List of custom html snippets to insert.

## Example Config

Expand All @@ -45,4 +47,9 @@ ogp_site_url = "http://example.org/"
ogp_image = "http://example.org/image.png"
ogp_description_length = 300
ogp_type = "article"

ogp_custom_meta_tags = [
'<meta property="og:ignore_canonical" content="true" />',
]

```
4 changes: 4 additions & 0 deletions sphinxext/opengraph.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,9 @@ def get_tags(context: Dict[str, Any], doctree: nodes.document, config: Dict[str,
if image_url:
tags += make_tag("og:image", image_url)

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

return tags


Expand All @@ -198,6 +201,7 @@ def setup(app: Sphinx) -> Dict[str, Any]:
app.add_config_value("ogp_image", None, "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")

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

Expand Down
12 changes: 12 additions & 0 deletions tests/roots/test-custom-tags/conf.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
extensions = ["sphinxext.opengraph"]

master_doc = "index"
exclude_patterns = ["_build"]

html_theme = "basic"

ogp_site_url = "http://example.org/"

ogp_custom_meta_tags = [
'<meta property="og:ignore_canonical" content="true" />',
]
1 change: 1 addition & 0 deletions tests/roots/test-custom-tags/index.rst
Original file line number Diff line number Diff line change
@@ -0,0 +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.
5 changes: 5 additions & 0 deletions tests/test_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,8 @@ def test_nested_list_punctuation(og_meta_tags):
def test_skip_comments(og_meta_tags):
assert get_tag_content(og_meta_tags, "description") == "This is text."


@pytest.mark.sphinx("html", testroot="custom-tags")
def test_custom_tags(og_meta_tags):
assert get_tag_content(og_meta_tags, "ignore_canonical") == "true"

0 comments on commit f10020b

Please sign in to comment.