diff --git a/.changeset/nervous-hairs-walk.md b/.changeset/nervous-hairs-walk.md new file mode 100644 index 0000000000..c7d5e4b80f --- /dev/null +++ b/.changeset/nervous-hairs-walk.md @@ -0,0 +1,5 @@ +--- +"@tiptap/core": minor +--- + +Support `validate` options in node and mark attribute definitions. diff --git a/packages/core/src/helpers/getAttributesFromExtensions.ts b/packages/core/src/helpers/getAttributesFromExtensions.ts index 9d26508075..4b6638a30c 100644 --- a/packages/core/src/helpers/getAttributesFromExtensions.ts +++ b/packages/core/src/helpers/getAttributesFromExtensions.ts @@ -11,8 +11,9 @@ export function getAttributesFromExtensions(extensions: Extensions): ExtensionAt const extensionAttributes: ExtensionAttribute[] = [] const { nodeExtensions, markExtensions } = splitExtensions(extensions) const nodeAndMarkExtensions = [...nodeExtensions, ...markExtensions] - const defaultAttribute: Required = { + const defaultAttribute: Required> & Pick = { default: null, + validate: undefined, rendered: true, renderHTML: null, parseHTML: null, diff --git a/packages/core/src/helpers/getSchemaByResolvedExtensions.ts b/packages/core/src/helpers/getSchemaByResolvedExtensions.ts index d8585425e2..074b1b62d0 100644 --- a/packages/core/src/helpers/getSchemaByResolvedExtensions.ts +++ b/packages/core/src/helpers/getSchemaByResolvedExtensions.ts @@ -71,7 +71,7 @@ export function getSchemaByResolvedExtensions(extensions: Extensions, editor?: E isolating: callOrReturn(getExtensionField(extension, 'isolating', context)), attrs: Object.fromEntries( extensionAttributes.map(extensionAttribute => { - return [extensionAttribute.name, { default: extensionAttribute?.attribute?.default }] + return [extensionAttribute.name, { default: extensionAttribute?.attribute?.default, validate: extensionAttribute?.attribute?.validate }] }), ), }) @@ -132,7 +132,7 @@ export function getSchemaByResolvedExtensions(extensions: Extensions, editor?: E code: callOrReturn(getExtensionField(extension, 'code', context)), attrs: Object.fromEntries( extensionAttributes.map(extensionAttribute => { - return [extensionAttribute.name, { default: extensionAttribute?.attribute?.default }] + return [extensionAttribute.name, { default: extensionAttribute?.attribute?.default, validate: extensionAttribute?.attribute?.validate }] }), ), }) diff --git a/packages/core/src/types.ts b/packages/core/src/types.ts index fd6e3221f6..690f8e69c7 100644 --- a/packages/core/src/types.ts +++ b/packages/core/src/types.ts @@ -225,6 +225,7 @@ export type KeyboardShortcutCommand = (props: { editor: Editor }) => boolean export type Attribute = { default?: any + validate?: string | ((value: any) => void) rendered?: boolean renderHTML?: ((attributes: Record) => Record | null) | null parseHTML?: ((element: HTMLElement) => any | null) | null @@ -239,7 +240,7 @@ export type Attributes = { export type ExtensionAttribute = { type: string name: string - attribute: Required + attribute: Required> & Pick } export type GlobalAttributes = {