Declare lowlight as a peerDependency (#2625)

Declare lowlight as a peerDependency to delegate
the control of which version of lowlight is used
to the client application

Co-authored-by: Enrique Alcantara <ealcantara@gitlab.com>
This commit is contained in:
Enrique Alcántara
2022-06-06 13:33:52 -04:00
committed by GitHub
parent fd593bb93e
commit 39f5e4c31e
7 changed files with 39 additions and 12 deletions

View File

@@ -1,4 +1,3 @@
import lowlight from 'lowlight/lib/core'
import CodeBlock, { CodeBlockOptions } from '@tiptap/extension-code-block'
import { LowlightPlugin } from './lowlight-plugin'
@@ -11,7 +10,7 @@ export const CodeBlockLowlight = CodeBlock.extend<CodeBlockLowlightOptions>({
addOptions() {
return {
...this.parent?.(),
lowlight,
lowlight: {},
defaultLanguage: null,
}
},

View File

@@ -65,7 +65,15 @@ function getDecorations({
return DecorationSet.create(doc, decorations)
}
function isFunction(param: Function) {
return typeof param === 'function'
}
export function LowlightPlugin({ name, lowlight, defaultLanguage }: { name: string, lowlight: any, defaultLanguage: string | null | undefined }) {
if (!['highlight', 'highlightAuto', 'listLanguages'].every(api => isFunction(lowlight[api]))) {
throw Error('You should provide an instance of lowlight to use the code-block-lowlight extension')
}
return new Plugin({
key: new PluginKey('lowlight'),