62 lines
1.4 KiB
JavaScript
62 lines
1.4 KiB
JavaScript
/**
|
|
* @typedef {import('hast').Element} Element
|
|
* @typedef {import('mdast').Table} Table
|
|
* @typedef {import('../state.js').State} State
|
|
*/
|
|
|
|
import {pointEnd, pointStart} from 'unist-util-position'
|
|
|
|
/**
|
|
* Turn an mdast `table` node into hast.
|
|
*
|
|
* @param {State} state
|
|
* Info passed around.
|
|
* @param {Table} node
|
|
* mdast node.
|
|
* @returns {Element}
|
|
* hast node.
|
|
*/
|
|
export function table(state, node) {
|
|
const rows = state.all(node)
|
|
const firstRow = rows.shift()
|
|
/** @type {Array<Element>} */
|
|
const tableContent = []
|
|
|
|
if (firstRow) {
|
|
/** @type {Element} */
|
|
const head = {
|
|
type: 'element',
|
|
tagName: 'thead',
|
|
properties: {},
|
|
children: state.wrap([firstRow], true)
|
|
}
|
|
state.patch(node.children[0], head)
|
|
tableContent.push(head)
|
|
}
|
|
|
|
if (rows.length > 0) {
|
|
/** @type {Element} */
|
|
const body = {
|
|
type: 'element',
|
|
tagName: 'tbody',
|
|
properties: {},
|
|
children: state.wrap(rows, true)
|
|
}
|
|
|
|
const start = pointStart(node.children[1])
|
|
const end = pointEnd(node.children[node.children.length - 1])
|
|
if (start && end) body.position = {start, end}
|
|
tableContent.push(body)
|
|
}
|
|
|
|
/** @type {Element} */
|
|
const result = {
|
|
type: 'element',
|
|
tagName: 'table',
|
|
properties: {},
|
|
children: state.wrap(tableContent, true)
|
|
}
|
|
state.patch(node, result)
|
|
return state.applyData(node, result)
|
|
}
|