zod-matter 中文文档教程
zod-物质???️
类型安全的前面内容
功能
zod-matter 是 gray-matter 和 Zod 使用 静态类型推断。
grey-matter 是一个很好的解析前文的包,但不提供没有验证或类型安全 。该包公开了一个 API,添加了 schema
参数,以使用 Zod 验证前文数据。这对于解析和解析特别有用。例如,验证用户生成的 Markdown 或 MDX 文件的前言。
安装
使用您最喜欢的包管理器安装 zod-matter
及其对等依赖项,例如:
pnpm add zod-matter zod gray-matter
使用
parse
根据给定的 Zod 模式提取并解析前面的内容。
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 }
参数
input
:要解析的带有content
属性的字符串、缓冲区或对象。schema
:用于解析前面内容的 Zod 模式。选项
:可选地,使用灰色物质选项。
返回值
与 gray-matter 文件对象具有相同属性的文件对象,但具有从给定 Zod 架构推断出的类型的 data
属性。
如果 Front Matter 数据无效,则会抛出 ZodError
。
read
根据给定的 Zod 模式从文件中提取并解析前面的内容。
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 }
参数
path
:要读取和解析的文件的路径。schema
:用于解析前面内容的 Zod 模式。选项
:可选地,使用灰色物质选项。
返回值
与 gray-matter 文件对象具有相同属性的文件对象,但具有从给定 Zod 架构推断出的类型的 data
属性。
如果 Front Matter 数据无效,则会抛出 ZodError
。
stringify
将对象字符串化为 YAML 或指定语言,并将其附加到给定字符串。
import { stringify } from 'zod-matter'
import { z } from 'zod'
const content = stringify('# Hello world!', { author: 'HiDeoo' })
// ^? ---
// author: HiDeoo
// ---
// # Hello world!
为了方便起见,该函数重新导出,具有相同签名的灰质。
test
检查给定字符串是否包含前面的内容。
import { test } from 'zod-matter'
import { z } from 'zod'
const containsFrontMatter = test('---\nauthor: HiDeoo\n---\n# Hello world!')
// ^? true
为了方便起见,该函数从具有相同签名的灰质中重新导出。
许可证
根据 MIT 许可证获得许可,版权所有 © HiDeoo。
有关更多信息,请参阅许可证。
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 acontent
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.