mirror of
https://github.com/ueberdosis/tiptap.git
synced 2025-06-11 11:45:15 +08:00
feat: add defaultLanguage option to CodeBlockLowlight extension, fix #2121
This commit is contained in:
parent
b5b9363b49
commit
0f94bcd591
@ -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
|
## Commands
|
||||||
|
|
||||||
### setCodeBlock()
|
### setCodeBlock()
|
||||||
|
@ -4,6 +4,7 @@ import { LowlightPlugin } from './lowlight-plugin'
|
|||||||
|
|
||||||
export interface CodeBlockLowlightOptions extends CodeBlockOptions {
|
export interface CodeBlockLowlightOptions extends CodeBlockOptions {
|
||||||
lowlight: any,
|
lowlight: any,
|
||||||
|
defaultLanguage: string | null | undefined,
|
||||||
}
|
}
|
||||||
|
|
||||||
export const CodeBlockLowlight = CodeBlock.extend<CodeBlockLowlightOptions>({
|
export const CodeBlockLowlight = CodeBlock.extend<CodeBlockLowlightOptions>({
|
||||||
@ -11,6 +12,7 @@ export const CodeBlockLowlight = CodeBlock.extend<CodeBlockLowlightOptions>({
|
|||||||
return {
|
return {
|
||||||
...this.parent?.(),
|
...this.parent?.(),
|
||||||
lowlight,
|
lowlight,
|
||||||
|
defaultLanguage: null,
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
@ -20,6 +22,7 @@ export const CodeBlockLowlight = CodeBlock.extend<CodeBlockLowlightOptions>({
|
|||||||
LowlightPlugin({
|
LowlightPlugin({
|
||||||
name: this.name,
|
name: this.name,
|
||||||
lowlight: this.options.lowlight,
|
lowlight: this.options.lowlight,
|
||||||
|
defaultLanguage: this.options.defaultLanguage,
|
||||||
}),
|
}),
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
|
@ -30,13 +30,19 @@ function getHighlightNodes(result: any) {
|
|||||||
return result.value || result.children || []
|
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[] = []
|
const decorations: Decoration[] = []
|
||||||
|
|
||||||
findChildren(doc, node => node.type.name === name)
|
findChildren(doc, node => node.type.name === name)
|
||||||
.forEach(block => {
|
.forEach(block => {
|
||||||
let from = block.pos + 1
|
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 languages = lowlight.listLanguages()
|
||||||
const nodes = language && languages.includes(language)
|
const nodes = language && languages.includes(language)
|
||||||
? getHighlightNodes(lowlight.highlight(language, block.node.textContent))
|
? 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)
|
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({
|
return new Plugin({
|
||||||
key: new PluginKey('lowlight'),
|
key: new PluginKey('lowlight'),
|
||||||
|
|
||||||
state: {
|
state: {
|
||||||
init: (_, { doc }) => getDecorations({ doc, name, lowlight }),
|
init: (_, { doc }) => getDecorations({
|
||||||
|
doc,
|
||||||
|
name,
|
||||||
|
lowlight,
|
||||||
|
defaultLanguage,
|
||||||
|
}),
|
||||||
apply: (transaction, decorationSet, oldState, newState) => {
|
apply: (transaction, decorationSet, oldState, newState) => {
|
||||||
const oldNodeName = oldState.selection.$head.parent.type.name
|
const oldNodeName = oldState.selection.$head.parent.type.name
|
||||||
const newNodeName = newState.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)
|
return decorationSet.map(transaction.mapping, transaction.doc)
|
||||||
|
Loading…
Reference in New Issue
Block a user