tiptap/packages/extension-code/index.ts
2020-10-22 23:21:52 +02:00

57 lines
945 B
TypeScript

import {
Command, createMark, markInputRule, markPasteRule,
} from '@tiptap/core'
export const inputRegex = /(?:^|\s)((?:`)((?:[^`]+))(?:`))$/gm
export const pasteRegex = /(?:^|\s)((?:`)((?:[^`]+))(?:`))/gm
const Code = createMark({
name: 'code',
excludes: '_',
parseHTML() {
return [
{ tag: 'code' },
]
},
renderHTML({ attributes }) {
return ['code', attributes, 0]
},
addCommands() {
return {
code: (): Command => ({ commands }) => {
return commands.toggleMark('code')
},
}
},
addKeyboardShortcuts() {
return {
'Mod-`': () => this.editor.code(),
}
},
addInputRules() {
return [
markInputRule(inputRegex, this.type),
]
},
addPasteRules() {
return [
markPasteRule(inputRegex, this.type),
]
},
})
export default Code
declare module '@tiptap/core/src/Editor' {
interface AllExtensions {
Code: typeof Code,
}
}