UNPKG

myst-to-jats

Version:
22 lines (21 loc) 766 B
import { selectAll } from 'unist-util-select'; export function tableTransform(mdast) { const tables = selectAll('table', mdast); tables.forEach((table) => { const head = { type: 'tableHead', children: [] }; const body = { type: 'tableBody', children: [] }; table.children.forEach((tr) => { const isHeaderRow = tr.children.reduce((h, v) => h && !!v.header, true); if (isHeaderRow && body.children.length === 0) { head.children.push(tr); } else { body.children.push(tr); } }); table.children = head.children.length > 0 ? [head, body] : [body]; }); } export const tablePlugin = () => (tree) => { tableTransform(tree); };