mirror of
https://github.com/ueberdosis/tiptap.git
synced 2024-12-25 02:07:49 +08:00
92 lines
2.0 KiB
TypeScript
92 lines
2.0 KiB
TypeScript
import { Command, createNode } from '@tiptap/core'
|
|
import { textblockTypeInputRule } from 'prosemirror-inputrules'
|
|
|
|
export interface CodeBlockOptions {
|
|
languageClassPrefix: string,
|
|
}
|
|
|
|
export const backtickInputRegex = /^```(?<language>[a-z]*)? $/
|
|
export const tildeInputRegex = /^~~~(?<language>[a-z]*)? $/
|
|
|
|
const CodeBlock = createNode({
|
|
name: 'code_block',
|
|
|
|
defaultOptions: <CodeBlockOptions>{
|
|
languageClassPrefix: 'language-',
|
|
},
|
|
|
|
content: 'text*',
|
|
|
|
marks: '',
|
|
|
|
group: 'block',
|
|
|
|
code: true,
|
|
|
|
defining: true,
|
|
|
|
addAttributes() {
|
|
return {
|
|
language: {
|
|
default: null,
|
|
rendered: false,
|
|
},
|
|
}
|
|
},
|
|
|
|
parseHTML() {
|
|
return [
|
|
{
|
|
tag: 'pre',
|
|
preserveWhitespace: 'full',
|
|
getAttrs: node => {
|
|
const classAttribute = (node as Element).firstElementChild?.getAttribute('class')
|
|
|
|
if (!classAttribute) {
|
|
return null
|
|
}
|
|
|
|
const regexLanguageClassPrefix = new RegExp(`^(${this.options.languageClassPrefix})`)
|
|
|
|
return { language: classAttribute.replace(regexLanguageClassPrefix, '') }
|
|
},
|
|
},
|
|
]
|
|
},
|
|
|
|
renderHTML({ node, attributes }) {
|
|
return ['pre', attributes, ['code', {
|
|
class: node.attrs.language && this.options.languageClassPrefix + node.attrs.language,
|
|
}, 0]]
|
|
},
|
|
|
|
addCommands() {
|
|
return {
|
|
codeBlock: (attrs?: CodeBlockOptions): Command => ({ commands }) => {
|
|
return commands.toggleBlockType('code_block', 'paragraph', attrs)
|
|
},
|
|
}
|
|
},
|
|
|
|
addKeyboardShortcuts() {
|
|
return {
|
|
'Mod-Shift-c': () => this.editor.codeBlock(),
|
|
}
|
|
},
|
|
|
|
addInputRules() {
|
|
return [
|
|
textblockTypeInputRule(backtickInputRegex, this.type, ({ groups }: any) => groups),
|
|
textblockTypeInputRule(tildeInputRegex, this.type, ({ groups }: any) => groups),
|
|
]
|
|
},
|
|
})
|
|
|
|
export default CodeBlock
|
|
|
|
declare module '@tiptap/core/src/Editor' {
|
|
interface AllExtensions {
|
|
CodeBlock: typeof CodeBlock,
|
|
}
|
|
}
|