mirror of
https://github.com/ueberdosis/tiptap.git
synced 2024-12-23 08:47:50 +08:00
39 lines
1019 B
TypeScript
39 lines
1019 B
TypeScript
|
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 }
|
||
|
},
|
||
|
}
|
||
|
}
|