mirror of
https://github.com/larvit/adf-codec.git
synced 2026-11-19 14:45:44 +01:00
Delete the unreached task-marker paragraph in taskBlocks; check the README error tables against the codes
This commit is contained in:
@@ -158,6 +158,30 @@ test('spells a task list as a bullet list whose items lead with their state', ()
|
||||
assert.equal(portable(node('taskList', {}, task('DONE', 'a'), node('taskList', {}, task('TODO', '')))), '- [x] a\n - [ ]\n')
|
||||
})
|
||||
|
||||
test('leaves a block task item only when it opens with a paragraph', () => {
|
||||
const task = (state: string, value: string): AdfNode => node('taskItem', { state }, text(value))
|
||||
const blockTask = (...content: AdfNode[]): AdfNode => node('blockTaskItem', { state: 'TODO' }, ...content)
|
||||
const codeBlock: AdfNode = { content: [text('x')], type: 'codeBlock' }
|
||||
const nested = node('taskList', {}, task('TODO', 'n'))
|
||||
const lists = [
|
||||
node('taskList', {}, blockTask(said('a'), said('b')), blockTask(codeBlock, said('c')), blockTask(bulletList(item(said('d'))), said('e'))),
|
||||
node('taskList', {}, blockTask(nested, said('f')), blockTask(said('g'), codeBlock, said('h')), blockTask(), blockTask(said('i'))),
|
||||
node('taskList', {}, blockTask(said('j'), nested, said('k'), nested), blockTask(said('l'), said('m'), nested)),
|
||||
]
|
||||
const reduced = reduceToPortable(document(...lists))
|
||||
assert.ok(reduced.ok)
|
||||
const pending = [...(reduced.value.content ?? [])]
|
||||
let blockTasks = 0
|
||||
for (let at = pending.pop(); at !== undefined; at = pending.pop()) {
|
||||
for (const child of at.content ?? []) pending.push(child)
|
||||
if (at.type !== 'blockTaskItem') continue
|
||||
blockTasks += 1
|
||||
assert.equal(at.content?.[0]?.type, 'paragraph')
|
||||
assert.ok((at.content ?? []).filter((child) => child.type === 'paragraph').length >= 2)
|
||||
}
|
||||
assert.equal(blockTasks, 2)
|
||||
})
|
||||
|
||||
test('spells a decision list as a plain bullet list', () => {
|
||||
assert.equal(portable(node('decisionList', {}, node('decisionItem', { state: 'DECIDED' }, text('Ship')), said('Stray'))), '- Ship\n- Stray\n')
|
||||
})
|
||||
|
||||
@@ -65,19 +65,17 @@ function placedBlock(node: AdfNode, path: ConvertErrorPath, depth: number, writi
|
||||
return block.ok ? success({ blocks: [{ ...block.value, node }], headroom: block.value.headroom }) : block
|
||||
}
|
||||
|
||||
// The marker leads the first paragraph, or stands as one where the blocks open with another.
|
||||
// A block task item reaches here holding two or more paragraphs, so the marker leads the first (`itemParts` in task-list.ts).
|
||||
function taskBlocks(task: AdfNode, path: ConvertErrorPath, depth: number, writing: Writing): Result<PlacedBlocks> {
|
||||
const marker = taskMarker(nodeAttrs(task)['state'])
|
||||
const markerBlock: PlacedBlock = { node: { type: 'paragraph' }, spelling: 'commonmark', text: blockText(marker) }
|
||||
if (task.type === 'taskItem') {
|
||||
const content = nodeContent(task)
|
||||
const line = content.length === 0 ? success('') : emitInlineLine(content, 'paragraph', path, writing.flavour)
|
||||
if (!line.ok) return line
|
||||
return success({ blocks: [{ ...markerBlock, text: blockText(line.value === '' ? marker : `${marker} ${line.value}`) }], headroom: Number.POSITIVE_INFINITY })
|
||||
return success({ blocks: [{ node: { type: 'paragraph' }, spelling: 'commonmark', text: blockText(line.value === '' ? marker : `${marker} ${line.value}`) }], headroom: Number.POSITIVE_INFINITY })
|
||||
}
|
||||
const walk = walkBlocks(nodeContent(task), path, depth, writing)
|
||||
if (!walk.ok) return walk
|
||||
const [first, ...rest] = walk.value.blocks
|
||||
const blocks = first?.node.type === 'paragraph' ? [{ ...first, text: prefixedText(`${marker} `, '', first.text) }, ...rest] : [markerBlock, ...walk.value.blocks]
|
||||
const blocks = walk.value.blocks.map((block, index) => (index === 0 ? { ...block, text: prefixedText(`${marker} `, '', block.text) } : block))
|
||||
return success({ blocks, headroom: walk.value.headroom })
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user