HiDeoo / zod-matter

Typesafe front matter

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

zod-matter 🏷️

Typesafe front matter

Features

zod-matter is a tiny wrapper for gray-matter and Zod to parse and validate front matter with static type inference.

gray-matter is a great package to parse front matter but provides no validation or type safety. This package exposes an API adding a schema parameter to validate front matter data using Zod. This can be particularly useful to parse & validate front matter for user generated Markdown or MDX files for example.

Installation

Install zod-matter and its peer dependencies with your favorite package manager, e.g.:

pnpm add zod-matter zod gray-matter

Usage

parse

Extracts and parses front matter according to the given Zod schema.

import { parse } from 'zod-matter'
import { z } from 'zod'

const { data } = parse('---\nauthor: HiDeoo\n---\n# Hello world!', z.object({ author: z.string() }))
//       ^? { author: string }

Parameters

  • input: The string, buffer or object with a content property to parse.
  • schema: The Zod schema to use to parse the front matter.
  • options: Optionally, the gray-matter options to use.

Return value

A file object with the same properties of a gray-matter file object but with a data property of the type inferred from the given Zod schema.

If the front matter data is invalid, a ZodError will be thrown.

read

Extracts and parses front matter from a file according to the given Zod schema.

import { read } from 'zod-matter'
import { z } from 'zod'

const { data } = read('./data/post.md', z.object({ author: z.string(), date: z.date() }))
//       ^? { author: string; date: Date }

Parameters

  • path: The path to the file to read and parse.
  • schema: The Zod schema to use to parse the front matter.
  • options: Optionally, the gray-matter options to use.

Return value

A file object with the same properties of a gray-matter file object but with a data property of the type inferred from the given Zod schema.

If the front matter data is invalid, a ZodError will be thrown.

stringify

Stringify an object to YAML or a specified language, and append it to the given string.

import { stringify } from 'zod-matter'
import { z } from 'zod'

const content = stringify('# Hello world!', { author: 'HiDeoo' })
//       ^? ---
//          author: HiDeoo
//          ---
//          # Hello world!

This function is re-exported for convenience from gray-matter with the same signature.

test

Checks if the given string contains a front matter.

import { test } from 'zod-matter'
import { z } from 'zod'

const containsFrontMatter = test('---\nauthor: HiDeoo\n---\n# Hello world!')
//            ^? true

This function is re-exported for convenience from gray-matter with the same signature.

License

Licensed under the MIT License, Copyright © HiDeoo.

See LICENSE for more information.

About

Typesafe front matter

License:MIT License


Languages

Language:TypeScript 99.4%Language:Shell 0.6%