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

Report a refusal's path where the node stands once the task list moves it

This commit is contained in:
2026-10-11 11:53:03 +02:00
committed by Lillem4n
parent 6a082c9947
commit a38b61347a
4 changed files with 88 additions and 19 deletions
+16
View File
@@ -1,4 +1,5 @@
import type { AdfNode } from '../../adf/document.ts'
import type { ConvertErrorPath } from '../../result.ts'
import { isTaskItem } from '../../adf/block-nodes.ts'
import { nodeContent } from '../../adf/document.ts'
@@ -18,6 +19,21 @@ export function splitTaskList(list: AdfNode): AdfNode[] {
return blocks
}
// Where `node`, a child of one of the list's tasks, stands among the blocks splitTaskList returns; a lone paragraph, holding its inline node, stands as its taskItem.
export function splitPlace(list: AdfNode, node: AdfNode & { content: [AdfNode] }): [number, ...ConvertErrorPath] | undefined {
const [inline] = node.content
const places = splitTaskList(list).flatMap((block, index): [number, ...ConvertErrorPath][] => {
if (block === node) return [[index]]
if (block.type !== 'taskList') return []
return nodeContent(block).flatMap((item, child): [number, ...ConvertErrorPath][] => {
if (item === node || nodeContent(item)[0] === inline) return [[index, 'content', child]]
const at = nodeContent(item).indexOf(node)
return at === -1 ? [] : [[index, 'content', child, 'content', at]]
})
})
return places[0]
}
// An item is a task and the children up to the next one.
function itemsOf(children: readonly AdfNode[]): AdfNode[][] {
const items: AdfNode[][] = []