Write the same plain markdown for documents the editor holds equal: normalize the reduction's input again, naming a refusal by the caller's document
This commit is contained in:
@@ -338,3 +338,9 @@ test('keeps the nodes the plain flavour spells and degrades only what it cannot'
|
||||
const reduced = reduceToPlain(document(node('panel', { localId: '01a0d99b-1f56-7a50-889a-f4375f09ee05', panelType: 'info' }, said('x')), tasks))
|
||||
assert.deepEqual(reduced.ok ? reduced.value : undefined, document(node('panel', { panelType: 'info' }, said('x')), { content: [node('taskItem', { state: 'DONE' }, text('t'))], type: 'taskList' }))
|
||||
})
|
||||
|
||||
test('writes what the editor-normal form of the document writes', () => {
|
||||
assert.equal(plain({ attrs: { order: -0 }, content: [{ content: [], type: 'listItem' }], type: 'orderedList' }), '0.\n')
|
||||
assert.equal(plain({ content: [], text: 'hi', type: 'futureInline' }), 'hi\n')
|
||||
assert.equal(plain(paragraph({ marks: [{ attrs: {}, type: 'code' }], text: 'a', type: 'text' }, { marks: [{ type: 'code' }], text: '|b', type: 'text' })), '`a|b`\n')
|
||||
})
|
||||
|
||||
@@ -51,9 +51,14 @@ export function reduceToPlain(document: AdfDocument): Result<AdfDocument> {
|
||||
const fault = adfDocumentFault(document)
|
||||
if (fault !== undefined) return faulted(fault, [])
|
||||
if (document.version !== 1) return failure('unsupported-document-version', `no markdown spelling carries ADF version ${document.version}`, [])
|
||||
// A refusal's path indexes the caller's document, so only the reduction's output normalizes (docs/decisions.md §Equality is deep).
|
||||
const blocks = reduceBlocks(nodeContent(document), { depth: 0, memo: new Map(), path: [] })
|
||||
return blocks.ok ? success({ content: nodeContent(toEditorNormal({ content: blocks.value, type: 'doc', version: 1 })).slice(), type: 'doc', version: 1 }) : blocks
|
||||
// docs/decisions.md §Equality is deep: the reduction reads and writes editor-normal ADF.
|
||||
const blocks = reduceBlocks(nodeContent(toEditorNormal(document)), { depth: 0, memo: new Map(), path: [] })
|
||||
if (!blocks.ok) {
|
||||
// Merging text renumbers siblings, so the caller's document names the refusal's path.
|
||||
const raw = reduceBlocks(nodeContent(document), { depth: 0, memo: new Map(), path: [] })
|
||||
return raw.ok ? blocks : raw
|
||||
}
|
||||
return success({ content: nodeContent(toEditorNormal({ content: blocks.value, type: 'doc', version: 1 })).slice(), type: 'doc', version: 1 })
|
||||
}
|
||||
|
||||
function reduceBlocks(nodes: readonly AdfNode[], reduction: Reduction): Result<AdfNode[]> {
|
||||
@@ -86,7 +91,7 @@ function reduceStanding(node: AdfNode, reduction: Reduction): Result<AdfNode[]>
|
||||
|
||||
function standsInline(node: AdfNode): boolean {
|
||||
if (node.type === 'text' || inlineNodeModel(node.type) !== undefined) return true
|
||||
return !isBlockNodeType(node.type) && node.content === undefined
|
||||
return !isBlockNodeType(node.type) && nodeContent(node).length === 0
|
||||
}
|
||||
|
||||
function reduceBody(node: AdfNode, reduction: Reduction): Result<AdfNode[]> {
|
||||
|
||||
Reference in New Issue
Block a user