tiptap/packages/extension-bold/index.ts

69 lines
1.6 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-30 18:40:25 +08:00
2020-04-01 04:17:54 +08:00
declare module '@tiptap/core/src/Editor' {
interface Editor {
bold(): Editor,
}
}
2020-09-09 05:44:45 +08:00
export default new Mark()
.name('bold')
.schema(() => ({
parseDOM: [
{
tag: 'strong',
2020-04-02 20:34:07 +08:00
},
2020-09-09 05:44:45 +08:00
{
tag: 'b',
getAttrs: node => (node as HTMLElement).style.fontWeight !== 'normal' && null,
},
{
style: 'font-weight',
getAttrs: value => /^(bold(er)?|[5-9]\d{2,})$/.test(value as string) && null,
},
],
toDOM: () => ['strong', 0],
}))
2020-09-09 17:23:24 +08:00
.commands(({ editor, name, type }) => ({
2020-09-09 05:44:45 +08:00
bold: next => () => {
editor.toggleMark(name)
next()
},
}))
.keys(({ editor }) => ({
'Mod-b': () => editor.bold()
}))
// .inputRules(({ type }) => {
// return ['**', '__'].map(character => {
// const regex = VerEx()
// .add('(?:^|\\s)')
// .beginCapture()
// .find(character)
// .beginCapture()
// .somethingBut(character)
// .endCapture()
// .find(character)
// .endCapture()
// .endOfLine()
2020-04-13 18:27:29 +08:00
2020-09-09 05:44:45 +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-04-02 14:53:59 +08:00
2020-09-09 05:44:45 +08:00
// return markPasteRule(regex, type)
// })
// })
.create()