diff --git a/.changeset/table-whitespace.md b/.changeset/table-whitespace.md new file mode 100644 index 000000000..3cac34375 --- /dev/null +++ b/.changeset/table-whitespace.md @@ -0,0 +1,5 @@ +--- +'@doc-kit/generator-react': patch +--- + +Fix a crash when a table's rows and cells are surrounded by whitespace text nodes, as `rehype-raw` 9.1.1 and later keep them. The transformer now finds the header row and the cells themselves instead of assuming they are the first children, and labels cells by their column. diff --git a/packages/react/src/jsx-ast/plugins/__tests__/transformer.test.mjs b/packages/react/src/jsx-ast/plugins/__tests__/transformer.test.mjs index 0af3f6a8d..38ad991a6 100644 --- a/packages/react/src/jsx-ast/plugins/__tests__/transformer.test.mjs +++ b/packages/react/src/jsx-ast/plugins/__tests__/transformer.test.mjs @@ -3,7 +3,97 @@ import { describe, it } from 'node:test'; import transformer from '../transformer.mjs'; +const text = value => ({ type: 'text', value }); + +const element = (tagName, children) => ({ + type: 'element', + tagName, + properties: {}, + children, +}); + +const cell = (tagName, value) => element(tagName, [text(value)]); + +/** + * Builds a row whose cells are surrounded by whitespace text nodes, as + * `rehype-raw` leaves them. + */ +const spacedRow = (tagName, values) => + element('tr', [ + text('\n'), + ...values.flatMap(value => [cell(tagName, value), text('\n')]), + ]); + +/** + * Builds a table whose rows and cells are surrounded by whitespace text nodes. + */ +const spacedTable = () => + element('table', [ + text('\n'), + element('thead', [ + text('\n'), + spacedRow('th', ['Name', 'Value']), + text('\n'), + ]), + text('\n'), + element('tbody', [ + text('\n'), + spacedRow('td', ['a', '1']), + text('\n'), + spacedRow('td', ['b', '2']), + text('\n'), + ]), + text('\n'), + ]); + describe('jsx-ast transformer', () => { + it('labels cells when whitespace surrounds the header row', () => { + const tree = { type: 'root', children: [spacedTable()] }; + + transformer()(tree); + + const [container] = tree.children; + const table = container.children[0]; + const tbody = table.children.find(el => el.tagName === 'tbody'); + const labels = tbody.children + .filter(el => el.tagName === 'tr') + .flatMap(tr => tr.children.filter(el => el.tagName === 'td')) + .map(td => td.properties['data-label']); + + assert.equal(container.tagName, 'div'); + assert.deepEqual(container.properties.className, ['overflow-container']); + assert.deepEqual(labels, ['Name', 'Value', 'Name', 'Value']); + }); + + it('does not throw when the header has no row', () => { + const tree = { + type: 'root', + children: [ + element('table', [ + element('thead', []), + element('tbody', [spacedRow('td', ['a'])]), + ]), + ], + }; + + assert.doesNotThrow(() => transformer()(tree)); + }); + + it('does not throw when a table with a header has no body', () => { + const tree = { + type: 'root', + children: [ + element('table', [ + text('\n'), + element('thead', [spacedRow('th', ['a'])]), + text('\n'), + ]), + ], + }; + + assert.doesNotThrow(() => transformer()(tree)); + }); + it('moves generated footnotes into the page content fragment', () => { const content = { type: 'mdxJsxFlowElement', diff --git a/packages/react/src/jsx-ast/plugins/transformer.mjs b/packages/react/src/jsx-ast/plugins/transformer.mjs index cbb768357..a6afe1eca 100644 --- a/packages/react/src/jsx-ast/plugins/transformer.mjs +++ b/packages/react/src/jsx-ast/plugins/transformer.mjs @@ -13,6 +13,12 @@ const isFootnotesSection = node => (node.properties?.dataFootnotes !== undefined || node.properties?.className?.includes('footnotes')); +/** + * Whether a HAST node is a table cell. + * @param {import('hast').Element} node + */ +const isTableCell = node => node.tagName === 'td' || node.tagName === 'th'; + /** * Finds the JSX fragment wrapping the page content (see `buildContent`). * @param {import('hast').Root} tree @@ -50,18 +56,28 @@ const transformer = tree => { // Not every table will have a header, so only do this on tables // with them. const thead = node.children.find(el => el.tagName === 'thead'); + const tbody = node.children.find(el => el.tagName === 'tbody'); - if (thead) { + if (thead && tbody) { + // Table rows and cells don't have to be the first children, nor be + // adjacent: `rehype-raw` keeps the whitespace between them as text + // nodes. // TODO(@avivkeller): These are only strings afaict, so a `toString` dependency // might not actually be needed. - const headers = thead.children[0].children.map(toString); - const tbody = node.children.find(el => el.tagName === 'tbody'); + const headerRow = thead.children.find(el => el.tagName === 'tr'); + const headers = + headerRow?.children.filter(isTableCell).map(toString) ?? []; + + for (const tr of tbody.children.filter(el => el.tagName === 'tr')) { + const cells = tr.children.filter(isTableCell); - visit( - tbody, - node => node.tagName === 'td', - (node, index) => (node.properties['data-label'] = headers[index]) - ); + for (const [column, cell] of cells.entries()) { + if (cell.tagName === 'td' && headers[column] !== undefined) { + cell.properties ||= {}; + cell.properties['data-label'] = headers[column]; + } + } + } } } });