Skip to content

Universal SEO skill for Claude Code. 26 sub-skills + 19 sub-agents covering technical SEO, E-E-A-T, schema, GEO/AEO, agent readiness (Lighthouse Agentic Browsing, WebMCP, llms.txt), backlinks, local SEO, e-commerce, international SEO, Google APIs, and PDF/Excel reporting. 9 optional extensions for live SEO data.

fix(parse_html): keep spaces around inline tags in headings and link text

Switch Case Studio8cbc8105b19e
diff --git a/scripts/parse_html.py b/scripts/parse_html.py
index 8c411c5..7848e8c 100755
--- a/scripts/parse_html.py
+++ b/scripts/parse_html.py
@@ -61,6 +61,17 @@ def _has_rel_token(tag, token: str) -> bool:
return any(isinstance(value, str) and value.lower() == token for value in rel or [])
+def _element_text(tag) -> str:
+ """Return a tag's visible text with each run of whitespace collapsed to one space.
+
+ ``get_text(strip=True)`` strips every text node before joining them, so the
+ spaces next to inline tags are lost (``for <em>every</em> dollar`` becomes
+ ``foreverydollar``). Joining the unstripped text and collapsing whitespace
+ keeps those spaces and also folds newlines in multi-line headings.
+ """
+ return " ".join(tag.get_text().split())
+
+
def _detect_lazy_method(img) -> str:
"""Return a coarse classification of the image's lazy-loading mechanism.
@@ -124,7 +135,7 @@ def parse_html(html: str, base_url: Optional[str] = None) -> dict:
# Title
title_tag = soup.find("title")
if title_tag:
- result["title"] = title_tag.get_text(strip=True)
+ result["title"] = _element_text(title_tag)
# Meta tags
for meta in soup.find_all("meta"):
@@ -166,7 +177,7 @@ def parse_html(html: str, base_url: Optional[str] = None) -> dict:
# Headings
for tag in ["h1", "h2", "h3"]:
for heading in soup.find_all(tag):
- text = heading.get_text(strip=True)
+ text = _element_text(heading)
if text:
result[tag].append(text)
# Flag suspiciously short or purely numeric headings (likely counters/stats)
@@ -210,7 +221,7 @@ def parse_html(html: str, base_url: Optional[str] = None) -> dict:
link_data = {
"href": full_url,
- "text": a.get_text(strip=True)[:100],
+ "text": _element_text(a)[:100],
"rel": a.get("rel", []),
}
diff --git a/tests/test_parse_html_inline_spacing.py b/tests/test_parse_html_inline_spacing.py
new file mode 100644
index 0000000..4b92a57
--- /dev/null
+++ b/tests/test_parse_html_inline_spacing.py
@@ -0,0 +1,59 @@
+"""
+Tests for whitespace around inline tags in scripts/parse_html.py.
+
+Background: headings, link text and the title were read with
+`get_text(strip=True)`. bs4 strips each text node before joining them, so the
+spaces next to inline tags such as <em>, <strong>, <a> and <b> disappeared
+("for <em>every</em> dollar" became "foreverydollar"), and newlines inside a
+multi-line heading were kept. Every consumer of the headings and anchor text
+(keyword checks, duplicate-heading detection, drift baselines) saw the glued
+text.
+"""
+import sys
+from pathlib import Path
+
+# Make scripts/ importable without requiring it to be a package
+REPO_ROOT = Path(__file__).resolve().parent.parent
+sys.path.insert(0, str(REPO_ROOT / "scripts"))
+
+from parse_html import parse_html # noqa: E402
+
+
+def test_h1_keeps_spaces_around_inline_emphasis():
+ html = "<h1>Free money calculators for <em>every</em> dollar question.</h1>"
+ result = parse_html(html)
+ assert result["h1"] == ["Free money calculators for every dollar question."]
+
+
+def test_h2_keeps_spaces_around_strong_and_link():
+ html = '<h2>Save <strong>more</strong>, spend <a href="/x">less</a></h2>'
+ result = parse_html(html)
+ assert result["h2"] == ["Save more, spend less"]
+
+
+def test_link_text_keeps_spaces_around_inline_tags():
+ html = '<a href="/tools/">Browse all <b>46</b> calculators</a>'
+ result = parse_html(html, base_url="https://example.com/")
+ assert [link["text"] for link in result["links"]["internal"]] == [
+ "Browse all 46 calculators"
+ ]
+
+
+def test_multiline_heading_collapses_whitespace():
+ html = "<h3>\n Multi-line\n heading </h3>"
+ result = parse_html(html)
+ assert result["h3"] == ["Multi-line heading"]
+
+
+def test_multiline_title_collapses_whitespace():
+ html = "<html><head><title>\n Acme\n Tools </title></head></html>"
+ result = parse_html(html)
+ assert result["title"] == "Acme Tools"
+
+
+def test_link_text_is_still_capped_at_100_characters():
+ html = '<a href="/long">' + "word " * 40 + "</a>"
+ result = parse_html(html, base_url="https://example.com/")
+ text = result["links"]["internal"][0]["text"]
+ assert len(text) == 100
+ assert text == ("word " * 20)[:100]