5b3: merge the code list's duplicate causes, spell the list separator, claim the bare pipe table
This commit is contained in:
@@ -4,6 +4,21 @@ import { backslashEscape, claimsPipeLine, trimSpace } from './commonmark-grammar
|
||||
const alignmentCell = /^:-+:?$|^-+:$/
|
||||
const delimiterCell = /^-+$/
|
||||
|
||||
// spec/flavour.md, Tables: the cells of a row the leading `|` no claim read, GFM's form without it.
|
||||
export function barePipeCells(line: string): string[] | undefined {
|
||||
const cells = splitPipeCells(line, 0)
|
||||
return cells.length > 1 ? cells : undefined
|
||||
}
|
||||
|
||||
export function isBareDelimiterRow(line: string): boolean {
|
||||
const cells = barePipeCells(line)
|
||||
return cells !== undefined && isDelimiterRow(cells)
|
||||
}
|
||||
|
||||
export function isDelimiterRow(cells: readonly string[]): boolean {
|
||||
return cells.every((cell) => isPipeDelimiter(cell) || isPipeAlignment(cell))
|
||||
}
|
||||
|
||||
export function isPipeAlignment(cell: string): boolean {
|
||||
return alignmentCell.test(cell)
|
||||
}
|
||||
@@ -18,24 +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 {
|
||||
if (!claimsPipeLine(line)) return undefined
|
||||
const cells: string[] = []
|
||||
let start = 1
|
||||
let index = 1
|
||||
while (index < line.length) {
|
||||
if (backslashEscape(line, index) !== undefined) {
|
||||
index += 2
|
||||
continue
|
||||
}
|
||||
if (line.charAt(index) === '|') {
|
||||
cells.push(trimSpace(line.slice(start, index)))
|
||||
start = index + 1
|
||||
}
|
||||
index += 1
|
||||
}
|
||||
cells.push(trimSpace(line.slice(start)))
|
||||
if (cells.length > 1 && cells.at(-1) === '') cells.pop()
|
||||
return cells
|
||||
return claimsPipeLine(line) ? splitPipeCells(line, 1) : undefined
|
||||
}
|
||||
|
||||
export function spellPipeDelimiter(columns: number): string {
|
||||
@@ -45,3 +43,23 @@ export function spellPipeDelimiter(columns: number): string {
|
||||
export function spellPipeRow(cells: readonly string[]): string {
|
||||
return `| ${cells.join(' | ')} |`
|
||||
}
|
||||
|
||||
function splitPipeCells(line: string, start: number): string[] {
|
||||
const cells: string[] = []
|
||||
let cellStart = start
|
||||
let index = start
|
||||
while (index < line.length) {
|
||||
if (backslashEscape(line, index) !== undefined) {
|
||||
index += 2
|
||||
continue
|
||||
}
|
||||
if (line.charAt(index) === '|') {
|
||||
cells.push(trimSpace(line.slice(cellStart, index)))
|
||||
cellStart = index + 1
|
||||
}
|
||||
index += 1
|
||||
}
|
||||
cells.push(trimSpace(line.slice(cellStart)))
|
||||
if (cells.length > 1 && cells.at(-1) === '') cells.pop()
|
||||
return cells
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user