Repository navigation
refactor(parser): preserve HTML <table> structure (inline + structured item) - #18129
Conversation
…d item) Stop flattening <table> into a single text blob. A <table> now emits: 1. an inlined doc_type_kwd:"text" item keeping the <table>...</table> markup (row/column structure survives for embedding/retrieval/LLM rendering), and 2. a structured doc_type_kwd:"table" / ck_type:"table" item appended after the walk, consumed by the downstream chunker. Covers both top-level tables (walkHTMLBlocks) and tables nested in div/section/article (via walkHTMLLeaf). Mirrors markdown_parser.go table handling and Python's RAGFlowHtmlParser. Tests: - TestHTMLParser_TableProducesStructuredItems - TestHTMLParser_NestedTableProducesStructuredItems - TestHTMLParser_NestedListItemsPreserved - TestHTMLParser_PrePreservesVerbatim - TestHTMLParser_AlignmentGolden (compares vs html.python.golden.json, ignores "table" via meta.accepted_divergences) Depends on infiniflow#18014 and the foundation helpers PR (align_test.go). Files: internal/parser/parser/html_parser.go internal/parser/parser/html_parser_align_test.go (new) internal/parser/parser/testdata/html.python.golden.json (new) internal/parser/parser/testdata/html.sample.html (new)
renderTableHTML only fails on unsupported node kinds, which a parsed <table> never triggers, so the degenerate flatten-children branch in walkHTMLLeaf is unreachable. Remove it so the nested-table path matches the top-level walkHTMLBlocks case (both skip silently when the markup is empty). Also tighten the inaccurate 'mirrors Python's HtmlParser' inline comment in the same block (the two-item split is the markdown_parser.go model; Python keeps the table once, outside the inline flow).
📝 WalkthroughWalkthroughThe HTML parser now preserves tables as inline HTML and separate structured items. Nested tables retain their markup and content. Tests and English/Chinese golden fixtures validate table ordering, nested lists, verbatim text, and parser alignment. ChangesHTML table preservation
Estimated code review effort: 3 (Moderate) | ~25 minutes Sequence Diagram(s)sequenceDiagram
participant HTMLDocument
participant walkHTMLBlocks
participant walkHTMLLeaf
participant renderHTMLTable
participant ParserItems
HTMLDocument->>walkHTMLBlocks: traverse document blocks
walkHTMLBlocks->>walkHTMLLeaf: traverse leaf content
walkHTMLLeaf->>renderHTMLTable: serialize encountered table
renderHTMLTable->>ParserItems: emit inline table markup
renderHTMLTable->>ParserItems: collect structured table item
walkHTMLBlocks->>ParserItems: append structured tables after traversal
Possibly related issues
Possibly related PRs
Suggested labels: Suggested reviewers: Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🧹 Nitpick comments (1)
internal/parser/parser/html_parser_align_test.go (1)
21-23: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick winRemove migration-history comments from the tests.
The current assertions already define the required table behavior. The descriptions of the pre-fix implementation will become stale.
internal/parser/parser/html_parser_align_test.go#L21-L23: remove the obsolete flattened-output description.internal/parser/parser/html_parser_align_test.go#L92-L100: remove the obsolete nested-table traversal description.As per coding guidelines, remove stale documentation and keep Go comments aligned with the current runtime path rather than migration history.
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@internal/parser/parser/html_parser_align_test.go` around lines 21 - 23, Remove the obsolete migration-history comments from internal/parser/parser/html_parser_align_test.go at lines 21-23 and 92-100; retain the existing assertions and test logic, including the current table and nested-table behavior coverage.Source: Coding guidelines
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@internal/parser/parser/html_parser.go`:
- Around line 136-157: Update both table-handling paths in
internal/parser/parser/html_parser.go (lines 136-157 and 323-341) to traverse
descendant tables before returning, ensuring nested tables produce structured
items with doc_type_kwd:"table" in addition to the parent table. Collapse the
duplicate collection logic into a shared helper or reusable path where
appropriate, and add a test covering a table nested inside a table cell.
---
Nitpick comments:
In `@internal/parser/parser/html_parser_align_test.go`:
- Around line 21-23: Remove the obsolete migration-history comments from
internal/parser/parser/html_parser_align_test.go at lines 21-23 and 92-100;
retain the existing assertions and test logic, including the current table and
nested-table behavior coverage.
🪄 Autofix
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro Plus
Run ID: 8602a36d-917d-4a9f-93ae-ae90e2c5ada9
📒 Files selected for processing (4)
internal/parser/parser/html_parser.gointernal/parser/parser/html_parser_align_test.gointernal/parser/parser/testdata/html.python.golden.jsoninternal/parser/parser/testdata/html.sample.html
| case "table": | ||
| // Keep the table as its full HTML markup (NOT flattened) so | ||
| // row/column structure survives into embedding, retrieval, and | ||
| // LLM rendering. This mirrors the markdown table handling | ||
| // (markdown_parser.go:305-328) and Python's HtmlParser, which | ||
| // keeps the <table>…</table> string as the section text. | ||
| markup := renderTableHTML(child) | ||
| if strings.TrimSpace(markup) != "" { | ||
| // Inlined copy in document order (matches Python's <table> | ||
| // section text and markdown's text-flow copy). | ||
| *out = append(*out, map[string]any{ | ||
| "text": markup, | ||
| "doc_type_kwd": "text", | ||
| }) | ||
| // Structured table item, appended after the walk. | ||
| tableItems = append(tableItems, map[string]any{ | ||
| "text": markup, | ||
| "doc_type_kwd": "table", | ||
| "ck_type": "table", | ||
| }) | ||
| } | ||
| continue |
There was a problem hiding this comment.
🗄️ Data Integrity & Integration | 🟠 Major | ⚡ Quick win
Collect structured items for tables nested inside table cells.
Both paths serialize the current table and return. If a <table> is inside a <td>, its markup is included in the parent table, but the child table is never visited as a table node. The child table therefore has no structured doc_type_kwd:"table" item.
Collect descendant table nodes before returning, and add a test with a table nested in a table cell.
internal/parser/parser/html_parser.go#L136-L157: collect structured items for child tables in the top-level table path.internal/parser/parser/html_parser.go#L323-L341: collect structured items for child tables in the leaf traversal path.
As per coding guidelines, collapse duplicate paths in actively refactored parser code.
📍 Affects 1 file
internal/parser/parser/html_parser.go#L136-L157(this comment)internal/parser/parser/html_parser.go#L323-L341
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
In `@internal/parser/parser/html_parser.go` around lines 136 - 157, Update both
table-handling paths in internal/parser/parser/html_parser.go (lines 136-157 and
323-341) to traverse descendant tables before returning, ensuring nested tables
produce structured items with doc_type_kwd:"table" in addition to the parent
table. Collapse the duplicate collection logic into a shared helper or reusable
path where appropriate, and add a test covering a table nested inside a table
cell.
Source: Coding guidelines
Mirror the markdown/textcode alignment pattern: rename the English html sample to _en, add a focused _zh sample (full-width delimiters, Chinese table cells), and make TestHTMLParser_AlignmentGolden table-driven over both cases. No generator script is committed; baselines stay reproducible from each golden's meta block. Co-Authored-By: CodeBuddy <[email protected]>
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@internal/parser/parser/testdata/html.sample.en.html`:
- Line 1: Add <!doctype html> before the opening <html> element in both
internal/parser/parser/testdata/html.sample.en.html lines 1-1 and
internal/parser/parser/testdata/html.sample.zh.html lines 1-1, ensuring each
complete HTML fixture begins with the doctype declaration.
🪄 Autofix
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro Plus
Run ID: 36ea06fe-f925-4f27-89c5-9338c3809090
📒 Files selected for processing (5)
internal/parser/parser/html_parser_align_test.gointernal/parser/parser/testdata/html.python.en.golden.jsoninternal/parser/parser/testdata/html.python.zh.golden.jsoninternal/parser/parser/testdata/html.sample.en.htmlinternal/parser/parser/testdata/html.sample.zh.html
🚧 Files skipped from review as they are similar to previous changes (1)
- internal/parser/parser/html_parser_align_test.go
| @@ -0,0 +1,11 @@ | |||
| <html><body> | |||
There was a problem hiding this comment.
📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick win
Add <!doctype html> to both HTML fixtures.
Both complete documents start with <html> and trigger the same doctype-first lint error.
internal/parser/parser/testdata/html.sample.en.html#L1-L1: add<!doctype html>before<html>.internal/parser/parser/testdata/html.sample.zh.html#L1-L1: add<!doctype html>before<html>.
🧰 Tools
🪛 HTMLHint (1.9.2)
[error] 1-1: Doctype must be declared before any non-comment content.
(doctype-first)
📍 Affects 2 files
internal/parser/parser/testdata/html.sample.en.html#L1-L1(this comment)internal/parser/parser/testdata/html.sample.zh.html#L1-L1
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
In `@internal/parser/parser/testdata/html.sample.en.html` at line 1, Add <!doctype
html> before the opening <html> element in both
internal/parser/parser/testdata/html.sample.en.html lines 1-1 and
internal/parser/parser/testdata/html.sample.zh.html lines 1-1, ensuring each
complete HTML fixture begins with the doctype declaration.
Source: Linters/SAST tools
Summary
Stop flattening
<table>into a single text blob. A<table>now emits:doc_type_kwd:"text"item keeping the<table>…</table>markup(row/column structure survives for embedding/retrieval/LLM rendering), and
doc_type_kwd:"table"/ck_type:"table"item appended afterthe walk, consumed by the downstream chunker.
Covers both top-level tables (
walkHTMLBlocks) and tables nested indiv/section/article (via
walkHTMLLeaf). Mirrorsmarkdown_parser.go'stwo-item table handling (inline
textcopy + appendeddoc_type_kwd:"table"item). It matches Python's
RAGFlowHtmlParseronly in keeping the raw<table>…</table>markup: Python keeps the table once, outside the inlineflow, and adds
doc_type_kwd/ck_typedownstream — this change emits bothitems at the parser layer.
Test plan
TestHTMLParser_TableProducesStructuredItemsTestHTMLParser_NestedTableProducesStructuredItemsTestHTMLParser_NestedListItemsPreservedTestHTMLParser_PrePreservesVerbatimTestHTMLParser_AlignmentGolden(compares vshtml.python.golden.json,ignores
"table"viameta.accepted_divergences)Dependency
fix/parser-align-foundation→ main).main; this PR is opened from the fork(
xugangqiang:fix/html-table-structure) per repo workflow.Note
This change was not described in the original combined PR's title; called out
here for explicit review.
Related