From 5c77d18a138460a6c6e6e3930ade13ee1b89f5b2 Mon Sep 17 00:00:00 2001 From: Lilleman auf Larv Date: Thu, 19 Nov 2026 17:25:01 +0100 Subject: [PATCH] Drop the id checks and defaults no input reaches --- src/markdown/emit/adf-to-markdown.ts | 33 ++++++++++++++------------- src/markdown/parse/markdown-to-adf.ts | 6 ++--- 2 files changed, 20 insertions(+), 19 deletions(-) diff --git a/src/markdown/emit/adf-to-markdown.ts b/src/markdown/emit/adf-to-markdown.ts index 984758a..2d56b86 100644 --- a/src/markdown/emit/adf-to-markdown.ts +++ b/src/markdown/emit/adf-to-markdown.ts @@ -169,7 +169,7 @@ const readableSpellings: ReadonlyMap = new Map 'each line behind `> `', spell: (node, path, depth, writing) => { - const id = markedId(node, [], writing.flavour) + const id = markedId(node) return id === undefined ? undefined : quotedBlocks(node, path, depth, writing, (body) => alertEscaped(body, writing), id) }, }], @@ -191,7 +191,7 @@ const readableSpellings: ReadonlyMap = new Map 'its text alone', spell: (node, path, _depth, writing) => tryParagraph(node, path, writing.flavour) }], - ['rule', { describe: () => '`---`', spell: (node, _path, _depth, writing) => readableLines(tryRule(node, writing.flavour)) }], + ['rule', { describe: () => '`---`', spell: (node) => readableLines(tryRule(node)) }], ['table', { describe: () => 'a pipe table', spell: (node, path, _depth, writing) => readableText(tryPipeTable(node, path, writing.flavour)) }], ]) @@ -252,20 +252,21 @@ function alertEscaped(body: BlockText, writing: Writing): BlockText { } // Quote spellers call this directly, never through a wrapper: each frame a quote level adds lowers the depth the stack holds (docs/decisions.md §Nothing recurses unbounded). -export function quotedBlocks(node: AdfNode, path: ConvertErrorPath, depth: number, writing: Writing, bodyText: (body: BlockText) => BlockText, id: MarkedId = { id: undefined }): Result { +export function quotedBlocks(node: AdfNode, path: ConvertErrorPath, depth: number, writing: Writing, bodyText: (body: BlockText) => BlockText, id: MarkedId = {}): Result { const inner = walkBlocks(nodeContent(node), path, depth + 1, writing) if (!inner.ok) return inner return success(commonMarkText(markedLines(id, quotedText(bodyText(joinBlocks(inner.value.blocks, 'quote'))), false), inner.value.headroom)) } // The id a marker carries beside a CommonMark spelling holding the attributes `spelled` names; `undefined` where the node holds another attribute, or an id no marker carries. -type MarkedId = { id: string | undefined } +type MarkedId = { id?: string } -function markedId(node: AdfNode, spelled: readonly string[], flavour: WrittenFlavour): MarkedId | undefined { +// The portable reduction drops every id, so only the lossless writer meets one. +function markedId(node: AdfNode, ...spelled: string[]): MarkedId | undefined { if (!holdsOnlyAttributes(node, [...spelled, idKey])) return undefined const id = nodeAttrs(node)[idKey] - if (id === undefined) return { id } - return typeof id === 'string' && flavour === 'lossless' ? { id } : undefined + if (id === undefined) return {} + return typeof id === 'string' ? { id } : undefined } // spec/flavour.md, The CommonMark blocks: the marker on the line above, a blank line parting it from lines CommonMark would otherwise read with it. @@ -280,7 +281,7 @@ function trailingMarker(marked: MarkedId): string { } function tryCodeBlock(node: AdfNode, flavour: WrittenFlavour): BlockText | undefined { - const id = markedId(node, ['language'], flavour) + const id = markedId(node, 'language') if (id === undefined) return undefined const slot = languageSlot(nodeAttrs(node)['language'], flavourClaims[flavour]) if (slot.kind === 'attribute') return undefined @@ -313,7 +314,7 @@ function fencedTexts(node: AdfNode): string[] | undefined { } function tryHeading(node: AdfNode, path: ConvertErrorPath, flavour: WrittenFlavour): Result | undefined { - const id = markedId(node, ['level'], flavour) + const id = markedId(node, 'level') const level = nodeAttrs(node)['level'] if (id === undefined || !isHeadingLevel(level)) return undefined const hashes = '#'.repeat(level) @@ -326,7 +327,7 @@ function tryHeading(node: AdfNode, path: ConvertErrorPath, flavour: WrittenFlavo function tryList(node: AdfNode, path: ConvertErrorPath, depth: number, writing: Writing): Result | undefined { const ordered = node.type === 'orderedList' - const id = markedId(node, ordered ? ['order'] : [], writing.flavour) + const id = markedId(node, ...(ordered ? ['order'] : [])) if (id === undefined) return undefined const items = nodeContent(node) const start = listStart(node, items.length) @@ -334,7 +335,7 @@ function tryList(node: AdfNode, path: ConvertErrorPath, depth: number, writing: // Every item is checked before any is walked: a list giving way to its directive walks them again. const marked: { id: MarkedId; node: AdfNode }[] = [] for (const item of items) { - const itemId = item.type === 'listItem' ? markedId(item, [], writing.flavour) : undefined + const itemId = item.type === 'listItem' ? markedId(item) : undefined if (itemId === undefined) return undefined marked.push({ id: itemId, node: item }) } @@ -362,11 +363,11 @@ function holdsItemMarker(item: WalkedItem): boolean { const [first] = item.walk.blocks if (first?.node.type === 'paragraph' && first.spelling === 'commonmark') return true if (item.id.id !== undefined) return false - return first === undefined || first.spelling === 'directive' || !opensWithMarker(first.text) + return first === undefined || !opensWithMarker(first.text) } function opensWithMarker(text: BlockText): boolean { - return text.first.character === '!' && text.first.text.startsWith(markerOpener) + return text.first.text.startsWith(markerOpener) } function markedList(id: MarkedId, list: Extract): EmittedBlock { @@ -419,7 +420,7 @@ function tryListItemLines(inner: BlockText, marker: string, id: string | undefin function tryParagraph(node: AdfNode, path: ConvertErrorPath, flavour: WrittenFlavour): Result | undefined { const content = nodeContent(node) - const id = markedId(node, [], flavour) + const id = markedId(node) if (content.length === 0 || id === undefined) return undefined const line = emitInlineLine(content, 'paragraph', path, flavour) if (!line.ok) return line @@ -427,7 +428,7 @@ function tryParagraph(node: AdfNode, path: ConvertErrorPath, flavour: WrittenFla return success(id.id === undefined ? text : { ...text, trailing: blockText(`${line.value}${trailingMarker(id)}`) }) } -function tryRule(node: AdfNode, flavour: WrittenFlavour): BlockText | undefined { - const id = markedId(node, [], flavour) +function tryRule(node: AdfNode): BlockText | undefined { + const id = markedId(node) return id === undefined ? undefined : markedLines(id, blockText('---'), true) } diff --git a/src/markdown/parse/markdown-to-adf.ts b/src/markdown/parse/markdown-to-adf.ts index fc64cdf..573fa2d 100644 --- a/src/markdown/parse/markdown-to-adf.ts +++ b/src/markdown/parse/markdown-to-adf.ts @@ -32,7 +32,7 @@ export type FlavourBlockReaders = { export type FlavourReading = Extract // trailing: the container's first paragraph ends its last line with its id, the line above it being the container's. -type Opening = 'line' | 'trailing' +type Opening = 'trailing' export type ReadAs = { flavour: 'commonmark' | 'lossless' } | { blocks: FlavourBlockReaders; flavour: 'portable' } @@ -66,7 +66,7 @@ function readMarkdown(markdown: string, readAs: ReadAs): Result { return content.ok ? success({ content: content.value, type: 'doc', version: 1 }) : content } -export function readBlocks(blocks: readonly Block[], reading: Reading, path: ConvertErrorPath, depth: number, opening: Opening = 'line'): Result { +export function readBlocks(blocks: readonly Block[], reading: Reading, path: ConvertErrorPath, depth: number, opening?: Opening): Result { if (depth > largestNesting) return tooDeep(path) const content: AdfNode[] = [] let marker: MarkerBlock | undefined @@ -272,7 +272,7 @@ function inlineBodyNode(node: AdfNode, blocks: readonly Block[], reading: Readin return positioned(contentNode(node, only.text, reading, path, 'paragraph'), only.position) } -export function containerNode(node: AdfNode, blocks: readonly Block[], reading: Reading, path: ConvertErrorPath, depth: number, opening: Opening = 'line'): Result { +export function containerNode(node: AdfNode, blocks: readonly Block[], reading: Reading, path: ConvertErrorPath, depth: number, opening?: Opening): Result { const content = readBlocks(blocks, reading, path, depth + 1, opening) if (!content.ok) return content return success(withContent(node, content.value))