mirror of
https://github.com/neuml/txtai.git
synced 2026-10-11 21:39:55 +00:00
Keep word spacing and drop empty link targets in HTMLToMarkdown (#1347)
This commit is contained in:
@@ -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)
|
||||
|
||||
|
||||
@@ -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*")
|
||||
|
||||
Reference in New Issue
Block a user