refactoring
This commit is contained in:
@@ -1,10 +1,10 @@
|
|||||||
import CodeBlock, { CodeBlockOptions } from '@tiptap/extension-code-block'
|
import CodeBlock, { CodeBlockOptions } from '@tiptap/extension-code-block'
|
||||||
import low from 'lowlight/lib/core'
|
import lowlight from 'lowlight/lib/core'
|
||||||
import { LowlightPlugin } from './lowlight-plugin'
|
import { LowlightPlugin } from './lowlight-plugin'
|
||||||
|
|
||||||
export interface CodeBlockLowlightOptions extends CodeBlockOptions {
|
export interface CodeBlockLowlightOptions extends CodeBlockOptions {
|
||||||
languages: {
|
languages: {
|
||||||
[key: string]: Function
|
[key: string]: Function,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -15,9 +15,11 @@ export const CodeBlockLowlight = CodeBlock.extend<CodeBlockLowlightOptions>({
|
|||||||
},
|
},
|
||||||
|
|
||||||
onBeforeCreate() {
|
onBeforeCreate() {
|
||||||
Object.entries(this.options.languages).forEach(([name, mapping]) => {
|
Object
|
||||||
low.registerLanguage(name, mapping)
|
.entries(this.options.languages)
|
||||||
})
|
.forEach(([name, mapping]) => {
|
||||||
|
lowlight.registerLanguage(name, mapping)
|
||||||
|
})
|
||||||
},
|
},
|
||||||
|
|
||||||
addProseMirrorPlugins() {
|
addProseMirrorPlugins() {
|
||||||
|
|||||||
@@ -1,8 +1,8 @@
|
|||||||
import { Plugin, PluginKey } from 'prosemirror-state'
|
import { Plugin, PluginKey } from 'prosemirror-state'
|
||||||
import { Decoration, DecorationSet } from 'prosemirror-view'
|
import { Decoration, DecorationSet } from 'prosemirror-view'
|
||||||
import { Node as ProsemirrorNode } from 'prosemirror-model'
|
import { Node as ProsemirrorNode } from 'prosemirror-model'
|
||||||
import low from 'lowlight/lib/core'
|
|
||||||
import { findChildren } from '@tiptap/core'
|
import { findChildren } from '@tiptap/core'
|
||||||
|
import lowlight from 'lowlight/lib/core'
|
||||||
|
|
||||||
function parseNodes(nodes: any[], className: string[] = []): { text: string, classes: string[] }[] {
|
function parseNodes(nodes: any[], className: string[] = []): { text: string, classes: string[] }[] {
|
||||||
return nodes
|
return nodes
|
||||||
@@ -35,10 +35,10 @@ function getDecorations({ doc, name }: { doc: ProsemirrorNode, name: string}) {
|
|||||||
const { language } = block.node.attrs
|
const { language } = block.node.attrs
|
||||||
// TODO: add missing type for `listLanguages`
|
// TODO: add missing type for `listLanguages`
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
const languages = low.listLanguages() as string[]
|
const languages = lowlight.listLanguages() as string[]
|
||||||
const nodes = language && languages.includes(language)
|
const nodes = language && languages.includes(language)
|
||||||
? low.highlight(language, block.node.textContent).value
|
? lowlight.highlight(language, block.node.textContent).value
|
||||||
: low.highlightAuto(block.node.textContent).value
|
: lowlight.highlightAuto(block.node.textContent).value
|
||||||
|
|
||||||
parseNodes(nodes).forEach(node => {
|
parseNodes(nodes).forEach(node => {
|
||||||
const to = from + node.text.length
|
const to = from + node.text.length
|
||||||
|
|||||||
Reference in New Issue
Block a user