Skip to content

Commit 285768e

Browse files
authored
Exclude raw nodes from description (#23)
1 parent 6deec4b commit 285768e

File tree

4 files changed

+29
-0
lines changed

4 files changed

+29
-0
lines changed

sphinxext/opengraph.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,9 @@ def dispatch_visit(self, node: nodes.Element) -> None:
9797
if node.astext() in self.known_titles:
9898
raise nodes.SkipNode
9999

100+
if isinstance(node, nodes.raw):
101+
raise nodes.SkipNode
102+
100103
# Only include leaf nodes in the description
101104
if len(node.children) == 0:
102105
text = node.astext().replace("\r", "").replace("\n", " ").strip()

tests/roots/test-skip-raw/conf.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
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+
ogp_description_length = 100

tests/roots/test-skip-raw/index.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
This text should be included.
2+
3+
.. raw:: html
4+
5+
<p> This text should be skipped. </p>
6+
7+
This text should also be included.

tests/test_options.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,3 +100,13 @@ def test_skip_comments(og_meta_tags):
100100
@pytest.mark.sphinx("html", testroot="custom-tags")
101101
def test_custom_tags(og_meta_tags):
102102
assert get_tag_content(og_meta_tags, "ignore_canonical") == "true"
103+
104+
105+
@pytest.mark.sphinx("html", testroot="skip-raw")
106+
def test_skip_raw(og_meta_tags):
107+
description = get_tag_content(og_meta_tags, "description")
108+
assert "<p>" not in description
109+
assert (
110+
description
111+
== "This text should be included. This text should also be included."
112+
)

0 commit comments

Comments
 (0)