fix: compatibility with lowlight v2 (#1939)

This commit is contained in:
MO 2021-10-01 03:34:20 +08:00 committed by GitHub
parent dead826250
commit f79347e128
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -34,8 +34,8 @@ function getDecorations({ doc, name, lowlight }: { doc: ProsemirrorNode, name: s
const { language } = block.node.attrs
const languages = lowlight.listLanguages()
const nodes = language && languages.includes(language)
? lowlight.highlight(language, block.node.textContent).value
: lowlight.highlightAuto(block.node.textContent).value
? getHighlightNodes(lowlight.highlight(language, block.node.textContent))
: getHighlightNodes(lowlight.highlightAuto(block.node.textContent))
parseNodes(nodes).forEach(node => {
const to = from + node.text.length
@ -51,6 +51,11 @@ function getDecorations({ doc, name, lowlight }: { doc: ProsemirrorNode, name: s
from = to
})
})
function getHighlightNodes(result) {
// `.value` for lowlight v1, `.children` for lowlight v2
return result.value || result.children || []
}
return DecorationSet.create(doc, decorations)
}