@soukadao/frontmatter-ts
v0.0.1
Published
A frontmatter parser for TypeScript. Parses YAML frontmatter delimited by `---` from text content.
Readme
@soukdao/frontmatter-ts
A frontmatter parser for TypeScript. Parses YAML frontmatter delimited by --- from text content.
Built on top of @soukadao/yaml-ts.
Installation
bun add @soukdao/frontmatter-tsUsage
import { parse } from "@soukdao/frontmatter-ts";
const source = `---
title: Hello World
tags:
- typescript
- yaml
draft: false
---
# Hello World
This is the body content.`;
const result = parse(source);
// result.data => { title: "Hello World", tags: ["typescript", "yaml"], draft: false }
// result.content => "# Hello World\n\nThis is the body content."parse returns null if the input has no frontmatter.
parse("No frontmatter here"); // => nullAPI
parse(source: string): FrontmatterResult | null
Parses a string containing YAML frontmatter.
- Returns
{ data, content }if frontmatter is found - Returns
nullif no frontmatter is detected
Types
interface FrontmatterResult {
data: { [key: string]: YamlValue };
content: string;
}Errors
FrontmatterError— base error classExtractError— thrown on extraction failures
License
MIT
