improve attribute handling

This commit is contained in:
Philipp Kühn
2020-10-27 14:53:08 +01:00
parent fd278f922f
commit 77f67bceae
3 changed files with 4 additions and 4 deletions

View File

@@ -7,8 +7,8 @@ export type Extensions = (Extension | NodeExtension | MarkExtension)[]
export type Attribute = { export type Attribute = {
default: any, default: any,
rendered?: boolean, rendered?: boolean,
renderHTML?: ((attributes: { [key: string]: any }) => any) | null, renderHTML?: ((attributes: { [key: string]: any }) => { [key: string]: any } | null) | null,
parseHTML?: ((element: HTMLElement) => { [key: string]: any }) | null, parseHTML?: ((element: HTMLElement) => { [key: string]: any } | null) | null,
} }
export type Attributes = { export type Attributes = {

View File

@@ -12,7 +12,7 @@ export default function getRenderedAttributes(nodeOrMark: Node | Mark, extension
} }
} }
return item.attribute.renderHTML(nodeOrMark.attrs) return item.attribute.renderHTML(nodeOrMark.attrs) || {}
}) })
.reduce((attributes, attribute) => { .reduce((attributes, attribute) => {
return mergeAttributes(attributes, attribute) return mergeAttributes(attributes, attribute)

View File

@@ -27,7 +27,7 @@ export default function injectExtensionAttributesToParseRule(parseRule: ParseRul
.filter(item => item.attribute.rendered) .filter(item => item.attribute.rendered)
.reduce((items, item) => { .reduce((items, item) => {
const attributes = item.attribute.parseHTML const attributes = item.attribute.parseHTML
? item.attribute.parseHTML(node as HTMLElement) ? item.attribute.parseHTML(node as HTMLElement) || {}
: { : {
[item.name]: (node as HTMLElement).getAttribute(item.name), [item.name]: (node as HTMLElement).getAttribute(item.name),
} }