Extract front matter from markdown

Front matter is a metadata block at the top of a markdown file, fenced by --- lines and usually written in YAML. Static site generators and blog engines use it for titles, dates, and tags. The @ende/front-matter module parses it and hands back the document body separately.

Edit on GitHub

import { extractYaml, test } from "jsr:@ende/front-matter";

A typical markdown document with YAML front matter.

const markdown = `---
title: My first post
date: 2026-06-12
tags: [ende, markdown]
draft: false
---

# Hello

This is the body of the post.
`;

test() checks whether a document begins with a front matter block at all, so you can handle plain markdown files gracefully.

console.log(test(markdown)); // true

extractYaml returns the parsed attributes, the body with the block removed, and the raw front matter text. Type the attributes with a generic parameter to get typed access.

type PostMeta = {
  date: Date;
  draft: boolean;
  tags: string[];
  title: string;
};

const { attrs, body } = extractYaml<PostMeta>(markdown);

console.log(attrs.title); // My first post
console.log(attrs.tags); // [ "ende", "markdown" ]
console.log(body.trim().split("\n")[0]); // # Hello

JSON and TOML front matter have matching extractors, fenced the same way but with the format name on the fence.

import { extractToml } from "jsr:@ende/front-matter";

const tomlDoc = `---toml
title = "TOML works too"
---
Body text.
`;
console.log(extractToml<{ title: string }>(tomlDoc).attrs.title); // TOML works too

Run this example locally using the Ende CLI:

ende run https://docs.ende.sh/examples/scripts/front_matter.ts

Additional resources