Skip to content

Commit

Permalink
Use an object as the argument for ImageFunction to allow for more o…
Browse files Browse the repository at this point in the history
…ptions later (#128)
  • Loading branch information
mburumaxwell authored Jun 14, 2024
1 parent 4898e1c commit aef8c2a
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 7 deletions.
5 changes: 5 additions & 0 deletions .changeset/forty-spiders-glow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'markdownlayer': minor
---

Use an object as the argument for `ImageFunction` to allow for more options later
8 changes: 6 additions & 2 deletions packages/markdownlayer/src/core/generation/image.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
6 changes: 3 additions & 3 deletions packages/markdownlayer/src/core/generation/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -241,12 +241,12 @@ async function generateDocuments(options: GenerateDocsOptions): Promise<Generati
if (typeof schema === 'function') {
schema = schema({
// TODO: figure out how to handle images in the schema without needing to pass the file path (module augmentation may help)
image: (optional) =>
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 }),
});
}

Expand Down
11 changes: 9 additions & 2 deletions packages/markdownlayer/src/core/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit aef8c2a

Please sign in to comment.