tiptap/packages/extension-horizontal-rule/index.ts

29 lines
625 B
TypeScript
Raw Normal View History

2020-09-22 15:08:08 +08:00
import { Command, Node, nodeInputRule } from '@tiptap/core'
export type HorizontalRuleCommand = () => Command
2020-09-10 18:28:14 +08:00
declare module '@tiptap/core/src/Editor' {
interface Editor {
2020-09-22 15:08:08 +08:00
horizontalRule: HorizontalRuleCommand,
2020-09-10 18:28:14 +08:00
}
}
export default new Node()
.name('horizontalRule')
.schema(() => ({
group: 'block',
parseDOM: [{ tag: 'hr' }],
toDOM: () => ['hr'],
}))
2020-09-22 15:08:08 +08:00
.commands(({ type }) => ({
horizontalRule: () => ({ tr }) => {
tr.replaceSelectionWith(type.create())
2020-09-10 18:28:14 +08:00
2020-09-22 15:08:08 +08:00
return true
},
}))
2020-09-10 18:28:14 +08:00
.inputRules(({ type }) => [
nodeInputRule(/^(?:---|___\s|\*\*\*\s)$/, type),
])
.create()