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
}
function parseNodes(nodes: any[], className: string[] = []): any {
return nodes.map(node => {
function parseNodes(nodes: any[], className: string[] = []): { text: string, classes: string[] }[] {
return nodes
.map(node => {
const classes = [
...className,
...node.properties ? node.properties.className : [],
...node.properties
? node.properties.className
: [],
]
if (node.children) {
@ -40,13 +42,15 @@ function parseNodes(nodes: any[], className: string[] = []): any {
classes,
}
})
.flat()
}
function getDecorations({ doc, name }: { doc: ProsemirrorNode, name: string}) {
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
const { language } = block.node.attrs
// 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.highlightAuto(block.node.textContent).value
parseNodes(nodes)
.flat(Infinity)
.map((node: any) => {
parseNodes(nodes).forEach(node => {
const from = startPos
const to = from + node.text.length
startPos = to
return {
...node,
from,
to,
}
})
.forEach((node: any) => {
const decoration = Decoration.inline(node.from, node.to, {
const decoration = Decoration.inline(from, to, {
class: node.classes.join(' '),
})
decorations.push(decoration)
})
})