test

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

function

function test(str: string, formats?: Format[]): boolean

Tests if a string has valid front matter. Supports YAML, TOML and JSON.

str: string

String to test.

formats?: Format[]

A list of formats to test for. Defaults to all supported formats.

Returns

true if the string has valid front matter, otherwise false.

Example: Test for valid YAML front matter

import { test } from "@ende/front-matter/test";
import { assert } from "@ende/assert";

const result = test(
`---
title: Three dashes marks the spot
---
`);
assert(result);

Example: Test for valid TOML front matter

import { test } from "@ende/front-matter/test";
import { assert } from "@ende/assert";

const result = test(
`---toml
title = 'Three dashes followed by format marks the spot'
---
`);
assert(result);

Example: Test for valid JSON front matter

import { test } from "@ende/front-matter/test";
import { assert } from "@ende/assert";

const result = test(
`---json
{"title": "Three dashes followed by format marks the spot"}
---
`);
assert(result);

Example: JSON front matter is not valid as YAML

import { test } from "@ende/front-matter/test";
import { assertFalse } from "@ende/assert";

const result = test(
`---json
{"title": "Three dashes followed by format marks the spot"}
---
`, ["yaml"]);
assertFalse(result);

Source