fix nested lists save

This commit is contained in:
Arjun 2026-03-30 22:43:48 +05:30 committed by arkml
parent 86cc2aaf73
commit 61e92783b2

View file

@ -109,39 +109,44 @@ function getMarkdownWithBlankLines(editor: Editor): string {
const level = (node.attrs?.level as number) || 1 const level = (node.attrs?.level as number) || 1
const text = nodeToText(node) const text = nodeToText(node)
blocks.push('#'.repeat(level) + ' ' + text) blocks.push('#'.repeat(level) + ' ' + text)
} else if (node.type === 'bulletList' || node.type === 'orderedList') { } else if (node.type === 'bulletList' || node.type === 'orderedList' || node.type === 'taskList') {
// Handle lists - all items are part of one block // Recursively serialize lists to handle nested bullets
const listLines: string[] = [] const serializeList = (
const listItems = (node.content || []) as Array<{ content?: Array<unknown>; attrs?: Record<string, unknown> }> listNode: { type?: string; content?: Array<Record<string, unknown>>; attrs?: Record<string, unknown> },
listItems.forEach((item, index) => { indent: number
const prefix = node.type === 'orderedList' ? `${index + 1}. ` : '- ' ): string[] => {
const itemContent = (item.content || []) as Array<{ type?: string; content?: Array<{ type?: string; text?: string; marks?: Array<{ type: string; attrs?: Record<string, unknown> }> }>; attrs?: Record<string, unknown> }> const lines: string[] = []
itemContent.forEach((para: { type?: string; content?: Array<{ type?: string; text?: string; marks?: Array<{ type: string; attrs?: Record<string, unknown> }> }>; attrs?: Record<string, unknown> }, paraIndex: number) => { const items = (listNode.content || []) as Array<{ content?: Array<Record<string, unknown>>; attrs?: Record<string, unknown> }>
const text = nodeToText(para) items.forEach((item, index) => {
if (paraIndex === 0) { const indentStr = ' '.repeat(indent)
listLines.push(prefix + text) let prefix: string
if (listNode.type === 'taskList') {
const checked = item.attrs?.checked ? 'x' : ' '
prefix = `- [${checked}] `
} else if (listNode.type === 'orderedList') {
prefix = `${index + 1}. `
} else { } else {
listLines.push(' ' + text) prefix = '- '
} }
const itemContent = (item.content || []) as Array<{ type?: string; content?: Array<{ type?: string; text?: string; marks?: Array<{ type: string; attrs?: Record<string, unknown> }> }>; attrs?: Record<string, unknown> }>
let firstPara = true
itemContent.forEach(child => {
if (child.type === 'bulletList' || child.type === 'orderedList' || child.type === 'taskList') {
lines.push(...serializeList(child, indent + 1))
} else {
const text = nodeToText(child)
if (firstPara) {
lines.push(indentStr + prefix + text)
firstPara = false
} else {
lines.push(indentStr + ' ' + text)
}
}
})
}) })
}) return lines
blocks.push(listLines.join('\n')) }
} else if (node.type === 'taskList') { blocks.push(serializeList(node, 0).join('\n'))
const listLines: string[] = []
const listItems = (node.content || []) as Array<{ content?: Array<unknown>; attrs?: Record<string, unknown> }>
listItems.forEach(item => {
const checked = item.attrs?.checked ? 'x' : ' '
const itemContent = (item.content || []) as Array<{ type?: string; content?: Array<{ type?: string; text?: string; marks?: Array<{ type: string; attrs?: Record<string, unknown> }> }>; attrs?: Record<string, unknown> }>
itemContent.forEach((para: { type?: string; content?: Array<{ type?: string; text?: string; marks?: Array<{ type: string; attrs?: Record<string, unknown> }> }>; attrs?: Record<string, unknown> }, paraIndex: number) => {
const text = nodeToText(para)
if (paraIndex === 0) {
listLines.push(`- [${checked}] ${text}`)
} else {
listLines.push(' ' + text)
}
})
})
blocks.push(listLines.join('\n'))
} else if (node.type === 'taskBlock') { } else if (node.type === 'taskBlock') {
blocks.push('```task\n' + (node.attrs?.data as string || '{}') + '\n```') blocks.push('```task\n' + (node.attrs?.data as string || '{}') + '\n```')
} else if (node.type === 'imageBlock') { } else if (node.type === 'imageBlock') {