@github-docs/frontmatter

中文文档
1.3.1发布于 2020/03/28MITCommonJS近 30 天下载 4112

Parse and validate YAML frontmatter

原始使用文档
文档目录7 节
  1. Installation
  2. Features
  3. Usage
  4. API
  5. frontmatter(markdown, [options])
  6. frontmatter.stringify(markdown, [data], [opts])
  7. License

@github-docs/frontmatter

Parse and validate YAML frontmatter

This is a frontmatter parser built on gray-matter that supports optional frontmatter validation using a revalidator JSON schema.

Installation

This is a Node.js module available through the npm registry.

npm install @github-docs/frontmatter

Features

  • Make frontmatter entries required or optional
  • Validate value type, length, pattern, etc. See the revalidator#schema.
  • Validate urls, emails, IP addresses, dates, times, etc. See revalidator#format.
  • Set an explicit list of allowable values with enum.
  • Enforce a specific order of frontmatter values with validateKeyOrder
  • Disallow values that are not specified in the schema with validateKeyNames

Usage

const frontmatter = require('@github-docs/frontmatter')

const schema = {
  properties: {
    title: {
      type: 'string',
      required: true
    },
    meaning_of_life: {
      type: 'number',
      minimum: 40,
      maximum: 50
    }
  }
}

const markdown = `---
title: Hello, World
meaning_of_life: 42
---

I am content.
`

const { data, content, errors } = frontmatter(markdown)

API

frontmatter(markdown, [options])

Parses a string containing markdown and (optional) frontmatter.

  • markdown String (required) - the contents of a markdown file that includes YAML frontmatter.
  • options Object (optional)
    • schema Object - A revalidator JSON schema.
    • filepath String - The name of the file being parsed. Useful for debugging when errors occur.
    • validateKeyNames Boolean - If true, checks that all keys are specified as schema properties. Defaults to false
    • validateKeyOrder Boolean - If true, checks that all keys are in the same order they appear in the schema. Defaults to false

frontmatter.stringify(markdown, [data], [opts])

This is the same stringify method exported by the gray-matter module, which can be used to join a markdown string and a frontmatter object together as a single string.

  • file StringObject - The content string to append to stringified front-matter, or a file object with file.content string.
  • data Object - Front matter to stringify.
  • options Object - Options to pass to gray-matter and js-yaml.
  • returns String - Returns a string created by wrapping stringified yaml with delimiters, and appending that to the given string.

License

MIT

版本与发布记录

@github-docs/frontmatter 共发布 6 个版本,首次发布于 2019/09/11;过去 12 个月发布了 0 个版本。

版本发布日期距今
1.3.1latest2020/03/286 年前
1.3.02019/12/116 年前
1.2.12019/11/017 年前
1.2.02019/09/117 年前
1.1.02019/09/117 年前
1.0.02019/09/117 年前

关于 @github-docs/frontmatter 的常见问题

@github-docs/frontmatter 是什么?

Parse and validate YAML frontmatter

如何安装 @github-docs/frontmatter?

在项目目录运行 npm install @github-docs/frontmatter,或使用 pnpm add / yarn add / bun add。

@github-docs/frontmatter 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/github-docs__frontmatter 是否存在。

@github-docs/frontmatter 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@github-docs/frontmatter 可以商用吗?

@github-docs/frontmatter 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@github-docs/frontmatter 的下载量有多少?

根据 npm 官方下载统计,@github-docs/frontmatter 在 2026/10/17 – 2026/11/15 这 30 天内被下载 4,112 次,日均约 137 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@github-docs/frontmatter 还在维护吗?

最新版本 1.3.1 发布于 2020/03/28(6 年前);至今共发布 6 个版本,其中过去 12 个月发布了 0 个;首次发布于 2019/09/11。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。