3
0
mirror of https://github.com/larvit/adf-codec.git synced 2026-11-18 14:15:44 +01:00

Read GFM's pipe table without leading pipes and with alignment colons in portableMarkdownToAdf

This commit is contained in:
2026-10-14 00:33:59 +02:00
parent 60f50a4f2c
commit a43b532eaa
9 changed files with 78 additions and 39 deletions
+38 -10
View File
@@ -19,7 +19,7 @@ import {
thematicBreakTail,
type ThematicBreakTail,
} from '../commonmark/grammar.ts'
import { barePipeCells, isDelimiterRow, isPipeAlignment, isPipeDelimiter, malformedPipeTable, pipeCells } from '../pipe-table-syntax.ts'
import { barePipeCells, isDelimiterRow, isPipeAlignment, malformedPipeTable, pipeCells } from '../pipe-table-syntax.ts'
import { blockDirectiveForm } from '../block-directive.ts'
import { directiveEscape, malformedDirective, readDirectiveLine, spellDirectiveCloser } from '../directive-syntax.ts'
import { readLinkDefinitions } from '../commonmark/link-reference-definitions.ts'
@@ -355,8 +355,13 @@ function continuesLazily(walk: Walk, line: Line): boolean {
if (walk.leaf?.kind !== 'paragraph' || isBlankLine(line.text)) return false
if (leadingColumns(line) >= indentedCodeColumns) return true
const opener = removeColumns(line, largestOpenerIndentation).text
if ((walk.claims.directives && readDirectiveLine(opener) !== undefined) || (walk.claims.pipeTables && claimsPipeLine(opener)) || isThematicBreak(opener)) return false
return atxHeading(opener) === undefined && openingCodeFence(opener) === undefined && openingHtmlBlock(opener, true) === undefined
return !opensBlock(walk.claims, opener)
}
// Every leaf a line can open but a paragraph, a setext underline and indented code.
function opensBlock(claims: Claims, opener: string): boolean {
if ((claims.directives && readDirectiveLine(opener) !== undefined) || (claims.pipeTables !== false && claimsPipeLine(opener)) || isThematicBreak(opener)) return true
return atxHeading(opener) !== undefined || openingCodeFence(opener) !== undefined || openingHtmlBlock(opener, true) !== undefined
}
function readBlockLine(walk: Walk, line: Line): void {
@@ -371,7 +376,7 @@ function readBlockLine(walk: Walk, line: Line): void {
return
}
if (leaf?.kind === 'pipe-table') {
const cells = pipeCells(removeColumns(line, largestOpenerIndentation).text)
const cells = pipeTableRow(walk.claims, removeColumns(line, largestOpenerIndentation).text)
if (cells !== undefined) {
leaf.rows.push(cells)
return
@@ -414,7 +419,8 @@ function openLeaf(walk: Walk, line: Line): void {
else pushFault(walk, directive.fault)
return
}
const cells = walk.claims.pipeTables ? pipeCells(opener) : undefined
if (openGfmTable(walk, opener)) return
const cells = walk.claims.pipeTables === false ? undefined : pipeCells(opener)
if (cells !== undefined) {
closeLeaf(walk)
walk.leaf = { kind: 'pipe-table', position: walk.position, rows: [cells] }
@@ -482,16 +488,38 @@ function closeLeaf(walk: Walk): void {
walk.leaf = undefined
const { position } = leaf
if (leaf.kind === 'html') currentBlocks(walk).push({ construct: leaf.construct, kind: 'html', position })
else if (leaf.kind === 'pipe-table') currentBlocks(walk).push(pipeTableBlock(leaf.rows, position))
else if (leaf.kind === 'pipe-table') currentBlocks(walk).push(pipeTableBlock(leaf.rows, position, walk.claims))
else currentBlocks(walk).push({ kind: 'code', language: leaf.kind === 'fenced-code' ? decodeTextEscapes(leaf.info) : '', position, text: leaf.lines.join('\n') })
}
function pipeTableBlock(rows: readonly [string[], ...string[][]], position: SourcePosition): Block {
// GFM, Tables: a paragraph's last line heads a table where a delimiter row of its cell count follows.
function openGfmTable(walk: Walk, opener: string): boolean {
const paragraph = walk.leaf
if (walk.claims.pipeTables !== 'gfm' || paragraph?.kind !== 'paragraph') return false
const delimiter = pipeCells(opener) ?? barePipeCells(opener)
const header = barePipeCells(paragraph.lines.at(-1) ?? '')
if (delimiter === undefined || header?.length !== delimiter.length || !isDelimiterRow(delimiter)) return false
paragraph.lines.pop()
const position = paragraph.positions.pop() ?? walk.position
if (paragraph.lines.length === 0) walk.leaf = undefined
else closeLeaf(walk)
walk.leaf = { kind: 'pipe-table', position, rows: [header, delimiter] }
return true
}
// GFM, Tables: a row without its leading pipe goes on until a line opens another block.
function pipeTableRow(claims: Claims, line: string): string[] | undefined {
const cells = pipeCells(line)
if (cells !== undefined || claims.pipeTables !== 'gfm' || opensBlock(claims, line)) return cells
return barePipeCells(line)
}
function pipeTableBlock(rows: readonly [string[], ...string[][]], position: SourcePosition, claims: Claims): Block {
const [header, delimiter, ...body] = rows
if (delimiter !== undefined && delimiter.some(isPipeAlignment)) {
if (claims.pipeTables !== 'gfm' && delimiter !== undefined && delimiter.some(isPipeAlignment)) {
return faultedBlock('a pipe table carries no column alignment ADF could hold: this delimiter row holds an alignment colon', position)
}
if (delimiter === undefined || !delimiter.every(isPipeDelimiter)) {
if (delimiter === undefined || !isDelimiterRow(delimiter)) {
return faultedBlock('a pipe table underlines its header with a row of `-` runs: this one has none; \\| at the start of every row keeps them literal text', position)
}
const ragged = [delimiter, ...body].find((row) => row.length !== header.length)
@@ -501,7 +529,7 @@ function pipeTableBlock(rows: readonly [string[], ...string[][]], position: Sour
// spec/flavour.md, Tables: GFM's table without the leading pipes, which no line of it claims.
function bareTableFault(paragraph: Extract<Block, { kind: 'paragraph' }>, claims: Claims): Block | undefined {
if (!claims.pipeTables) return undefined
if (claims.pipeTables !== 'flavour') return undefined
let header: string[] | undefined
for (const line of paragraph.text.split('\n')) {
const cells = barePipeCells(line)
@@ -278,6 +278,23 @@ test('reads text the writer kept from reading as a marker back as text', () => {
assert.deepEqual(roundTripped(bare('taskList', task('DONE', text('[x] ==a==')))), [bare('taskList', task('DONE', text('[x] ==a==')))])
})
test('reads GFM\'s pipe table without its leading pipes or with alignment colons, dropping the alignment', () => {
const cell = (type: string, value: string): AdfNode => bare(type, said(value))
const table = bare('table', bare('tableRow', cell('tableHeader', 'a'), cell('tableHeader', 'b')), bare('tableRow', cell('tableCell', 'c'), cell('tableCell', 'd')))
assert.deepEqual(read('a | b\n--- | ---\nc | d\n'), [table])
assert.deepEqual(read('| a | b |\n| :--- | ---: |\n| c | d |\n'), [table])
assert.deepEqual(read('a | b\n:---: | ---:\n| c | d |\n'), [table])
assert.deepEqual(read('| a | b |\n--- | ---\nc | d |\n'), [table])
assert.deepEqual(read('a | b\n| --- | --- |\nc | d\n'), [table])
assert.deepEqual(read('Intro.\na | b\n--- | ---\nc | d\n\nAfter.\n'), [said('Intro.'), table, said('After.')])
assert.deepEqual(read('> a | b\n> --- | ---\n> c | d\n'), [bare('blockquote', table)])
assert.deepEqual(read('a | b\n--- | ---\nc | d\nAfter.\n'), [table, said('After.')])
assert.deepEqual(read('a | b\n--- | ---\nc | d\n# h | i\n'), [table, { attrs: { level: 1 }, content: [text('h | i')], type: 'heading' }])
assert.deepEqual(read('a | b\n--- | --- | ---\n'), [said('a | b --- | --- | ---')])
assert.deepEqual(read('- a | b\n--- | ---\n'), [bare('bulletList', bare('listItem', said('a | b --- | ---')))])
assert.deepEqual(read('a | b\n--- | ---\nc\n'), [bare('table', table.content?.[0] ?? said('')), said('c')])
})
test('reads what CommonMark reads where no row reads, a directive and a carry fence among it, an image sharing its paragraph with a marker among it', () => {
assert.deepEqual(read('!adf:panel\n'), [said('!adf:panel')])
assert.deepEqual(read('a !adf:carry{json="{}"} \\!adf:x\n'), [said('a !adf:carry{json="{}"} !adf:x')])
+1 -1
View File
@@ -23,7 +23,7 @@ export function isPipeAlignment(cell: string): boolean {
return alignmentCell.test(cell)
}
export function isPipeDelimiter(cell: string): boolean {
function isPipeDelimiter(cell: string): boolean {
return delimiterCell.test(cell)
}
+5 -4
View File
@@ -10,20 +10,21 @@ export type Claims = {
carryFence: boolean
directives: boolean
highlights: boolean
pipeTables: boolean
// `gfm` reads GFM's whole pipe table, its alignment dropped; `flavour` reads rows opening with `|` over a `-` delimiter row.
pipeTables: 'flavour' | 'gfm' | false
strikethrough: boolean
taskMarkers: boolean
}
type WrittenClaims = Claims & { pipeTables: true; strikethrough: true }
type WrittenClaims = Claims & { pipeTables: 'flavour' | 'gfm'; strikethrough: true }
// Directives exclude alerts and highlights: an expand directive leaves `inExpand` unset and a directive's slot reads `==`.
type ReadClaims = Claims & ({ directives: false } | { alerts: false; directives: true; highlights: false })
export const flavourClaims: Readonly<{ commonmark: ReadClaims } & Record<WrittenFlavour, ReadClaims & WrittenClaims>> = {
commonmark: { alerts: false, carryFence: false, directives: false, highlights: false, pipeTables: false, strikethrough: false, taskMarkers: false },
lossless: { alerts: false, carryFence: true, directives: true, highlights: false, pipeTables: true, strikethrough: true, taskMarkers: false },
portable: { alerts: true, carryFence: false, directives: false, highlights: true, pipeTables: true, strikethrough: true, taskMarkers: true },
lossless: { alerts: false, carryFence: true, directives: true, highlights: false, pipeTables: 'flavour', strikethrough: true, taskMarkers: false },
portable: { alerts: true, carryFence: false, directives: false, highlights: true, pipeTables: 'gfm', strikethrough: true, taskMarkers: true },
}
type AlertMarker = { folded: boolean; length: number; panelType: string }