( document: Node, conditionalTags: string[] = ['if'] )
| 35 | } |
| 36 | |
| 37 | export default function transform( |
| 38 | document: Node, |
| 39 | conditionalTags: string[] = ['if'] |
| 40 | ) { |
| 41 | for (const node of document.walk()) { |
| 42 | if (node.type !== 'tag' || node.tag !== 'table') continue; |
| 43 | |
| 44 | const [first, ...rest] = node.children; |
| 45 | if (!first || first.type === 'table') continue; |
| 46 | |
| 47 | const table = new Ast.Node('table', node.attributes, [ |
| 48 | new Ast.Node('thead'), |
| 49 | new Ast.Node('tbody'), |
| 50 | ]); |
| 51 | |
| 52 | const [thead, tbody] = table.children; |
| 53 | |
| 54 | if (first.type === 'list') thead.push(convertToRow(first, 'th')); |
| 55 | |
| 56 | for (const row of rest) { |
| 57 | // Convert lists to rows with special-case support for conditionals |
| 58 | // When a conditional is encountered, convert all of its top-level lists to rows |
| 59 | if (row.type === 'list') convertToRow(row); |
| 60 | else if (isConditionalTag(row, conditionalTags)) { |
| 61 | const children = []; |
| 62 | |
| 63 | for (const child of row.children) { |
| 64 | // Replace children and skip HRs in order to support conditionals with multiple rows |
| 65 | if (child.type === 'hr') continue; |
| 66 | if (child.type === 'list') convertToRow(child); |
| 67 | else if ( |
| 68 | isComment(child) || |
| 69 | child.tag === 'else' || |
| 70 | isConditionalTag(child, conditionalTags) |
| 71 | ) { |
| 72 | // Allow structural tags: else, nested conditionals, and comments |
| 73 | } else { |
| 74 | row.errors.push(unexpectedNodeError(child)); |
| 75 | continue; |
| 76 | } |
| 77 | children.push(child); |
| 78 | } |
| 79 | |
| 80 | row.children = children; |
| 81 | } else if (row.type !== 'hr' && !isComment(row)) { |
| 82 | node.errors.push(unexpectedNodeError(row)); |
| 83 | continue; |
| 84 | } else continue; |
| 85 | tbody.push(row); |
| 86 | } |
| 87 | |
| 88 | node.children = [table]; |
| 89 | } |
| 90 | } |
nothing calls this directly
no test coverage detected
searching dependent graphs…