Support mixed nested lists (#39)

This commit is contained in:
Yuri Karamian
2026-08-03 02:26:39 +02:00
parent 6b165059cc
commit 8731ca4ec1
3 changed files with 171 additions and 8 deletions
+27
View File
@@ -0,0 +1,27 @@
import type { Node as ProseMirrorNode, Schema } from '@tiptap/pm/model';
export type MixedListName = 'bulletList' | 'taskList';
export function convertListNode(
schema: Schema,
listNode: ProseMirrorNode,
targetListName: MixedListName,
): ProseMirrorNode | null {
if (
(listNode.type.name !== 'bulletList' && listNode.type.name !== 'taskList') ||
listNode.type.name === targetListName
) {
return null;
}
const targetListType = schema.nodes[targetListName];
const targetItemType = schema.nodes[targetListName === 'taskList' ? 'taskItem' : 'listItem'];
if (!targetListType || !targetItemType) return null;
const itemAttrs = targetListName === 'taskList' ? { checked: false } : null;
const convertedItems = listNode.content.content.map((item) =>
targetItemType.create(itemAttrs, item.content, item.marks)
);
return targetListType.create(null, convertedItems, listNode.marks);
}