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

What is front matter? Jump to heading

Front matter is metadata placed at the top of a file, typically used in Markdown or other text files to provide information about the document. It is usually enclosed within specific delimiters, such as --- for YAML, +++ for TOML, or ---json for JSON. The metadata can include details like the title, author, date, tags, and other attributes that describe the content of the file.

Why use @std/front-matter? Jump to heading

Use this package to easily parse and extract front matter from your content files, allowing you to separate metadata from the main content. This is especially useful in static site generators, blogging platforms, and content management systems where front matter is commonly used to manage document metadata.

Examples Jump to heading

// Detect and extract 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..."

Tips Jump to heading

  • Supports JSON, TOML, and YAML delimiters—pick one and stick with it for consistency.
  • Returned attrs are already parsed for you; body is the remaining content.

Did you find what you needed?

Privacy policy