site/node_modules/micromark-extension-gfm-table/dev/lib/infer.js
2024-10-14 08:09:33 +02:00

65 lines
1.6 KiB
JavaScript

/**
* @typedef {import('micromark-util-types').Event} Event
*/
/**
* @typedef {'center' | 'left' | 'none' | 'right'} Align
*/
import {ok as assert} from 'devlop'
/**
* Figure out the alignment of a GFM table.
*
* @param {Readonly<Array<Event>>} events
* List of events.
* @param {number} index
* Table enter event.
* @returns {Array<Align>}
* List of aligns.
*/
export function gfmTableAlign(events, index) {
assert(events[index][1].type === 'table', 'expected table')
let inDelimiterRow = false
/** @type {Array<Align>} */
const align = []
while (index < events.length) {
const event = events[index]
if (inDelimiterRow) {
if (event[0] === 'enter') {
// Start of alignment value: set a new column.
// To do: `markdown-rs` uses `tableDelimiterCellValue`.
if (event[1].type === 'tableContent') {
align.push(
events[index + 1][1].type === 'tableDelimiterMarker'
? 'left'
: 'none'
)
}
}
// Exits:
// End of alignment value: change the column.
// To do: `markdown-rs` uses `tableDelimiterCellValue`.
else if (event[1].type === 'tableContent') {
if (events[index - 1][1].type === 'tableDelimiterMarker') {
const alignIndex = align.length - 1
align[alignIndex] = align[alignIndex] === 'left' ? 'center' : 'right'
}
}
// Done!
else if (event[1].type === 'tableDelimiterRow') {
break
}
} else if (event[0] === 'enter' && event[1].type === 'tableDelimiterRow') {
inDelimiterRow = true
}
index += 1
}
return align
}