From 0f94bcd591e98c0a767d0914b87a1efee8b002e0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philipp=20Ku=CC=88hn?= Date: Fri, 5 Nov 2021 14:00:44 +0100 Subject: [PATCH] feat: add defaultLanguage option to CodeBlockLowlight extension, fix #2121 --- docs/api/nodes/code-block-lowlight.md | 11 ++++++++ .../src/code-block-lowlight.ts | 3 +++ .../src/lowlight-plugin.ts | 26 +++++++++++++++---- 3 files changed, 35 insertions(+), 5 deletions(-) diff --git a/docs/api/nodes/code-block-lowlight.md b/docs/api/nodes/code-block-lowlight.md index bfcf43eb..cc6a7139 100644 --- a/docs/api/nodes/code-block-lowlight.md +++ b/docs/api/nodes/code-block-lowlight.md @@ -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() diff --git a/packages/extension-code-block-lowlight/src/code-block-lowlight.ts b/packages/extension-code-block-lowlight/src/code-block-lowlight.ts index 5d369eb0..458f502c 100644 --- a/packages/extension-code-block-lowlight/src/code-block-lowlight.ts +++ b/packages/extension-code-block-lowlight/src/code-block-lowlight.ts @@ -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({ @@ -11,6 +12,7 @@ export const CodeBlockLowlight = CodeBlock.extend({ return { ...this.parent?.(), lowlight, + defaultLanguage: null, } }, @@ -20,6 +22,7 @@ export const CodeBlockLowlight = CodeBlock.extend({ LowlightPlugin({ name: this.name, lowlight: this.options.lowlight, + defaultLanguage: this.options.defaultLanguage, }), ] }, diff --git a/packages/extension-code-block-lowlight/src/lowlight-plugin.ts b/packages/extension-code-block-lowlight/src/lowlight-plugin.ts index 25b95556..73f09823 100644 --- a/packages/extension-code-block-lowlight/src/lowlight-plugin.ts +++ b/packages/extension-code-block-lowlight/src/lowlight-plugin.ts @@ -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)