diff --git a/src/schema/definePageType.ts b/src/schema/definePageType.ts index 87e6908..35a1e34 100644 --- a/src/schema/definePageType.ts +++ b/src/schema/definePageType.ts @@ -1,17 +1,15 @@ import { compact, get } from 'lodash'; -import { defineField, defineType, DocumentDefinition, SlugOptions } from 'sanity'; +import { defineField, defineType, DocumentDefinition } from 'sanity'; import { PageTreeField } from '../components/PageTreeField'; import { SlugField } from '../components/SlugField'; -import { PageTreeConfig } from '../types'; +import { PageTreeConfig, GlobalOptions } from '../types'; import { slugValidator } from '../validators/slug-validator'; import { allowedParentValidator } from '../validators/parent-validator'; -type Options = { +type Options = GlobalOptions & { isRoot?: boolean; - fieldsGroupName?: string; - slugSource?: SlugOptions['source']; }; function getPossibleParentsFromConfig(config: PageTreeConfig, ownType: DocumentDefinition): string[] { @@ -26,6 +24,7 @@ export const definePageType = ( config: PageTreeConfig, options: Options = { isRoot: false }, ) => { + options = {...config.globalOptions, ...options}; const slugSourceFieldName = getSlugSourceField(config, options); let slugSourceField; @@ -56,7 +55,10 @@ const basePageFields = (config: PageTreeConfig, options: Options, ownType: Docum components: { input: props => SlugField({ ...props, config }), }, - validation: Rule => Rule.required().custom(slugValidator(config)), + validation: Rule => [ + Rule.required().custom(slugValidator(config)), + ...toArray(options.slugValidationRules?.(Rule)) + ], group: options.fieldsGroupName, }), ] @@ -79,3 +81,4 @@ const basePageFields = (config: PageTreeConfig, options: Options, ownType: Docum ]; const getSlugSourceField = (config: PageTreeConfig, options: Options) => config.titleFieldName ?? options.slugSource; +const toArray = (t: undefined | T | T[]) => t === undefined ? [] : Array.isArray(t) ? t : [t]; diff --git a/src/types.ts b/src/types.ts index f5c0910..ed344da 100644 --- a/src/types.ts +++ b/src/types.ts @@ -1,4 +1,5 @@ import { DocumentListBuilder } from 'sanity/structure'; +import { SlugOptions, SlugRule, SlugValue, ValidationBuilder } from 'sanity'; export type SanityRef = { _ref: string; @@ -35,29 +36,37 @@ export type PageTreeItem = RawPageMetadataWithPublishedState & { path: string; }; +export type GlobalOptions = { + fieldsGroupName?: string; + slugSource?: SlugOptions['source']; + slugValidationRules?: ValidationBuilder +} + /** * @public */ export type PageTreeConfig = { - /* Api version that is used throughout your project */ + /** Api version that is used throughout your project */ apiVersion: string; - /* Root page schema type name, e.g. "homePage" */ + /** Root page schema type name, e.g. "homePage" */ rootSchemaType: string; - /* All your page schema type names, e.g. ["homePage", "contentPage"] */ + /** All your page schema type names, e.g. ["homePage", "contentPage"] */ pageSchemaTypes: string[]; - /* Field name of your page documents */ + /** @deprecated Use globalOptions.slugSource instead. Field name of your page documents */ titleFieldName?: string; - /* Optionally specify which document types can be the parent of a document type */ + /** Optionally specify which document types can be the parent of a document type */ allowedParents?: Record; - /* Used for creating page link on the editor page */ + /** Used for creating page link on the editor page */ baseUrl?: string; - /* This plugin supports the document-internationalization plugin. To use it properly, provide the supported languages. */ + /** This plugin supports the document-internationalization plugin. To use it properly, provide the supported languages. */ documentInternationalization?: { - /* Array of supported language code strings, e.g. ["en", "nl"]. These will be used in root pages and when creating a new child page it will set the language field based on the parent page. */ + /** Array of supported language code strings, e.g. ["en", "nl"]. These will be used in root pages and when creating a new child page it will set the language field based on the parent page. */ supportedLanguages: string[]; - /* Optional field name of the language field, defaults to "language" */ + /** Optional field name of the language field, defaults to "language" */ languageFieldName?: string; }; + /** Define options that apply to all pages. Can be overridden by options supplied using definePageType */ + globalOptions?: GlobalOptions }; /**