From aef8c2a0237bb1728c7ab21b72d4a723d768842a Mon Sep 17 00:00:00 2001 From: Maxwell Weru Date: Fri, 14 Jun 2024 11:40:22 +0300 Subject: [PATCH] Use an object as the argument for `ImageFunction` to allow for more options later (#128) --- .changeset/forty-spiders-glow.md | 5 +++++ packages/markdownlayer/src/core/generation/image.ts | 8 ++++++-- packages/markdownlayer/src/core/generation/index.ts | 6 +++--- packages/markdownlayer/src/core/types.ts | 11 +++++++++-- 4 files changed, 23 insertions(+), 7 deletions(-) create mode 100644 .changeset/forty-spiders-glow.md diff --git a/.changeset/forty-spiders-glow.md b/.changeset/forty-spiders-glow.md new file mode 100644 index 0000000..84a3456 --- /dev/null +++ b/.changeset/forty-spiders-glow.md @@ -0,0 +1,5 @@ +--- +'markdownlayer': minor +--- + +Use an object as the argument for `ImageFunction` to allow for more options later diff --git a/packages/markdownlayer/src/core/generation/image.ts b/packages/markdownlayer/src/core/generation/image.ts index 68753d7..33ecbef 100644 --- a/packages/markdownlayer/src/core/generation/image.ts +++ b/packages/markdownlayer/src/core/generation/image.ts @@ -3,9 +3,13 @@ import type { StaticImageData } from 'next/dist/shared/lib/image-external'; // t import path from 'path'; import { z } from 'zod'; -import type { GenerationMode, StaticImageDataSchema } from '../types'; +import type { GenerationMode, ImageSchemaFunctionOptions, StaticImageDataSchema } from '../types'; -type CreateImageOptions = { optional?: boolean; mode: GenerationMode; shouldEmitFile: boolean; sourceFilePath: string }; +type CreateImageOptions = ImageSchemaFunctionOptions & { + mode: GenerationMode; + shouldEmitFile: boolean; + sourceFilePath: string; +}; export function createImage({ optional, shouldEmitFile, sourceFilePath }: CreateImageOptions): StaticImageDataSchema { const schema = optional ? z.string().optional() : z.string(); diff --git a/packages/markdownlayer/src/core/generation/index.ts b/packages/markdownlayer/src/core/generation/index.ts index e8de4d1..dddb705 100644 --- a/packages/markdownlayer/src/core/generation/index.ts +++ b/packages/markdownlayer/src/core/generation/index.ts @@ -241,12 +241,12 @@ async function generateDocuments(options: GenerateDocsOptions): Promise + image: (options) => // @ts-expect-error - The type is correct but the error is due to the transform function - (optional ? z.string().optional() : z.string()).transform( + (options?.optional ? z.string().optional() : z.string()).transform( (): StaticImageData => ({ src: '', height: 0, width: 0 }), ), - // image: (optional) => createImage({ optional, shouldEmitFile: false, mode, sourceFilePath }), + // image: (options) => createImage({ ...options, shouldEmitFile: false, mode, sourceFilePath }), }); } diff --git a/packages/markdownlayer/src/core/types.ts b/packages/markdownlayer/src/core/types.ts index 39ca48b..b670543 100644 --- a/packages/markdownlayer/src/core/types.ts +++ b/packages/markdownlayer/src/core/types.ts @@ -43,12 +43,19 @@ export type StaticImageDataSchema = ZodObject<{ >; }>; +export type ImageSchemaFunctionOptions = { + /** Whether the image is optional. */ + optional?: boolean; + + // other options like optimization with sharp would go here +}; + /** * Schema for an image. - * @param optional Whether the image is optional. + * @param options - Options for the image schema. * @returns A Zod object representing an image. */ -export type ImageSchemaFunction = (optional?: boolean) => StaticImageDataSchema; +export type ImageSchemaFunction = (options?: ImageSchemaFunctionOptions) => StaticImageDataSchema; type DocumentDefinitionSchemaWithoutEffects = | AnyZodObject