Skip to content

armgit / txtai

Public Imported from GitHub
0

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

Preserve ordered-list start values in HTMLToMarkdown (#1355) (#1356)

Gaurav Chintakuntac8e7d813afa2
diff --git a/src/python/txtai/pipeline/data/htmltomd.py b/src/python/txtai/pipeline/data/htmltomd.py
index 21a6bd1d..8286dce5 100644
--- a/src/python/txtai/pipeline/data/htmltomd.py
+++ b/src/python/txtai/pipeline/data/htmltomd.py
@@ -269,6 +269,12 @@ class HTMLToMarkdown(Pipeline):
"""
elements, count = [], 0
+ if node.name == "ol":
+ try:
+ count = int(node.get("start", 1)) - 1
+ except ValueError:
+ pass
+
for element in node.find_all(("li", "ul", "ol"), recursive=False):
# Nested list that is a direct child of this list
if element.name != "li":
diff --git a/test/python/testpipeline/testdata/testtextractor.py b/test/python/testpipeline/testdata/testtextractor.py
index 5f9854f5..440f33de 100644
--- a/test/python/testpipeline/testdata/testtextractor.py
+++ b/test/python/testpipeline/testdata/testtextractor.py
@@ -175,6 +175,26 @@ class TestTextractor(unittest.TestCase):
"|Header1|Header2|\n|---|---|\n|Test1 Test2|Test3|",
)
+ def testHTMLListStart(self):
+ """
+ Test ordered lists retain their starting number
+ """
+
+ for start, expected in [("5", "5. Test1\n6. Test2"), ("0", "0. Test1\n1. Test2"), ("1", "1. Test1\n2. Test2")]:
+ with self.subTest(start=start):
+ self.assertMarkdown(f"<ol start='{start}'><li>Test1</li><li>Test2</li></ol>", expected)
+
+ self.assertMarkdown(
+ "<ol start='5'><li>Test1<ol start='3'><li>Test2</li></ol></li><li>Test3</li></ol>",
+ "5. Test1\n\t3. Test2\n6. Test3",
+ )
+
+ for start in ["", "invalid", "1.5"]:
+ with self.subTest(start=start):
+ self.assertMarkdown(f"<ol start='{start}'><li>Test1</li><li>Test2</li></ol>", "1. Test1\n2. Test2")
+
+ self.assertMarkdown("<ul start='invalid'><li>Test1</li><li>Test2</li></ul>", "- Test1\n- Test2")
+
def testHTMLMetadataNoContent(self):
"""
Test HTML with a meta description tag that has no content attribute