tiptap/packages/extension-italic/index.ts

61 lines
1.3 KiB
TypeScript
Raw Normal View History

2020-09-09 05:44:45 +08:00
import { Mark, markInputRule, markPasteRule } from '@tiptap/core'
2020-04-02 18:41:52 +08:00
import VerEx from 'verbal-expressions'
2020-03-31 06:07:12 +08:00
2020-04-01 04:17:54 +08:00
declare module '@tiptap/core/src/Editor' {
interface Editor {
italic(): Editor,
}
}
2020-09-09 05:44:45 +08:00
export default new Mark()
.name('italic')
.schema(() => ({
parseDOM: [
{ tag: 'i' },
{ tag: 'em' },
{ style: 'font-style=italic' },
],
toDOM: () => ['em', 0],
}))
.commands(({ editor, name }) => ({
italic: next => () => {
editor.toggleMark(name)
next()
},
}))
.keys(({ editor }) => ({
'Mod-i': () => editor.italic()
}))
2020-09-10 00:07:17 +08:00
.inputRules(({ type }) => {
return ['*', '_'].map(character => {
const regex = VerEx()
.add('(?:^|\\s)')
.beginCapture()
.find(character)
.beginCapture()
.somethingBut(character)
.endCapture()
.find(character)
.endCapture()
.endOfLine()
2020-09-09 05:44:45 +08:00
2020-09-10 00:07:17 +08:00
return markInputRule(regex, type)
})
})
.pasteRules(({ type }) => {
return ['*', '_'].map(character => {
const regex = VerEx()
.add('(?:^|\\s)')
.beginCapture()
.find(character)
.beginCapture()
.somethingBut(character)
.endCapture()
.find(character)
.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()