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

Read a GFM pipe table's short rows, rows without a pipe and one column in portableMarkdownToAdf

This commit is contained in:
2026-11-09 11:27:40 +01:00
committed by Lillem4n
parent 429ec8ccb8
commit db2c395d05
27 changed files with 110 additions and 38 deletions
+1
View File
@@ -0,0 +1 @@
import '../html/emit/adf-to-html.ts';
+1
View File
@@ -0,0 +1 @@
import '../markdown/flavour.ts';
+1
View File
@@ -0,0 +1 @@
import '../markdown/flavour.ts';
@@ -0,0 +1 @@
import '../../markdown/flavour.ts';
+1
View File
@@ -0,0 +1 @@
import '../emit/adf-to-html.ts';
+1
View File
@@ -0,0 +1 @@
import '../html/emit/adf-to-html.ts';
@@ -0,0 +1 @@
import '../emit/image.ts';
@@ -0,0 +1 @@
import '../../html/emit/adf-to-html.ts';
+1
View File
@@ -0,0 +1 @@
import '../parse/blocks.ts';
+1
View File
@@ -0,0 +1 @@
import '../../html/emit/adf-to-html.ts';
+1
View File
@@ -0,0 +1 @@
import '../portable/conventions.ts';
+2
View File
@@ -34,6 +34,8 @@ test('escapes the delimiter row a hard break leaves opening a pipe table with no
assert.equal(broken('--- | ---'), 'a | b\\\n\\--- | ---\n')
assert.equal(broken(':--- | ---:'), 'a | b\\\n\\:--- | ---:\n')
assert.equal(broken('c | d'), 'a | b\\\nc | d\n')
const oneColumn = markdown(adfToLosslessMarkdown(document(paragraph({ text: 'a |', type: 'text' }, { type: 'hardBreak' }, { text: '--- |', type: 'text' }))))
assert.equal(oneColumn, 'a |\\\n\\--- |\n')
})
test('escapes only text that would otherwise open a construct', () => {
+8 -5
View File
@@ -18,6 +18,7 @@ import {
setextHeadingLevel,
thematicBreakTail,
type ThematicBreakTail,
trimSpace,
} from '../commonmark/grammar.ts'
import { barePipeCells, isDelimiterRow, isPipeAlignment, malformedPipeTable, pipeCells } from '../pipe-table-syntax.ts'
import { blockDirectiveForm } from '../directive/block.ts'
@@ -532,11 +533,11 @@ function openGfmTable(walk: Walk, opener: string): boolean {
return true
}
// GFM, Tables: a row without its leading `|` continues the table while it holds a `|` and opens no other block.
// GFM, Tables: a row without its leading `|` continues the table until a blank line or a line opening another block; a line holding no `|` is one cell.
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)
if (cells !== undefined || claims.pipeTables !== 'gfm' || isBlankLine(line) || opensBlock(claims, line)) return cells
return barePipeCells(line) ?? [trimSpace(line)]
}
function pipeTableBlock(rows: readonly [string[], ...string[][]], position: SourcePosition, claims: Claims): Block {
@@ -547,9 +548,11 @@ function pipeTableBlock(rows: readonly [string[], ...string[][]], position: Sour
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)
const padsShortRows = claims.pipeTables === 'gfm'
const ragged = [delimiter, ...body].find((row) => (padsShortRows && row !== delimiter ? row.length > header.length : row.length !== header.length))
if (ragged !== undefined) return faultedBlock(`a pipe table row holds ${cellCount(ragged.length)} where its header holds ${cellCount(header.length)}`, position)
return { kind: 'table', position, rows: [header, ...body] }
const padded = body.map((row) => [...row, ...Array.from({ length: header.length - row.length }, () => '')])
return { kind: 'table', position, rows: [header, ...padded] }
}
// spec/flavour.md, Tables: GFM's table without the leading pipes, which no line of it claims.
@@ -0,0 +1 @@
import '../emit/spelling-questions.ts';
@@ -0,0 +1 @@
import '../../html/emit/adf-to-html.ts';
@@ -0,0 +1 @@
import '../portable/conventions.ts';
+1
View File
@@ -0,0 +1 @@
import '../emit/adf-to-markdown.ts';
@@ -259,6 +259,9 @@ test('names the pipe table whose rows open with no pipe', () => {
assert.equal(content(losslessMarkdownToAdf('Intro.\na | b\n--- | ---\n')), bare)
assert.equal(content(losslessMarkdownToAdf('a | b\n--- | ---\n===\n')), bare)
assert.equal(content(losslessMarkdownToAdf('a | b\n:--- | ---:\n')), bare)
assert.equal(content(losslessMarkdownToAdf('a |\n--- |\n')), bare)
assert.deepEqual(content(losslessMarkdownToAdf('| a | b |\n| --- | --- |\nc\n')), [table(row(cell('tableHeader', text('a')), cell('tableHeader', text('b')))), paragraph('c')])
assert.equal(content(losslessMarkdownToAdf('| a | b |\n| --- | --- |\n| c |\n')), 'malformed-pipe-table: a pipe table row holds 1 cell where its header holds 2 cells')
assert.deepEqual(content(losslessMarkdownToAdf('a | b\nc | d\n')), [paragraph('a | b c | d')])
assert.deepEqual(content(losslessMarkdownToAdf('a | b\n--- | --- | ---\n')), [paragraph('a | b --- | --- | ---')])
assert.deepEqual(content(losslessMarkdownToAdf('a \\| b\n--- | ---\n')), [paragraph('a | b --- | ---')])
+7 -3
View File
@@ -4,10 +4,10 @@ import { backslashEscape, claimsPipeLine, trimSpace } from './commonmark/grammar
const alignmentCell = /^:-+:?$|^-+:$/
const delimiterCell = /^-+$/
// spec/flavour.md, Tables: the cells of a row no leading `|` claimed — GFM's form without the outer pipes.
// spec/flavour.md, Tables: the cells of a row no leading `|` claimed — GFM's form without the outer pipes, one `|` making a one-column row.
export function barePipeCells(line: string): string[] | undefined {
const cells = splitPipeCells(line, 0)
return cells.length > 1 ? cells : undefined
return cells.length > 1 ? withoutClosingCell(cells) : undefined
}
export function isBareDelimiterRow(line: string): boolean {
@@ -33,7 +33,7 @@ export function malformedPipeTable(message: string): ConvertFault {
// spec/flavour.md, Tables: the cells of a claimed row, the closing `|` the spelling writes optional here.
export function pipeCells(line: string): string[] | undefined {
return claimsPipeLine(line) ? splitPipeCells(line, 1) : undefined
return claimsPipeLine(line) ? withoutClosingCell(splitPipeCells(line, 1)) : undefined
}
export function spellPipeDelimiter(columns: number): string {
@@ -60,6 +60,10 @@ function splitPipeCells(line: string, start: number): string[] {
index += 1
}
cells.push(trimSpace(line.slice(cellStart)))
return cells
}
function withoutClosingCell(cells: string[]): string[] {
if (cells.length > 1 && cells.at(-1) === '') cells.pop()
return cells
}
@@ -378,11 +378,9 @@ test('reads GFM\'s pipe table without its leading pipes or with alignment colons
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')])
assert.deepEqual(read('a | b\nc | d\n'), [said('a | b c | d')])
assert.deepEqual(read('| a | b |\n| --- | --- |\n | c | d |\n'), [bare('table', table.content?.[0] ?? said('')), bare('codeBlock', text('| c | d |'))])
assert.deepEqual(read('a | b\n--- | ---\n c | d\n'), [bare('table', table.content?.[0] ?? said('')), bare('codeBlock', text('c | d'))])
@@ -391,6 +389,27 @@ test('reads GFM\'s pipe table without its leading pipes or with alignment colons
assert.deepEqual(ragged.ok ? undefined : [ragged.error.code, ragged.error.position], ['malformed-pipe-table', { line: 2, offset: 7 }])
})
test('reads GFM\'s short row padded with empty cells, a line holding no pipe as a row, and a one-column table', () => {
const cell = (type: string, value: string): AdfNode => bare(type, value === '' ? { type: 'paragraph' } : said(value))
const header = bare('tableRow', cell('tableHeader', 'a'), cell('tableHeader', 'b'))
const short = bare('table', header, bare('tableRow', cell('tableCell', 'c'), cell('tableCell', '')))
assert.deepEqual(read('a | b\n--- | ---\nc\n'), [short])
assert.deepEqual(read('| a | b |\n| --- | --- |\n| c |\n'), [short])
assert.deepEqual(read('a | b\n--- | ---\nc | d\nAfter.\n'), [
bare('table', header, bare('tableRow', cell('tableCell', 'c'), cell('tableCell', 'd')), bare('tableRow', cell('tableCell', 'After.'), cell('tableCell', ''))),
])
assert.deepEqual(read('a | b\n--- | ---\nc\n\nAfter.\n'), [short, said('After.')])
assert.deepEqual(read('a | b\n--- | ---\nc\n# h\n'), [short, { attrs: { level: 1 }, content: [text('h')], type: 'heading' }])
const column = (rows: readonly string[]): AdfNode =>
bare('table', ...rows.map((value, index) => bare('tableRow', cell(index === 0 ? 'tableHeader' : 'tableCell', value))))
assert.deepEqual(read('a |\n--- |\nb |\n'), [column(['a', 'b'])])
assert.deepEqual(read('a |\n:--- |\nb\n'), [column(['a', 'b'])])
assert.deepEqual(read('| a |\n| --- |\nb\n'), [column(['a', 'b'])])
assert.deepEqual(read('a\n--- |\n'), [said('a --- |')])
const long = portableMarkdownToAdf('a | b\n--- | ---\nc | d | e\n')
assert.deepEqual(long.ok ? undefined : long.error.code, 'malformed-pipe-table')
})
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')])