mirror of
https://github.com/larvit/adf-codec.git
synced 2026-11-20 23:25:43 +01:00
Stand a block a task item cannot hold after its task list, in portableMarkdownToAdf and adfToPortableMarkdown
This commit is contained in:
@@ -131,9 +131,9 @@ test('spells a task list as a bullet list whose items lead with their state', ()
|
||||
assert.equal(portable(node('taskList', {}), task('TODO', 'Loose')), 'Loose\n')
|
||||
const blockTask = node('blockTaskItem', { state: 'DONE' }, said('First.'), said('Second.'))
|
||||
const codeTask = node('blockTaskItem', { state: 'TODO' }, { content: [text('x')], type: 'codeBlock' })
|
||||
assert.equal(portable(node('taskList', {}, blockTask, codeTask)), '- [x] First.\n\n Second.\n- [ ]\n\n ```\n x\n ```\n')
|
||||
assert.equal(portable(node('taskList', {}, blockTask, codeTask)), '- [x] First.\n\n Second.\n- [ ]\n\n```\nx\n```\n')
|
||||
const listTask = node('blockTaskItem', { state: 'DONE' }, bulletList(item(said('a'))))
|
||||
assert.equal(portable(node('taskList', {}, task('TODO', ''), listTask, node('taskList', {}, task('TODO', 'b')))), '- [ ]\n- [x]\n - a\n - \\[ ] b\n')
|
||||
assert.equal(portable(node('taskList', {}, task('TODO', ''), listTask, node('taskList', {}, task('TODO', 'b')))), '- \\[ ]\n- \\[x]\n- a\n- \\[ ] b\n')
|
||||
assert.equal(portable(node('taskList', {}, task('DONE', 'a'), node('taskList', {}, task('TODO', '')))), '- [x] a\n - [ ]\n')
|
||||
})
|
||||
|
||||
|
||||
@@ -9,6 +9,7 @@ import { inlineLeaves, isBlockNodeType, oneLine, reduceInline, writableHref } fr
|
||||
import { inlineNodeModel } from '../../adf/inline-nodes.ts'
|
||||
import { languageSlot } from '../code-language.ts'
|
||||
import { largestNesting } from '../../nesting.ts'
|
||||
import { splitTaskList } from './task-list.ts'
|
||||
import { toEditorNormal } from './editor-normal.ts'
|
||||
|
||||
// depth: the level the node reduced stands at, counted as the emitter counts it.
|
||||
@@ -309,7 +310,7 @@ function reduceTaskList(node: AdfNode, reduction: Reduction): Result<AdfNode[]>
|
||||
for (const block of blocks) (isTask(child) ? tasks : nested).push(block)
|
||||
}
|
||||
nestIn(tasks, nested)
|
||||
if (regular) return success([{ content: tasks, type: 'taskList' }])
|
||||
if (regular) return success(splitTaskList({ content: tasks, type: 'taskList' }))
|
||||
return success(listOf(nodeContent(tasksAsText({ content: tasks, type: 'taskList' })), 'bulletList'))
|
||||
}
|
||||
|
||||
@@ -331,7 +332,7 @@ function reduceTask(task: AdfNode, at: Reduction): Result<AdfNode[]> {
|
||||
return content.ok ? success([{ attrs, content: content.value, type: 'taskItem' }]) : content
|
||||
}
|
||||
const blocks = reduceBlocks(nodeContent(task), at)
|
||||
return blocks.ok ? success([{ attrs, content: blankedCode(blocks.value), type: 'blockTaskItem' }]) : blocks
|
||||
return blocks.ok ? success([{ attrs, content: blocks.value, type: 'blockTaskItem' }]) : blocks
|
||||
}
|
||||
|
||||
// The marker leads the first paragraph, or stands as one where the blocks open with another.
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
import type { AdfNode } from '../../adf/document.ts'
|
||||
import { nodeContent } from '../../adf/document.ts'
|
||||
|
||||
// Atlassian's schema holds a block task item to paragraphs: from its first other block, the item's blocks and the lists beside it stand after the list, which resumes at the next task.
|
||||
export function splitTaskList(list: AdfNode): AdfNode[] {
|
||||
const blocks: AdfNode[] = []
|
||||
let tasks: AdfNode[] = []
|
||||
let standing = false
|
||||
for (const child of nodeContent(list)) {
|
||||
const isTask = child.type === 'taskItem' || child.type === 'blockTaskItem'
|
||||
if (isTask) standing = false
|
||||
let stands: readonly AdfNode[] = standing || !(isTask || child.type === 'taskList') ? [child] : []
|
||||
if (child.type === 'blockTaskItem') {
|
||||
const content = nodeContent(child)
|
||||
const held = content.findIndex((block) => block.type !== 'paragraph')
|
||||
tasks.push(heldTask(child, held === -1 ? content : content.slice(0, held)))
|
||||
stands = held === -1 ? [] : content.slice(held)
|
||||
} else if (stands.length === 0) tasks.push(child)
|
||||
if (stands.length === 0) continue
|
||||
if (tasks.length > 0) blocks.push({ ...list, content: tasks })
|
||||
tasks = []
|
||||
for (const block of stands) blocks.push(block)
|
||||
standing = true
|
||||
}
|
||||
if (tasks.length > 0) blocks.push({ ...list, content: tasks })
|
||||
return blocks
|
||||
}
|
||||
|
||||
function heldTask(task: AdfNode, paragraphs: readonly AdfNode[]): AdfNode {
|
||||
const [only] = paragraphs
|
||||
if (paragraphs.length > 1) return { ...task, content: [...paragraphs] }
|
||||
return { ...task, content: nodeContent(only ?? {}).slice(), type: 'taskItem' }
|
||||
}
|
||||
Reference in New Issue
Block a user