tiptap/packages/extension-bold/index.ts

50 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-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-10 00:47:47 +08:00
export const starInputRegex = /(?:^|\s)((?:\*\*)((?:[^\*\*]+))(?:\*\*))$/gm
export const starPasteRegex = /(?:^|\s)((?:\*\*)((?:[^\*\*]+))(?:\*\*))/gm
export const underscoreInputRegex = /(?:^|\s)((?:__)((?:[^__]+))(?:__))$/gm
export const underscorePasteRegex = /(?:^|\s)((?:__)((?:[^__]+))(?:__))/gm
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-10 17:20:30 +08:00
.commands(({ editor, name }) => ({
2020-09-09 05:44:45 +08:00
bold: next => () => {
editor.toggleMark(name)
next()
},
}))
.keys(({ editor }) => ({
'Mod-b': () => editor.bold()
}))
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()