Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/table-whitespace.md
Original file line number Diff line number Diff line change
@@ -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.
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
32 changes: 24 additions & 8 deletions packages/react/src/jsx-ast/plugins/transformer.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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];
}
}
}
}
}
});
Expand Down
Loading