2020-09-10 18:28:14 +08:00
|
|
|
import { InputRule } from 'prosemirror-inputrules'
|
|
|
|
import { NodeType } from 'prosemirror-model'
|
|
|
|
|
2020-11-05 05:38:52 +08:00
|
|
|
export default function (regexp: RegExp, type: NodeType, getAttributes?: (match: any) => any): InputRule {
|
2020-09-10 18:28:14 +08:00
|
|
|
return new InputRule(regexp, (state, match, start, end) => {
|
2020-11-05 05:38:52 +08:00
|
|
|
const attributes = getAttributes instanceof Function
|
|
|
|
? getAttributes(match)
|
|
|
|
: getAttributes
|
2020-09-10 18:28:14 +08:00
|
|
|
const { tr } = state
|
|
|
|
|
|
|
|
if (match[0]) {
|
2020-11-05 05:38:52 +08:00
|
|
|
tr.replaceWith(start - 1, end, type.create(attributes))
|
2020-09-10 18:28:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return tr
|
|
|
|
})
|
2020-09-24 06:29:05 +08:00
|
|
|
}
|