UNPKG

zod-matter

Version:
60 lines (57 loc) 1.94 kB
/// <reference path="../types/gray-matter.d.ts" /> import matter from 'gray-matter'; import { AnyZodObject, z } from 'zod'; /** * Extracts and parses front matter according to the given Zod schema. * * @example * ```ts * import { parse } from 'zod-matter' * import { z } from 'zod' * * const frontMatter = parse( * input, * z.object({ * author: z.string(), * date: z.date(), * category: z.string().optional(), * }) * ) * ``` * * @param input - The string, buffer or object with a `content` property to parse. * @param schema - The Zod schema to use to parse the front matter. * @param options - The gray-matter options to use. */ declare function parse<TInput extends matter.Input, TSchema extends AnyZodObject, TOptions extends matter.GrayMatterOption<TInput, TOptions>>(input: TInput | { content: TInput; }, schema: TSchema, options?: TOptions): ZodMatterFile<TSchema, TInput>; /** * Extracts and parses front matter from a file according to the given Zod schema. * * @example * ```ts * import { read } from 'zod-matter' * import { z } from 'zod' * * const frontMatter = read( * path, * z.object({ * author: z.string(), * date: z.date(), * category: z.string().optional(), * }) * ) * ``` * * @param path - The path to the file to read and parse. * @param schema - The Zod schema to use to parse the front matter. * @param options - The gray-matter options to use. */ declare function read<TSchema extends AnyZodObject, TOptions extends matter.GrayMatterOption<string, TOptions>>(path: string, schema: TSchema, options?: TOptions): ZodMatterFile<TSchema, string>; declare const stringify: typeof matter.stringify; declare const test: typeof matter.test; type ZodMatterFile<TSchema extends AnyZodObject, TInput extends matter.Input> = Omit<matter.GrayMatterFile<TInput>, 'data'> & { data: z.infer<TSchema>; }; export { parse, read, stringify, test };