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

This commit is contained in:
Gaurav Chintakunta
2026-10-08 07:05:07 -04:00
committed by GitHub
parent edf61d1d4a
commit c8e7d813af
2 changed files with 26 additions and 0 deletions
@@ -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":
+20
View File
@@ -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