diff --git a/packages/core/src/helpers/getDebugJSON.ts b/packages/core/src/helpers/getDebugJSON.ts index 34c7e8af..fdd25fda 100644 --- a/packages/core/src/helpers/getDebugJSON.ts +++ b/packages/core/src/helpers/getDebugJSON.ts @@ -6,45 +6,41 @@ interface DebugJSONContent extends JSONContent { to: number, } -/** - * Returns a node tree with node positions. - */ -export default function getDebugJSON(node: ProseMirrorNode, startOffset = 0) { - const nodes: DebugJSONContent[] = [] +export default function getDebugJSON(node: ProseMirrorNode, startOffset = 0): DebugJSONContent { + const isTopNode = node.type === node.type.schema.topNodeType + const increment = isTopNode ? 0 : 1 + const from = startOffset // + offset + const to = from + node.nodeSize + const marks = node.marks.map(mark => ({ + type: mark.type.name, + attrs: { ...mark.attrs }, + })) + const attrs = { ...node.attrs } + const output: DebugJSONContent = { + type: node.type.name, + from, + to, + } - node.forEach((n, offset) => { - const from = startOffset + offset - const to = from + n.nodeSize - const marks = n.marks.map(mark => ({ - type: mark.type.name, - attrs: { ...mark.attrs }, - })) - const attrs = { ...n.attrs } - const content = getDebugJSON(n, from + 1) - const output: DebugJSONContent = { - type: n.type.name, - from, - to, - } + if (Object.keys(attrs).length) { + output.attrs = attrs + } - if (Object.keys(attrs).length) { - output.attrs = attrs - } + if (marks.length) { + output.marks = marks + } - if (marks.length) { - output.marks = marks - } + if (node.content.childCount) { + output.content = [] - if (content.length) { - output.content = content - } + node.forEach((child, offset) => { + output.content?.push(getDebugJSON(child, startOffset + offset + increment)) + }) + } - if (n.text) { - output.text = n.text - } + if (node.text) { + output.text = node.text + } - nodes.push(output) - }) - - return nodes + return output }