feat: add defaultLanguage option to CodeBlockLowlight extension, fix #2121

This commit is contained in:
Philipp Kühn 2021-11-05 14:00:44 +01:00
parent b5b9363b49
commit 0f94bcd591
3 changed files with 35 additions and 5 deletions

View File

@ -44,6 +44,17 @@ CodeBlockLowlight.configure({
})
```
### defaultLanguage
Define a default language instead of the automatic detection of lowlight.
Default: `null`
```js
CodeBlockLowlight.configure({
defaultLanguage: 'plaintext',
})
```
## Commands
### setCodeBlock()

View File

@ -4,6 +4,7 @@ import { LowlightPlugin } from './lowlight-plugin'
export interface CodeBlockLowlightOptions extends CodeBlockOptions {
lowlight: any,
defaultLanguage: string | null | undefined,
}
export const CodeBlockLowlight = CodeBlock.extend<CodeBlockLowlightOptions>({
@ -11,6 +12,7 @@ export const CodeBlockLowlight = CodeBlock.extend<CodeBlockLowlightOptions>({
return {
...this.parent?.(),
lowlight,
defaultLanguage: null,
}
},
@ -20,6 +22,7 @@ export const CodeBlockLowlight = CodeBlock.extend<CodeBlockLowlightOptions>({
LowlightPlugin({
name: this.name,
lowlight: this.options.lowlight,
defaultLanguage: this.options.defaultLanguage,
}),
]
},

View File

@ -30,13 +30,19 @@ function getHighlightNodes(result: any) {
return result.value || result.children || []
}
function getDecorations({ doc, name, lowlight }: { doc: ProsemirrorNode, name: string, lowlight: any }) {
function getDecorations({
doc,
name,
lowlight,
defaultLanguage,
}: { doc: ProsemirrorNode, name: string, lowlight: any, defaultLanguage: string | null | undefined }) {
const decorations: Decoration[] = []
findChildren(doc, node => node.type.name === name)
.forEach(block => {
let from = block.pos + 1
const { language } = block.node.attrs
const language = block.node.attrs.language || defaultLanguage
console.log({ language, defaultLanguage })
const languages = lowlight.listLanguages()
const nodes = language && languages.includes(language)
? getHighlightNodes(lowlight.highlight(language, block.node.textContent))
@ -60,12 +66,17 @@ function getDecorations({ doc, name, lowlight }: { doc: ProsemirrorNode, name: s
return DecorationSet.create(doc, decorations)
}
export function LowlightPlugin({ name, lowlight }: { name: string, lowlight: any }) {
export function LowlightPlugin({ name, lowlight, defaultLanguage }: { name: string, lowlight: any, defaultLanguage: string | null | undefined }) {
return new Plugin({
key: new PluginKey('lowlight'),
state: {
init: (_, { doc }) => getDecorations({ doc, name, lowlight }),
init: (_, { doc }) => getDecorations({
doc,
name,
lowlight,
defaultLanguage,
}),
apply: (transaction, decorationSet, oldState, newState) => {
const oldNodeName = oldState.selection.$head.parent.type.name
const newNodeName = newState.selection.$head.parent.type.name
@ -97,7 +108,12 @@ export function LowlightPlugin({ name, lowlight }: { name: string, lowlight: any
})
)
) {
return getDecorations({ doc: transaction.doc, name, lowlight })
return getDecorations({
doc: transaction.doc,
name,
lowlight,
defaultLanguage,
})
}
return decorationSet.map(transaction.mapping, transaction.doc)