refactoring

This commit is contained in:
Philipp Kühn
2021-04-02 23:40:52 +02:00
parent c94264894b
commit dd505d2773

View File

@@ -23,12 +23,14 @@ const findBlockNodes = (doc: ProsemirrorNode) => {
return nodes return nodes
} }
function parseNodes(nodes: any[], className: string[] = []): any { function parseNodes(nodes: any[], className: string[] = []): { text: string, classes: string[] }[] {
return nodes.map(node => { return nodes
.map(node => {
const classes = [ const classes = [
...className, ...className,
...node.properties ? node.properties.className : [], ...node.properties
? node.properties.className
: [],
] ]
if (node.children) { if (node.children) {
@@ -40,13 +42,15 @@ function parseNodes(nodes: any[], className: string[] = []): any {
classes, classes,
} }
}) })
.flat()
} }
function getDecorations({ doc, name }: { doc: ProsemirrorNode, name: string}) { function getDecorations({ doc, name }: { doc: ProsemirrorNode, name: string}) {
const decorations: Decoration[] = [] const decorations: Decoration[] = []
const blocks = findBlockNodes(doc).filter(block => block.node.type.name === name)
blocks.forEach(block => { findBlockNodes(doc)
.filter(block => block.node.type.name === name)
.forEach(block => {
let startPos = block.pos + 1 let startPos = block.pos + 1
const { language } = block.node.attrs const { language } = block.node.attrs
// TODO: add missing type for `listLanguages` // TODO: add missing type for `listLanguages`
@@ -56,24 +60,16 @@ function getDecorations({ doc, name }: { doc: ProsemirrorNode, name: string}) {
? low.highlight(language, block.node.textContent).value ? low.highlight(language, block.node.textContent).value
: low.highlightAuto(block.node.textContent).value : low.highlightAuto(block.node.textContent).value
parseNodes(nodes) parseNodes(nodes).forEach(node => {
.flat(Infinity)
.map((node: any) => {
const from = startPos const from = startPos
const to = from + node.text.length const to = from + node.text.length
startPos = to startPos = to
return { const decoration = Decoration.inline(from, to, {
...node,
from,
to,
}
})
.forEach((node: any) => {
const decoration = Decoration.inline(node.from, node.to, {
class: node.classes.join(' '), class: node.classes.join(' '),
}) })
decorations.push(decoration) decorations.push(decoration)
}) })
}) })