tiptap/packages/core/src/utils/injectExtensionAttributesToParseRule.ts

39 lines
1019 B
TypeScript
Raw Normal View History

import { ParseRule } from 'prosemirror-model'
import { ExtensionAttribute } from '../types'
export default function injectExtensionAttributesToParseRule(parseRule: ParseRule, extensionAttributes: ExtensionAttribute[]): ParseRule {
if (parseRule.style) {
return parseRule
}
return {
...parseRule,
getAttrs: node => {
const oldAttributes = parseRule.getAttrs
? parseRule.getAttrs(node)
: parseRule.attrs
if (oldAttributes === false) {
return false
}
const newAttributes = extensionAttributes
.filter(item => item.attribute.rendered)
.reduce((items, item) => {
const attributes = item.attribute.parseHTML
? item.attribute.parseHTML(node as HTMLElement)
: {
[item.name]: (node as HTMLElement).dataset[item.name],
}
return {
...items,
...attributes,
}
}, {})
return { ...oldAttributes, ...newAttributes }
},
}
}