-
Notifications
You must be signed in to change notification settings - Fork 68
fix(table): handle HTML table captions correctly #640
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -498,42 +498,155 @@ const HTML_BLOCK_RULE = { | |||||||||
| } | ||||||||||
| }; | ||||||||||
|
|
||||||||||
| const CAPTION_RULE = { | ||||||||||
| deserialize(el, next, ignoreSpace) { | ||||||||||
| if (el.tagName && el.tagName.toLowerCase() === 'caption') { | ||||||||||
| return { | ||||||||||
| type: 'caption', | ||||||||||
| nodes: next(el.childNodes, ignoreSpace) | ||||||||||
| }; | ||||||||||
| } | ||||||||||
| } | ||||||||||
| }; | ||||||||||
|
|
||||||||||
| /** | ||||||||||
| * Clean table cell nodes by removing Softbreaks and normalizing whitespace. | ||||||||||
| * @param {Array} nodes - the list of nodes | ||||||||||
| * @returns {Array} - the cleaned list of nodes | ||||||||||
| */ | ||||||||||
| function cleanTableNodes(nodes) { | ||||||||||
| const NS = CommonMarkModel.NAMESPACE; | ||||||||||
| const TEXT = `${NS}.Text`; | ||||||||||
| const SOFT = `${NS}.Softbreak`; | ||||||||||
|
|
||||||||||
| if (!nodes) return []; | ||||||||||
| nodes = Array.isArray(nodes) ? nodes : [nodes]; | ||||||||||
|
|
||||||||||
| const merged = nodes.reduce((acc, node) => { | ||||||||||
| if (!node) return acc; | ||||||||||
|
|
||||||||||
| let newNode = { ...node }; | ||||||||||
| if (newNode.nodes) newNode = { ...newNode, nodes: cleanTableNodes(newNode.nodes) }; | ||||||||||
|
|
||||||||||
| if (newNode.$class === SOFT) { | ||||||||||
| newNode = { $class: TEXT, text: ' ' }; | ||||||||||
| } | ||||||||||
|
|
||||||||||
| const last = acc[acc.length - 1]; | ||||||||||
| if (last && last.$class === TEXT && newNode.$class === TEXT) | ||||||||||
| { | ||||||||||
| last.text += newNode.text; | ||||||||||
| } | ||||||||||
| else | ||||||||||
| { | ||||||||||
| acc.push(newNode); | ||||||||||
| } | ||||||||||
|
|
||||||||||
| return acc; | ||||||||||
| }, []); | ||||||||||
|
|
||||||||||
| // Normalize whitespace inside Text nodes | ||||||||||
| merged.forEach(n => { | ||||||||||
| if (n.$class === TEXT) n.text = n.text.replace(/\s+/g, ' '); | ||||||||||
| }); | ||||||||||
|
|
||||||||||
|
|
||||||||||
| if (merged.length > 0 && merged[0].$class === TEXT) { | ||||||||||
| merged[0].text = merged[0].text.replace(/^\s+/, ''); | ||||||||||
| } | ||||||||||
| if (merged.length > 0 && merged[merged.length - 1].$class === TEXT) { | ||||||||||
| merged[merged.length - 1].text = merged[merged.length - 1].text.replace(/\s+$/, ''); | ||||||||||
| } | ||||||||||
|
|
||||||||||
| return merged.filter(n => n.$class !== TEXT || n.text.length > 0); | ||||||||||
| } | ||||||||||
|
Comment on lines
+517
to
+562
|
||||||||||
|
|
||||||||||
|
|
||||||||||
| const TABLE_RULE = { | ||||||||||
| deserialize(el, next, ignoreSpace) { | ||||||||||
| if (el.tagName && el.tagName.toLowerCase() === 'table') { | ||||||||||
| return { | ||||||||||
| const children = next(el.childNodes, ignoreSpace); | ||||||||||
| const captionNode = children.find(c => c.type === 'caption'); | ||||||||||
| let tableNodes = children.filter(node => | ||||||||||
| node.$class === `${CommonMarkModel.NAMESPACE}.TableHead` || | ||||||||||
| node.$class === `${CommonMarkModel.NAMESPACE}.TableBody` | ||||||||||
| ); | ||||||||||
|
|
||||||||||
| let head = tableNodes.find(n => n.$class === `${CommonMarkModel.NAMESPACE}.TableHead`); | ||||||||||
| const body = tableNodes.find(n => n.$class === `${CommonMarkModel.NAMESPACE}.TableBody`); | ||||||||||
|
|
||||||||||
| if (!head && body && body.nodes && body.nodes.length > 0) { | ||||||||||
| const firstRow = body.nodes[0]; | ||||||||||
| const hasHeaderCells = firstRow.nodes && firstRow.nodes.some(n => n.$class === `${CommonMarkModel.NAMESPACE}.HeaderCell`); | ||||||||||
|
|
||||||||||
| if (hasHeaderCells) { | ||||||||||
| head = { | ||||||||||
| $class: `${CommonMarkModel.NAMESPACE}.TableHead`, | ||||||||||
| nodes: [firstRow] | ||||||||||
| }; | ||||||||||
| const newBody = { | ||||||||||
| $class: `${CommonMarkModel.NAMESPACE}.TableBody`, | ||||||||||
| nodes: body.nodes.slice(1) | ||||||||||
| }; | ||||||||||
| tableNodes = [head, newBody]; | ||||||||||
| } | ||||||||||
| } | ||||||||||
|
|
||||||||||
| const table = { | ||||||||||
| $class: `${CommonMarkModel.NAMESPACE}.Table`, | ||||||||||
| nodes: next(el.childNodes), | ||||||||||
| nodes: tableNodes, | ||||||||||
| }; | ||||||||||
|
|
||||||||||
| if (captionNode) { | ||||||||||
| const captionParagraph = { | ||||||||||
| $class: `${CommonMarkModel.NAMESPACE}.Paragraph`, | ||||||||||
| nodes: [ | ||||||||||
| { | ||||||||||
| $class: `${CommonMarkModel.NAMESPACE}.Strong`, | ||||||||||
| nodes: cleanTableNodes(captionNode.nodes) | ||||||||||
| }, | ||||||||||
|
Comment on lines
+603
to
+607
|
||||||||||
| { | ||||||||||
| $class: `${CommonMarkModel.NAMESPACE}.Text`, | ||||||||||
| text: '\n\n' | ||||||||||
|
Comment on lines
+607
to
+610
|
||||||||||
| }, | |
| { | |
| $class: `${CommonMarkModel.NAMESPACE}.Text`, | |
| text: '\n\n' |
Copilot
AI
Apr 26, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are snapshot roundtrip tests for tables, but no coverage for HTML input containing <caption> (or the new header/body normalization paths). Please add a test that calls HtmlTransformer.toCiceroMark() on an HTML table with a caption and asserts the resulting CiceroMark/Markdown output (and ideally a roundtrip) so regressions in caption parsing/whitespace cleanup are caught.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
CAPTION_RULE returns a plain object with
type: 'caption'and no$class. If a<caption>appears outside of a<table>(invalid HTML, but possible input), this node will leak into the output DOM and likely break downstream visitors/serializers that expect only CommonMark/CiceroMark$classnodes. Consider handling<caption>directly inside thetablebranch (query the DOM children for<caption>and deserialize its contents there) or returning a valid CommonMark node and filtering it out safely.