Skip to content

Commit 0dc991c

Browse files
authored
Add option to use og:description as description (#71)
1 parent b3c9eb8 commit 0dc991c

File tree

5 files changed

+30
-3
lines changed

5 files changed

+30
-3
lines changed

docs/source/conf.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@
4444
# This pattern also affects html_static_path and html_extra_path.
4545
exclude_patterns = []
4646

47+
# Generate <meta name="description" content="..."> tags.
48+
enable_meta_description = True
4749

4850
# -- Options for HTML output -------------------------------------------------
4951

sphinxext/opengraph/__init__.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@
2828
}
2929

3030

31-
def make_tag(property: str, content: str) -> str:
31+
def make_tag(property: str, content: str, type_: str = "property") -> str:
3232
# Parse quotation, so they won't break html tags if smart quotes are disabled
3333
content = content.replace('"', "&quot;")
34-
return f'<meta property="{property}" content="{content}" />\n '
34+
return f'<meta {type_}="{property}" content="{content}" />\n '
3535

3636

3737
def get_tags(
@@ -104,6 +104,10 @@ def get_tags(
104104
# description tag
105105
if description:
106106
tags["og:description"] = description
107+
if config["enable_meta_description"]:
108+
config["ogp_custom_meta_tags"].append(
109+
make_tag("description", description, "name")
110+
)
107111

108112
# image tag
109113
# Get basic values from config
@@ -183,6 +187,7 @@ def setup(app: Sphinx) -> Dict[str, Any]:
183187
app.add_config_value("ogp_type", "website", "html")
184188
app.add_config_value("ogp_site_name", None, "html")
185189
app.add_config_value("ogp_custom_meta_tags", [], "html")
190+
app.add_config_value("enable_meta_description", False, "html")
186191

187192
app.connect("html-page-context", html_page_context)
188193

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
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/en/latest/"
9+
10+
enable_meta_description = True
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: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import pytest
22
from sphinx.application import Sphinx
33
import conftest
4-
import os
54

65

76
def get_tag(tags, tag_type):
@@ -26,6 +25,16 @@ def test_simple(og_meta_tags):
2625
)
2726

2827

28+
@pytest.mark.sphinx("html", testroot="meta-name-description")
29+
def test_meta_name_description(meta_tags):
30+
og_description = get_tag_content(meta_tags, "description")
31+
description = [tag for tag in meta_tags if tag.get("name") == "description"][0].get(
32+
"content", ""
33+
)
34+
35+
assert og_description == description
36+
37+
2938
@pytest.mark.sphinx("html", testroot="simple")
3039
def test_site_url(og_meta_tags):
3140
# Uses the same directory as simple, because it already contains url for a minimal config

0 commit comments

Comments
 (0)