From c94264894b4b2b6ad4f2ce70b30816fc24f2b7c0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philipp=20Ku=CC=88hn?= Date: Fri, 2 Apr 2021 23:02:05 +0200 Subject: [PATCH] fix a big in highlighter --- .../src/lowlight-plugin.ts | 40 +++++++++---------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/packages/extension-code-block-lowlight/src/lowlight-plugin.ts b/packages/extension-code-block-lowlight/src/lowlight-plugin.ts index ef4dfeb0..5df3b7ff 100644 --- a/packages/extension-code-block-lowlight/src/lowlight-plugin.ts +++ b/packages/extension-code-block-lowlight/src/lowlight-plugin.ts @@ -23,29 +23,29 @@ const findBlockNodes = (doc: ProsemirrorNode) => { return nodes } +function parseNodes(nodes: any[], className: string[] = []): any { + return nodes.map(node => { + + const classes = [ + ...className, + ...node.properties ? node.properties.className : [], + ] + + if (node.children) { + return parseNodes(node.children, classes) + } + + return { + text: node.value, + classes, + } + }) +} + function getDecorations({ doc, name }: { doc: ProsemirrorNode, name: string}) { const decorations: Decoration[] = [] const blocks = findBlockNodes(doc).filter(block => block.node.type.name === name) - function parseNodes(nodes: any[], className: string[] = []): any { - return nodes.map(node => { - - const classes = [ - ...className, - ...node.properties ? node.properties.className : [], - ] - - if (node.children) { - return parseNodes(node.children, classes) - } - - return { - text: node.value, - classes, - } - }) - } - blocks.forEach(block => { let startPos = block.pos + 1 const { language } = block.node.attrs @@ -57,7 +57,7 @@ function getDecorations({ doc, name }: { doc: ProsemirrorNode, name: string}) { : low.highlightAuto(block.node.textContent).value parseNodes(nodes) - .flat() + .flat(Infinity) .map((node: any) => { const from = startPos const to = from + node.text.length