tiptap/packages/extension-code/index.ts

56 lines
1.1 KiB
TypeScript
Raw Normal View History

2020-09-09 05:44:45 +08:00
import { Mark, markInputRule, markPasteRule } from '@tiptap/core'
2020-04-02 19:26:56 +08:00
import VerEx from 'verbal-expressions'
declare module '@tiptap/core/src/Editor' {
interface Editor {
code(): Editor,
}
}
2020-09-09 05:44:45 +08:00
export default new Mark()
.name('code')
.schema(() => ({
excludes: '_',
parseDOM: [
{ tag: 'code' },
],
toDOM: () => ['code', 0],
}))
.commands(({ editor, name }) => ({
code: next => () => {
editor.toggleMark(name)
next()
},
}))
.keys(({ editor }) => ({
'Mod-`': () => editor.code()
}))
2020-09-10 00:07:17 +08:00
.inputRules(({ type }) => {
const regex = VerEx()
.add('(?:^|\\s)')
.beginCapture()
.find('`')
.beginCapture()
.somethingBut('`')
.endCapture()
.find('`')
.endCapture()
.endOfLine()
2020-09-09 05:44:45 +08:00
2020-09-10 00:07:17 +08:00
return [markInputRule(regex, type)]
})
.pasteRules(({ type }) => {
const regex = VerEx()
.add('(?:^|\\s)')
.beginCapture()
.find('`')
.beginCapture()
.somethingBut('`')
.endCapture()
.find('`')
.endCapture()
2020-09-09 05:44:45 +08:00
2020-09-10 00:07:17 +08:00
return [markPasteRule(regex, type)]
})
2020-09-09 05:44:45 +08:00
.create()