ElyMark: a table stays a table whatever its spacing; edits keep the document's table style (R2.13)

This commit is contained in:
ZacharyZhang-NY committed 2026-10-09 03:50:52 -04:00
1 parent 7049a6a4aa
commit f28768634f
5 files changed
+31 -7

No files matched your search

+13 -2
View File
@@ -1,16 +1,17 @@
// The document's own markers, so blocks write back unchanged.
import { unified } from 'unified'
import remarkParse from 'remark-parse'
import remarkGfm from 'remark-gfm'
import { visit } from 'unist-util-visit'
/** remark-stringify options most of `text` already uses. */
/** Writing options most of `text` already uses; tables' for remark-gfm. */
export function style(text) {
const votes = {}
const vote = (key, value) => {
votes[key] ??= new Map()
votes[key].set(value, (votes[key].get(value) ?? 0) + 1)
}
visit(unified().use(remarkParse).parse(text), (node) => {
visit(unified().use(remarkParse).use(remarkGfm).parse(text), (node) => {
const at = node.position?.start.offset
if (at === undefined) return
const head = text.slice(at, at + 12)
@@ -34,6 +35,16 @@ export function style(text) {
case 'heading':
if (node.depth <= 2) vote('setext', head[0] !== '#')
break
case 'table': {
const rows = text.slice(at, node.position.end.offset).split('\n').map((row) => row.trim())
const padded = /\| /.test(rows[0])
const cells = rows.flatMap((row) => row.replace(/^\||\|$/g, '').split('|'))
const inner = (cell) => (padded ? cell.replace(/^ | $/g, '') : cell)
vote('tableCellPadding', padded)
// Aligned cells carry spaces beyond the padding.
vote('tablePipeAlign', cells.some((cell) => /^\s|\s$/.test(inner(cell))))
break
}
}
})
const most = (key) => [...votes[key].entries()].sort((a, b) => b[1] - a[1])[0][0]