tiptap/packages/extension-code/index.ts

57 lines
973 B
TypeScript
Raw Normal View History

2020-09-24 06:29:05 +08:00
import {
2020-10-22 18:34:49 +08:00
Command, createMark, markInputRule, markPasteRule,
2020-09-24 06:29:05 +08:00
} from '@tiptap/core'
2020-09-22 15:08:08 +08:00
2020-10-22 18:34:49 +08:00
// export type CodeCommand = () => Command
2020-04-02 19:26:56 +08:00
2020-10-22 18:34:49 +08:00
// declare module '@tiptap/core/src/Editor' {
// interface Commands {
// code: CodeCommand,
// }
// }
2020-04-02 19:26:56 +08:00
2020-09-10 00:47:47 +08:00
export const inputRegex = /(?:^|\s)((?:`)((?:[^`]+))(?:`))$/gm
export const pasteRegex = /(?:^|\s)((?:`)((?:[^`]+))(?:`))/gm
2020-10-22 18:34:49 +08:00
export default createMark({
name: 'code',
excludes: '_',
parseHTML() {
return [
2020-09-09 05:44:45 +08:00
{ tag: 'code' },
2020-10-22 18:34:49 +08:00
]
},
renderHTML({ attributes }) {
return ['strong', attributes, 0]
},
addCommands() {
return {
code: () => ({ commands }) => {
return commands.toggleMark('code')
},
}
},
addKeyboardShortcuts() {
return {
'Mod-`': () => this.editor.code(),
}
},
addInputRules() {
return [
markInputRule(inputRegex, this.type),
]
},
addPasteRules() {
return [
markPasteRule(inputRegex, this.type),
]
},
})