tiptap/packages/extension-code/index.ts

38 lines
836 B
TypeScript
Raw Normal View History

2020-09-22 15:08:08 +08:00
import { Command, Mark, markInputRule, markPasteRule } from '@tiptap/core'
export type CodeCommand = () => Command
2020-04-02 19:26:56 +08:00
declare module '@tiptap/core/src/Editor' {
interface Editor {
2020-09-22 15:08:08 +08:00
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-09-09 05:44:45 +08:00
export default new Mark()
.name('code')
.schema(() => ({
excludes: '_',
parseDOM: [
{ tag: 'code' },
],
toDOM: () => ['code', 0],
}))
2020-09-22 15:08:08 +08:00
.commands(({ name }) => ({
code: () => ({ commands }) => {
return commands.toggleMark(name)
},
}))
2020-09-09 05:44:45 +08:00
.keys(({ editor }) => ({
'Mod-`': () => editor.code()
}))
2020-09-10 00:47:47 +08:00
.inputRules(({ type }) => [
markInputRule(inputRegex, type)
])
.pasteRules(({ type }) => [
markPasteRule(inputRegex, type)
])
2020-09-09 05:44:45 +08:00
.create()