Skip to content

Commit

Permalink
Add option to use og:description as description (#71)
Browse files Browse the repository at this point in the history
  • Loading branch information
hugovk authored Oct 18, 2022
1 parent b3c9eb8 commit 0dc991c
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 3 deletions.
2 changes: 2 additions & 0 deletions docs/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@
# This pattern also affects html_static_path and html_extra_path.
exclude_patterns = []

# Generate <meta name="description" content="..."> tags.
enable_meta_description = True

# -- Options for HTML output -------------------------------------------------

Expand Down
9 changes: 7 additions & 2 deletions sphinxext/opengraph/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@
}


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


def get_tags(
Expand Down Expand Up @@ -104,6 +104,10 @@ def get_tags(
# description tag
if description:
tags["og:description"] = description
if config["enable_meta_description"]:
config["ogp_custom_meta_tags"].append(
make_tag("description", description, "name")
)

# image tag
# Get basic values from config
Expand Down Expand Up @@ -183,6 +187,7 @@ def setup(app: Sphinx) -> Dict[str, Any]:
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.add_config_value("enable_meta_description", False, "html")

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

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

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

html_theme = "basic"

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

enable_meta_description = True
1 change: 1 addition & 0 deletions tests/roots/test-meta-name-description/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.
11 changes: 10 additions & 1 deletion tests/test_options.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import pytest
from sphinx.application import Sphinx
import conftest
import os


def get_tag(tags, tag_type):
Expand All @@ -26,6 +25,16 @@ def test_simple(og_meta_tags):
)


@pytest.mark.sphinx("html", testroot="meta-name-description")
def test_meta_name_description(meta_tags):
og_description = get_tag_content(meta_tags, "description")
description = [tag for tag in meta_tags if tag.get("name") == "description"][0].get(
"content", ""
)

assert og_description == description


@pytest.mark.sphinx("html", testroot="simple")
def test_site_url(og_meta_tags):
# Uses the same directory as simple, because it already contains url for a minimal config
Expand Down

0 comments on commit 0dc991c

Please sign in to comment.