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

57 lines
947 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 ['strong', 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,
}
}