Skip to content

armgit / txtai

Public Imported from GitHub
0

💡 All-in-one AI framework for semantic search, LLM orchestration and language model workflows

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

Lukas341184ba462b
diff --git a/src/python/txtai/pipeline/data/htmltomd.py b/src/python/txtai/pipeline/data/htmltomd.py
index 4a87ab24..21a6bd1d 100644
--- a/src/python/txtai/pipeline/data/htmltomd.py
+++ b/src/python/txtai/pipeline/data/htmltomd.py
@@ -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)
diff --git a/test/python/testpipeline/testdata/testtextractor.py b/test/python/testpipeline/testdata/testtextractor.py
index 5dd74693..5f9854f5 100644
--- a/test/python/testpipeline/testdata/testtextractor.py
+++ b/test/python/testpipeline/testdata/testtextractor.py
@@ -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*")