Skip to main content

latest
Works with
It is unknown whether this package works with Node.js, Deno, Browsers, Cloudflare Workers, Bun
It is unknown whether this package works with Node.js
It is unknown whether this package works with Deno
It is unknown whether this package works with Browsers
It is unknown whether this package works with Cloudflare Workers
It is unknown whether this package works with Bun
JSR Score64%
Published2 years ago (0.215.0)
function createExtractor
createExtractor(formats: Partial<Record<"yaml" | "toml" | "json" | "unknown", Parser>>): Extractor

Factory that creates a function that extracts front matter from a string with the given parsers. Supports YAML, TOML and JSON.

Parameters

formats: Partial<Record<"yaml" | "toml" | "json" | "unknown", Parser>>

A descriptor containing Format-parser pairs to use for each format.

Return Type

A function that extracts front matter from a string with the given parsers.

import { createExtractor, Parser } from "@std/front-matter";
import { assertEquals } from "@std/assert/assert_equals";
import { parse as parseYAML } from "@std/yaml/parse";
import { parse as parseTOML } from "@std/toml/parse";
const extractYAML = createExtractor({ yaml: parseYAML as Parser });
const extractTOML = createExtractor({ toml: parseTOML as Parser });
const extractJSON = createExtractor({ json: JSON.parse as Parser });
const extractYAMLOrJSON = createExtractor({
yaml: parseYAML as Parser,
json: JSON.parse as Parser,
});

let { attrs, body, frontMatter } = extractYAML<{ title: string }>("---\ntitle: Three dashes marks the spot\n---\nferret");
assertEquals(attrs.title, "Three dashes marks the spot");
assertEquals(body, "ferret");
assertEquals(frontMatter, "title: Three dashes marks the spot");

({ attrs, body, frontMatter } = extractTOML<{ title: string }>("---toml\ntitle = 'Three dashes followed by format marks the spot'\n---\n"));
assertEquals(attrs.title, "Three dashes followed by format marks the spot");
assertEquals(body, "");
assertEquals(frontMatter, "title = 'Three dashes followed by format marks the spot'");

({ attrs, body, frontMatter } = extractJSON<{ title: string }>("---json\n{\"title\": \"Three dashes followed by format marks the spot\"}\n---\ngoat"));
assertEquals(attrs.title, "Three dashes followed by format marks the spot");
assertEquals(body, "goat");
assertEquals(frontMatter, "{\"title\": \"Three dashes followed by format marks the spot\"}");

({ attrs, body, frontMatter } = extractYAMLOrJSON<{ title: string }>("---\ntitle: Three dashes marks the spot\n---\nferret"));
assertEquals(attrs.title, "Three dashes marks the spot");
assertEquals(body, "ferret");
assertEquals(frontMatter, "title: Three dashes marks the spot");

({ attrs, body, frontMatter } = extractYAMLOrJSON<{ title: string }>("---json\n{\"title\": \"Three dashes followed by format marks the spot\"}\n---\ngoat"));
assertEquals(attrs.title, "Three dashes followed by format marks the spot");
assertEquals(body, "goat");
assertEquals(frontMatter, "{\"title\": \"Three dashes followed by format marks the spot\"}");

Report package

Please provide a reason for reporting this package. We will review your report and take appropriate action.

Please review the JSR usage policy before submitting a report.

Add Package

deno add jsr:@std/front-matter

Import symbol

import { createExtractor } from "@std/front-matter";
or

Import directly with a jsr specifier

import { createExtractor } from "jsr:@std/front-matter";