tiptap/packages/extension-italic/index.ts

45 lines
1.1 KiB
TypeScript
Raw Normal View History

2020-09-24 06:29:05 +08:00
import {
Command, Mark, markInputRule, markPasteRule,
} from '@tiptap/core'
2020-09-22 05:17:30 +08:00
export type ItalicCommand = () => Command
2020-03-31 06:07:12 +08:00
2020-04-01 04:17:54 +08:00
declare module '@tiptap/core/src/Editor' {
2020-09-22 16:49:38 +08:00
interface Commands {
2020-09-22 05:17:30 +08:00
italic: ItalicCommand,
2020-04-01 04:17:54 +08:00
}
}
2020-09-24 06:37:31 +08:00
export const starInputRegex = /(?:^|\s)((?:\*)((?:[^*]+))(?:\*))$/gm
export const starPasteRegex = /(?:^|\s)((?:\*)((?:[^*]+))(?:\*))/gm
2020-09-10 00:47:47 +08:00
export const underscoreInputRegex = /(?:^|\s)((?:_)((?:[^_]+))(?:_))$/gm
export const underscorePasteRegex = /(?:^|\s)((?:_)((?:[^_]+))(?:_))/gm
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],
}))
2020-09-22 05:17:30 +08:00
.commands(({ name }) => ({
italic: () => ({ commands }) => {
return commands.toggleMark(name)
2020-09-09 05:44:45 +08:00
},
}))
.keys(({ editor }) => ({
2020-09-24 06:29:05 +08:00
'Mod-i': () => editor.italic(),
2020-09-09 05:44:45 +08:00
}))
2020-09-10 00:47:47 +08:00
.inputRules(({ type }) => [
markInputRule(starInputRegex, type),
markInputRule(underscoreInputRegex, type),
])
.pasteRules(({ type }) => [
markPasteRule(starPasteRegex, type),
markPasteRule(underscorePasteRegex, type),
])
2020-09-09 05:44:45 +08:00
.create()