| 8 | * based on their corresponding table headers (th). |
| 9 | */ |
| 10 | export default function remarkTableTitles() { |
| 11 | return (tree: Root) => { |
| 12 | visit(tree, 'table', table => { |
| 13 | // Ensure table has at least a header row and one data row |
| 14 | if (table.children.length < 2) { |
| 15 | return; |
| 16 | } |
| 17 | |
| 18 | const [headerRow, ...dataRows] = table.children; |
| 19 | |
| 20 | if (headerRow.children.length <= 1) { |
| 21 | table.data ??= {}; |
| 22 | |
| 23 | table.data.hProperties = { |
| 24 | 'data-cards': 'false', |
| 25 | }; |
| 26 | } |
| 27 | |
| 28 | // Extract header labels from the first row |
| 29 | const headerLabels = headerRow.children.map(headerCell => |
| 30 | toString(headerCell.children) |
| 31 | ); |
| 32 | |
| 33 | // Assign data-label to each cell in data rows |
| 34 | dataRows.forEach(row => { |
| 35 | row.children.forEach((cell, idx) => { |
| 36 | if (idx > headerLabels.length - 1) { |
| 37 | return; |
| 38 | } |
| 39 | cell.data ??= {}; |
| 40 | |
| 41 | cell.data.hProperties = { |
| 42 | 'data-label': headerLabels[idx], |
| 43 | }; |
| 44 | }); |
| 45 | }); |
| 46 | }); |
| 47 | }; |
| 48 | } |