import type * as core from '@contentlayer2/core' import type { Thunk } from '@contentlayer2/utils' import type { ComputedField } from './computed-field.js' import type { FieldDef, FieldDefWithName } from './field.js' export * from './field.js' export type SchemaDef = { documentTypeDefs: DocumentTypeDef[] } export type DocumentContentType = 'markdown' | 'mdx' | 'data' export type TypeExtensions = { stackbit?: core.StackbitExtension.TypeExtension } export type FieldDefs = Record | FieldDefWithName[] export type DocumentTypeDef = { name: DefName description?: string /** * The field definitions can either be provided as an object with the field names as keys or * as an array of all field definitions including the name as an extra field. (The array definition * can be used if you want more control over the order of the fields.) * * @default [] */ fields?: FieldDefs computedFields?: ComputedFields /** Path is relative to the `contentDirPath` config */ filePathPattern?: string // | ((doc: Document) => string) /** * Default is `markdown` * * Choose `data` e.g. for a `.json` or `.yaml` file */ contentType?: DocumentContentType isSingleton?: boolean extensions?: TypeExtensions } export type ComputedFields = Record> export type NestedTypeDef = { // type: 'NestedTypeDef' name: DefName description?: string /** @default [] */ fields?: FieldDefs extensions?: TypeExtensions } export const isNestedTypeDef = (_: NestedTypeDef | NestedUnnamedTypeDef): _ is NestedTypeDef => _.hasOwnProperty('name') export type NestedUnnamedTypeDef = { // type: 'NestedUnnamedTypeDef' /** @default [] */ fields?: FieldDefs extensions?: TypeExtensions } export const isNestedUnnamedTypeDef = (_: NestedTypeDef | NestedUnnamedTypeDef): _ is NestedUnnamedTypeDef => !_.hasOwnProperty('name') // export type FieldType = // | 'string' // | 'text' // | 'slug' // /** Reference to owned embedded, (= "inline model") */ // | 'nested' // // /** Reference to owned model */ // // | 'model' // /** Reference to document */ // | 'reference' export type NestedType = { type: 'nested' def: Thunk | NestedUnnamedTypeDef> } export type DocumentType = { type: 'document'; def: Thunk> } // `` cast here is needed here to flip variance (see https://github.com/contentlayerdev/contentlayer/issues/33) export type DocumentTypes = DocumentType[] | Record> export const defineNestedType = ( def: Thunk | NestedUnnamedTypeDef>, // NOTE we're not using the generic `DefName` here because it causes problems with when using the defined document type ): NestedType => ({ type: 'nested', def, }) export const defineDocumentType = ( def: Thunk>, // NOTE we're not using the generic `DefName` here because it causes problems with when using the defined document type ): DocumentType => ({ type: 'document', def, }) export const defineFields = (fields: TFieldDefs): TFieldDefs => fields export const defineComputedFields = < TDefName extends string, TComputedFields extends ComputedFields = ComputedFields, >( computedFields: TComputedFields, ): TComputedFields => computedFields