mirror of
https://github.com/larvit/adf-codec.git
synced 2026-11-19 14:45: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:
@@ -1,6 +1,6 @@
|
||||
import type { AdfDocument, AdfNode } from '../../adf/document.ts'
|
||||
import { adfDocumentFault, nodeAttrs, nodeContent } from '../../adf/document.ts'
|
||||
import { blockNodeModel } from '../../adf/block-nodes.ts'
|
||||
import { blockNodeModel, isTaskItem } from '../../adf/block-nodes.ts'
|
||||
import { centeredImage, externalMedia } from '../external-image.ts'
|
||||
import { commonMarkSpelling, largestListMarker, writeMarkdown, type SpellingMemo } from '../emit/adf-to-markdown.ts'
|
||||
import { failure, faulted, success, type ConvertErrorPath, type Result } from '../../result.ts'
|
||||
@@ -179,7 +179,7 @@ function tasksAsText(list: AdfNode): AdfNode {
|
||||
if (list.type !== 'taskList') return list
|
||||
const items: AdfNode[] = []
|
||||
for (const child of nodeContent(list)) {
|
||||
const previous = isTask(child) || child.type === 'listItem' ? undefined : items.pop()
|
||||
const previous = isTaskItem(child) || child.type === 'listItem' ? undefined : items.pop()
|
||||
items.push(itemOf(previous === undefined ? taskAsText(child) : mergedLists([...nodeContent(previous), child])))
|
||||
}
|
||||
return { content: items, type: 'bulletList' }
|
||||
@@ -285,27 +285,28 @@ function blankedLines(code: readonly AdfNode[]): AdfNode[] {
|
||||
return blanked === '' ? [] : [text(blanked)]
|
||||
}
|
||||
|
||||
// A task list opening with a task and holding tasks and task lists alone keeps its spelling, a list nesting in the task before it; any other keeps its markers as text. A child reducing to nothing counts for neither.
|
||||
// A task list opening with a task and holding tasks and task lists alone keeps its spelling; any other keeps its markers as text. A child reducing to nothing counts for neither.
|
||||
function reduceTaskList(node: AdfNode, reduction: Reduction): Result<AdfNode[]> {
|
||||
const kept: { blocks: AdfNode[]; child: AdfNode }[] = []
|
||||
for (const [index, child] of nodeContent(node).entries()) {
|
||||
const at = childReduction(reduction, index)
|
||||
const reduced = isTask(child) ? reduceTask(child, at) : reduceStanding(child, at)
|
||||
const reduced = isTaskItem(child) ? reduceTask(child, at) : reduceStanding(child, at)
|
||||
if (!reduced.ok) return reduced
|
||||
if (reduced.value.length > 0) kept.push({ blocks: reduced.value, child })
|
||||
}
|
||||
const regular = isTask(kept[0]?.child) && kept.every(({ child }) => isTask(child) || child.type === 'taskList')
|
||||
const regular = isTaskItem(kept[0]?.child) && kept.every(({ child }) => isTaskItem(child) || child.type === 'taskList')
|
||||
const tasks: AdfNode[] = []
|
||||
for (const { blocks, child } of kept) {
|
||||
const standsAlone = !regular && !isTask(child) && child.type !== 'taskList'
|
||||
const standsAlone = !regular && !isTaskItem(child) && child.type !== 'taskList'
|
||||
for (const block of standsAlone ? [{ content: blocks, type: 'listItem' }] : blocks) tasks.push(block)
|
||||
}
|
||||
if (regular) return success(splitTaskList({ content: tasks, type: 'taskList' }).map((block) => (block.type === 'taskList' ? { ...block, content: mergedLists(nodeContent(block)) } : block)))
|
||||
if (regular) return success(splitTasks({ content: tasks, type: 'taskList' }))
|
||||
return success(listOf(nodeContent(tasksAsText({ content: tasks, type: 'taskList' })), 'bulletList'))
|
||||
}
|
||||
|
||||
function isTask(node: AdfNode | undefined): boolean {
|
||||
return node?.type === 'taskItem' || node?.type === 'blockTaskItem'
|
||||
// The split sets a task's nested lists beside the list after it, and adjacent lists read back as one.
|
||||
function splitTasks(list: AdfNode): AdfNode[] {
|
||||
return splitTaskList(list).map((block) => (block.type === 'taskList' ? { ...block, content: mergedLists(nodeContent(block)) } : block))
|
||||
}
|
||||
|
||||
function reduceTask(task: AdfNode, at: Reduction): Result<AdfNode[]> {
|
||||
|
||||
@@ -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' }
|
||||
|
||||
Reference in New Issue
Block a user