Keep word spacing and drop empty link targets in HTMLToMarkdown (#1347)

This commit is contained in:
Lukas
2026-10-07 08:58:51 -04:00
committed by GitHub
parent 647d80f95e
commit 341184ba46
2 changed files with 20 additions and 3 deletions
+8 -3
View File
@@ -194,12 +194,17 @@ class HTMLToMarkdown(Pipeline):
target, text = x if x.name else node, x.text
if text.strip():
# Leading whitespace separates this text from the previous word, keep it outside of the formatting
space = " " if text[0].isspace() and texts and not texts[-1][-1:].isspace() else ""
if target.name in ("b", "strong"):
text = f"**{text.strip()}** "
text = f"{space}**{text.strip()}** "
elif target.name in ("i", "em"):
text = f"*{text.strip()}* "
text = f"{space}*{text.strip()}* "
elif target.name == "a":
text = f"[{text.strip()}]({target.get('href')}) "
# Links without a target are added as plain text
href = target.get("href")
text = f"{space}[{text.strip()}]({href}) " if href else f"{space}{text.strip()} "
texts.append(text)
+12
View File
@@ -147,6 +147,18 @@ class TestTextractor(unittest.TestCase):
self.assertMarkdown("<p>This is a <em>test</em></p>", "This is a *test*")
self.assertMarkdown("<p>This is a <a href='link'>test</a>", "This is a [test](link)")
# Leading whitespace inside a formatting tag
self.assertMarkdown("<p>This is<b> a test</b></p>", "This is **a test**")
self.assertMarkdown("<p>This is<em> a test</em></p>", "This is *a test*")
self.assertMarkdown("<p>This is<a href='link'> a test</a></p>", "This is [a test](link)")
self.assertMarkdown("<p>This is a te<b>st</b></p>", "This is a te**st**")
self.assertMarkdown("<p>This is\n<b> a test</b></p>", "This is\n**a test**")
# Links without a target are plain text
self.assertMarkdown("<p>This is a <a name='anchor'>test</a></p>", "This is a test")
self.assertMarkdown("<p>This is<a name='anchor'> a test</a></p>", "This is a test")
self.assertMarkdown("<h2><a id='anchor'>This is a test</a></h2>", "## This is a test")
# Collapse to outer tag
self.assertMarkdown("<p>This is a <strong><em>test</em></strong></p>", "This is a **test**")
self.assertMarkdown("<p>This is a <em><strong>test</strong></em></p>", "This is a *test*")