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 the paragraphs of a blockquote on separate lines in HTMLToMarkdown

RavSinghChandan4a60ff1288f9
diff --git a/src/python/txtai/pipeline/data/htmltomd.py b/src/python/txtai/pipeline/data/htmltomd.py
index 7f09025e..b4ffb664 100644
--- a/src/python/txtai/pipeline/data/htmltomd.py
+++ b/src/python/txtai/pipeline/data/htmltomd.py
@@ -253,7 +253,17 @@ class HTMLToMarkdown(Pipeline):
block as markdown
"""
- text = "\n".join(f"> {x}" for x in node.text.strip().split("\n"))
+ # Start each paragraph on its own line, even when the HTML has no whitespace between them
+ text, newline = "", False
+ for child in node.children:
+ if child.name or child.text.strip():
+ if (child.name in ("p", "div") or newline) and text and not text.endswith("\n"):
+ text += "\n"
+ newline = child.name in ("p", "div")
+
+ text += child.text
+
+ text = "\n".join(f"> {x}" for x in text.strip().split("\n"))
return f"{text}\n\n" if self.paragraphs else f"{text}\n"
def items(self, node, article):
diff --git a/test/python/testpipeline/testdata/testtextractor.py b/test/python/testpipeline/testdata/testtextractor.py
index 440f33de..96e8a4be 100644
--- a/test/python/testpipeline/testdata/testtextractor.py
+++ b/test/python/testpipeline/testdata/testtextractor.py
@@ -108,6 +108,8 @@ class TestTextractor(unittest.TestCase):
# Blockquotes
self.assertMarkdown("<blockquote>This is a test</blockquote>", "> This is a test")
+ self.assertMarkdown("<blockquote><p>Test1</p><p>Test2</p></blockquote>", "> Test1\n> Test2")
+ self.assertMarkdown("<blockquote>Test1<p>Test2</p>Test3</blockquote>", "> Test1\n> Test2\n> Test3")
# Lists
self.assertMarkdown("<ul><li>Test1</li><li>Test2</li></ul>", "- Test1\n- Test2")