refactoring
This commit is contained in:
@@ -23,60 +23,56 @@ 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 = [
|
||||||
|
...className,
|
||||||
|
...node.properties
|
||||||
|
? node.properties.className
|
||||||
|
: [],
|
||||||
|
]
|
||||||
|
|
||||||
const classes = [
|
if (node.children) {
|
||||||
...className,
|
return parseNodes(node.children, classes)
|
||||||
...node.properties ? node.properties.className : [],
|
}
|
||||||
]
|
|
||||||
|
|
||||||
if (node.children) {
|
return {
|
||||||
return parseNodes(node.children, classes)
|
text: node.value,
|
||||||
}
|
classes,
|
||||||
|
}
|
||||||
return {
|
})
|
||||||
text: node.value,
|
.flat()
|
||||||
classes,
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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)
|
||||||
let startPos = block.pos + 1
|
.filter(block => block.node.type.name === name)
|
||||||
const { language } = block.node.attrs
|
.forEach(block => {
|
||||||
// TODO: add missing type for `listLanguages`
|
let startPos = block.pos + 1
|
||||||
// @ts-ignore
|
const { language } = block.node.attrs
|
||||||
const languages = low.listLanguages() as string[]
|
// TODO: add missing type for `listLanguages`
|
||||||
const nodes = language && languages.includes(language)
|
// @ts-ignore
|
||||||
? low.highlight(language, block.node.textContent).value
|
const languages = low.listLanguages() as string[]
|
||||||
: low.highlightAuto(block.node.textContent).value
|
const nodes = language && languages.includes(language)
|
||||||
|
? low.highlight(language, 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)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
return DecorationSet.create(doc, decorations)
|
return DecorationSet.create(doc, decorations)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user