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

Answer architecture round 1: splitTaskList groups a list into items, bulletBlocks returns the split blocks itself, isTaskItem lives in adf/, a property reads generated task lists through portableMarkdownToAdf, and items 56 and 93 name today's code

This commit is contained in:
2026-10-09 14:15:40 +02:00
committed by Lillem4n
parent 3ad1bd504e
commit 2e640ded91
7 changed files with 111 additions and 43 deletions
+39 -20
View File
@@ -1,36 +1,55 @@
import type { AdfNode } from '../../adf/document.ts'
import { isTaskItem } from '../../adf/block-nodes.ts'
import { nodeContent } from '../../adf/document.ts'
// Atlassian's schema holds a blockTaskItem to paragraphs and extensions, and nests a task list beside its item.
// Atlassian's schema holds a blockTaskItem to paragraphs and extensions, and the portable flavour spells no extension: an item keeps its paragraphs, then the task lists nested in it, and the rest of it stands 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)
let held = 0
while (content[held]?.type === 'paragraph') held += 1
let nested = held
while (content[nested]?.type === 'taskList') nested += 1
tasks.push(heldTask(child, content.slice(0, held)))
for (const taskList of content.slice(held, nested)) tasks.push(taskList)
stands = content.slice(nested)
} else if (stands.length === 0) tasks.push(child)
if (stands.length === 0) continue
for (const item of itemsOf(nodeContent(list))) {
const { kept, rest } = itemParts(item)
for (const node of kept) tasks.push(node)
if (rest.length === 0) continue
if (tasks.length > 0) blocks.push({ ...list, content: tasks })
tasks = []
for (const block of stands) blocks.push(block)
standing = true
for (const block of rest) blocks.push(block)
}
if (tasks.length > 0) blocks.push({ ...list, content: tasks })
return blocks
}
function heldTask(task: AdfNode, paragraphs: readonly AdfNode[]): AdfNode {
// An item is a task and the children up to the next one.
function itemsOf(children: readonly AdfNode[]): AdfNode[][] {
const items: AdfNode[][] = []
for (const child of children) {
const item = items.at(-1)
if (item === undefined || isTaskItem(child)) items.push([child])
else item.push(child)
}
return items
}
function itemParts(item: readonly AdfNode[]): { kept: AdfNode[]; rest: AdfNode[] } {
const [task] = item
if (task === undefined || !isTaskItem(task)) return { kept: [], rest: [...item] }
const content = task.type === 'blockTaskItem' ? nodeContent(task) : []
const paragraphs = runEnd(content, 0, 'paragraph')
const nested = runEnd(content, paragraphs, 'taskList')
const unheld = content.slice(nested)
const after = item.slice(1)
const beside = unheld.length === 0 ? runEnd(after, 0, 'taskList') : 0
const head = task.type === 'blockTaskItem' ? taskOfParagraphs(task, content.slice(0, paragraphs)) : task
return { kept: [head, ...content.slice(paragraphs, nested), ...after.slice(0, beside)], rest: [...unheld, ...after.slice(beside)] }
}
function runEnd(nodes: readonly AdfNode[], start: number, type: string): number {
let end = start
while (nodes[end]?.type === type) end += 1
return end
}
// One paragraph or none reads as a taskItem.
function taskOfParagraphs(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' }