3
0
mirror of https://github.com/larvit/adf-codec.git synced 2026-11-19 06:35:44 +01:00

Stand a block a task item cannot hold after its task list, in portableMarkdownToAdf and adfToPortableMarkdown

This commit is contained in:
2026-10-09 13:16:22 +02:00
committed by Lillem4n
parent 1870111427
commit 654a960888
10 changed files with 87 additions and 29 deletions
+5 -7
View File
@@ -8,7 +8,7 @@ import type { LinkDefinitions } from './inline-content.ts'
import { carryFenceType, readCarriedBlock } from '../opaque-carry.ts'
import { commonMarkSpelling, type SpellingMemo } from '../emit/adf-to-markdown.ts'
import { documentAttribute, documentName, documentSpelling, listBreakName, listBreakSpelling } from '../block-directive.ts'
import { emptyKeys, nodeAttrs, nodeContent } from '../../adf/document.ts'
import { emptyKeys, nodeAttrs } from '../../adf/document.ts'
import { failure, faulted, positioned, success, type ConvertErrorPath, type ParseError, type Result, type SourcePosition } from '../../result.ts'
import { flavourClaims, leadingMarker, readAlertMarker, readTaskMarker } from '../portable/conventions.ts'
import { languageSlot } from '../code-language.ts'
@@ -19,6 +19,7 @@ import { parseInlineContent } from './inline-content.ts'
import { readBlockDirectiveNode } from './directive-nodes.ts'
import { spellStringAttribute, unsupportedNodeShape } from '../directive-syntax.ts'
import { spellsEmpty } from '../empty-keys.ts'
import { splitTaskList } from '../portable/task-list.ts'
type Paragraph = Extract<Block, { kind: 'paragraph' }>
@@ -82,7 +83,8 @@ function readBlocks(blocks: readonly ReadBlock[], reading: Reading, path: Conver
}
const node = positioned(readBlock(block, reading, nodePath, depth), block.position)
if (!node.ok) return node
content.push(node.value)
if (node.value.type !== 'taskList' || block.kind !== 'bulletList') content.push(node.value)
else for (const split of splitTaskList(node.value)) content.push(split)
}
return success(content)
}
@@ -212,11 +214,7 @@ function bulletNode(items: readonly Block[][], reading: Reading, path: ConvertEr
if (!read.ok) return read
let beside = read.value.length
while (read.value[beside - 1]?.type === 'taskList') beside -= 1
const kept = read.value.slice(0, beside)
const [only] = kept
const attrs = { state: marker.state }
const inline = kept.length <= 1 && (only === undefined || only.type === 'paragraph')
tasks.push(inline ? { attrs, content: nodeContent(only ?? {}).slice(), type: 'taskItem' } : { attrs, content: kept, type: 'blockTaskItem' })
tasks.push({ attrs: { state: marker.state }, content: read.value.slice(0, beside), type: 'blockTaskItem' })
for (const nested of read.value.slice(beside)) tasks.push(nested)
}
return success({ content: tasks, type: 'taskList' })
@@ -145,12 +145,31 @@ test('reads a bullet list whose every item leads with a task marker to a task li
])
})
test('moves a nested task list beside its item and makes an item holding more than one block a block task item', () => {
test('moves a nested task list beside its item and makes an item holding more than one paragraph a block task item', () => {
assert.deepEqual(read('- [x] Parent\n - [ ] Child\n- [ ] Next\n'), [bare('taskList', task('DONE', text('Parent')), bare('taskList', task('TODO', text('Child'))), task('TODO', text('Next')))])
assert.deepEqual(read('- [x] First.\n\n Second.\n- [ ]\n\n ```\n x\n ```\n'), [
bare('taskList', node('blockTaskItem', { state: 'DONE' }, said('First.'), said('Second.')), node('blockTaskItem', { state: 'TODO' }, bare('codeBlock', text('x')))),
assert.deepEqual(read('- [x] First.\n\n Second.\n- [ ] Next\n'), [bare('taskList', node('blockTaskItem', { state: 'DONE' }, said('First.'), said('Second.')), task('TODO', text('Next')))])
})
test('stands a block a task item cannot hold, and what follows it in the item, after the task list, which resumes at the next task', () => {
assert.deepEqual(read('- [x] Deploy\n\n ![a](u)\n- [ ] Tell\n'), [
bare('taskList', task('DONE', text('Deploy'))),
node('mediaSingle', { layout: 'center' }, { attrs: { alt: 'a', type: 'external', url: 'u' }, type: 'media' }),
bare('taskList', task('TODO', text('Tell'))),
])
assert.deepEqual(read('- [x] First.\n\n Second.\n\n > q\n\n Third.\n- [ ]\n\n ```\n x\n ```\n'), [
bare('taskList', node('blockTaskItem', { state: 'DONE' }, said('First.'), said('Second.'))),
bare('blockquote', said('q')),
said('Third.'),
bare('taskList', task('TODO')),
bare('codeBlock', text('x')),
])
assert.deepEqual(read('- [x] A\n - plain\n- [ ] B\n'), [bare('taskList', task('DONE', text('A'))), bare('bulletList', bare('listItem', said('plain'))), bare('taskList', task('TODO', text('B')))])
assert.deepEqual(read('- [ ] A\n\n # h\n - [ ] Child\n- [ ] B\n'), [
bare('taskList', task('TODO', text('A'))),
node('heading', { level: 1 }, text('h')),
bare('taskList', task('TODO', text('Child'))),
bare('taskList', task('TODO', text('B'))),
])
assert.deepEqual(read('- [x] A\n - plain\n'), [bare('taskList', node('blockTaskItem', { state: 'DONE' }, said('A'), bare('bulletList', bare('listItem', said('plain')))))])
})
test('leaves mixed, ordered and unmarked lists plain', () => {
@@ -209,9 +228,9 @@ test('reads what the reduction wrote back to the node it reduced, less the attri
task('DONE', text('Write')),
bare('taskList', task('TODO', text('Review'))),
node('blockTaskItem', { state: 'TODO' }, said('First.'), said('Second.')),
node('blockTaskItem', { state: 'DONE' }, bare('codeBlock', text('x'))),
task('DONE'),
)
assert.deepEqual(roundTripped(tasks), [plainTasks])
assert.deepEqual(roundTripped(tasks), [plainTasks, bare('codeBlock', text('x'))])
const colour: AdfMark = { attrs: { color: '#c6edfb' }, type: 'backgroundColor' }
assert.deepEqual(roundTripped(paragraph(text('a '), text('hi', colour, strong), text(' b'))), [paragraph(text('a '), text('hi', highlight, strong), text(' b'))])
assert.deepEqual(roundTripped(paragraph(text('=', colour), text(' '), text('a==b', colour))), [paragraph(text('=', highlight), text(' '), text('a==b', highlight))])