Coder Social home page Coder Social logo

zod-matter's Introduction

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.

zod-matter's People

Contributors

hideoo avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.