tiptap/packages/extension-bold/index.ts

51 lines
1.3 KiB
TypeScript
Raw Normal View History

2020-09-22 05:17:30 +08:00
import { Command, Mark, markInputRule, markPasteRule } from '@tiptap/core'
export type BoldCommand = () => Command
2020-03-30 18:40:25 +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
bold: BoldCommand,
2020-04-01 04:17:54 +08:00
}
}
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-22 05:17:30 +08:00
.commands(({ name }) => ({
bold: () => ({ commands }) => {
return commands.toggleMark(name)
2020-09-09 05:44:45 +08:00
},
}))
.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()