Skip to main content
On this page

@std/front-matter

Overview Jump to heading

Extracts front matter from strings. Adapted from jxson/front-matter.

Supported formats

JSON

import { test, extractJson } from "@std/front-matter";
import { assertEquals } from "@std/assert";

const str = "---json\n{\"and\": \"this\"}\n---\ndeno is awesome";

assertEquals(test(str), true);
assertEquals(extractJson(str), {
  frontMatter: "{\"and\": \"this\"}",
  body: "deno is awesome",
  attrs: { and: "this" }
});

extract and test support the following delimiters.

---json
{
  "and": "this"
}
---
{
  "is": "JSON"
}

TOML

import { test, extractToml } from "@std/front-matter";
import { assertEquals } from "@std/assert";

const str = "---toml\nmodule = 'front_matter'\n---\ndeno is awesome";

assertEquals(test(str), true);
assertEquals(extractToml(str), {
  frontMatter: "module = 'front_matter'",
  body: "deno is awesome",
  attrs: { module: "front_matter" }
});

extract and test support the following delimiters.

---toml
this = 'is'
---
= toml =
parsed = 'as'
toml = 'data'
= toml =
+++
is = 'that'
not = 'cool?'
+++

YAML

import { test, extractYaml } from "@std/front-matter";
import { assertEquals } from "@std/assert";

const str = "---yaml\nmodule: front_matter\n---\ndeno is awesome";

assertEquals(test(str), true);
assertEquals(extractYaml(str), {
  frontMatter: "module: front_matter",
  body: "deno is awesome",
  attrs: { module: "front_matter" }
});

extract and test support the following delimiters.

---
these: are
---
---yaml
all: recognized
---
= yaml =
as: yaml
= yaml =

Add to your project Jump to heading

deno add jsr:@std/front-matter

See all symbols in @std/front-matter on

什么是 front matter? Jump to heading

Front matter 是放置在文件顶部的元数据,通常用于 Markdown 或其他文本文件中,提供关于文档的信息。它通常被特定的分隔符包围,例如 YAML 使用 ---,TOML 使用 +++,JSON 使用 ---json。元数据可以包括标题、作者、日期、标签等描述文件内容的属性。

为什么使用 @std/front-matter? Jump to heading

使用此包可以轻松解析和提取内容文件中的 front matter,允许你将元数据与正文内容分离。这对于静态网站生成器、博客平台和内容管理系统尤为有用,这些场景中 front matter 通常用于管理文档元数据。

示例 Jump to heading

// 检测并提取 YAML front matter
import { extractYaml, test } from "@std/front-matter";

const source = `---yaml\ntitle: Hello\nauthor: Ada\n---\nContent starts here.`;

if (test(source)) {
  const { attrs, body, frontMatter } = extractYaml(source);
  // attrs: { title: "Hello", author: "Ada" }
  // body: "Content starts here."
  // frontMatter: "title: Hello\nauthor: Ada"
}
// JSON front matter
import { extractJson } from "@std/front-matter";

const jsonSource = `---json\n{ "tags": ["news", "deno"] }\n---\nPost body`;
const { attrs } = extractJson(jsonSource);
// attrs: { tags: ["news", "deno"] }
// TOML front matter
import { extractToml } from "@std/front-matter";

const tomlSource = `+++\ncategory = 'release'\n+++\nNotes...`;
const { attrs, body } = extractToml(tomlSource);
// attrs: { category: "release" }
// body: "Notes..."

小贴士 Jump to heading

  • 支持 JSON、TOML 和 YAML 分隔符——选择一种并保持一致性。
  • 返回的 attrs 已为你解析;body 是剩余的内容。

你找到了你需要的东西吗?

编辑此页面
隐私政策