Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions front_matter/deno.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
"./test": "./test.ts",
"./toml": "./toml.ts",
"./yaml": "./yaml.ts",
"./unstable-json": "./unstable_json.ts",
"./unstable-toml": "./unstable_toml.ts",
"./unstable-yaml": "./unstable_yaml.ts",
"./types": "./types.ts"
}
Expand Down
19 changes: 19 additions & 0 deletions front_matter/json_test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
// Copyright 2018-2026 the Deno authors. MIT license.

import { assert } from "@std/assert";
import { assertThrows } from "../assert/throws.ts";
import { extract } from "./json.ts";

import { assertEquals } from "@std/assert/equals";
import { test as unstableTest } from "./unstable_json.ts";

Deno.test("json() extracts type error on invalid input", () => {
assertThrows(() => extract(""));
Expand Down Expand Up @@ -80,3 +82,20 @@ Deno.test("extractJson() throws at missing newline before body", () => {
"Unexpected end of input",
);
});

Deno.test("(unstable)test() handles valid json input", () => {
assert(unstableTest("---json\nname = 'deno'\n---\n"));
assert(unstableTest("= json =\nname = 'deno'\n= json =\n"));
assert(unstableTest("= json =\nname = 'deno'\n= json =\ndeno is awesome\n"));
});

Deno.test("(unstable)test() handles invalid json input", () => {
assert(!unstableTest(""));
assert(!unstableTest("---"));
assert(!unstableTest("---json"));
assert(!unstableTest("= json ="));
assert(!unstableTest("---\n"));
assert(!unstableTest("---json\n"));
assert(!unstableTest("= json =\n"));
assert(!unstableTest("---\nasdasdasd"));
});
19 changes: 19 additions & 0 deletions front_matter/toml_test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
// Copyright 2018-2026 the Deno authors. MIT license.

import { assert } from "@std/assert";
import { assertThrows } from "../assert/throws.ts";
import { extract } from "./toml.ts";
import { assertEquals } from "@std/assert/equals";
import { test as unstableTest } from "./unstable_toml.ts";

Deno.test("toml() extracts type error on invalid input", () => {
assertThrows(() => extract(""));
Expand Down Expand Up @@ -101,3 +103,20 @@ Deno.test("extractToml() throws at missing newline before body", () => {
"Unexpected end of input",
);
});

Deno.test("(unstable)test() handles valid toml input", () => {
assert(unstableTest("---toml\nname = 'deno'\n---\n"));
assert(unstableTest("= toml =\nname = 'deno'\n= toml =\n"));
assert(unstableTest("= toml =\nname = 'deno'\n= toml =\ndeno is awesome\n"));
});

Deno.test("(unstable)test() handles invalid toml input", () => {
assert(!unstableTest(""));
assert(!unstableTest("---"));
assert(!unstableTest("---toml"));
assert(!unstableTest("= toml ="));
assert(!unstableTest("---\n"));
assert(!unstableTest("---toml\n"));
assert(!unstableTest("= toml =\n"));
assert(!unstableTest("---\nasdasdasd"));
});
43 changes: 43 additions & 0 deletions front_matter/unstable_json.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Copyright 2018-2026 the Deno authors. MIT license.
// This module is browser compatible.

import { test as _test } from "./test.ts";

/**
* Tests if a string has valid front matter.
* Supports {@link https://www.json.org/ | JSON}.
*
* @experimental **UNSTABLE**: New API, yet to be vetted.
*
* @param str String to test.
* @returns `true` if the string has valid JSON front matter, otherwise `false`.
*
* @example Test for valid JSON front matter
* ```ts
* import { test } from "@std/front-matter/unstable-json";
* import { assert } from "@std/assert";
*
* const result = test(
* `---json
* {"title": "Three dashes followed by format marks the spot"}
* ---
* `);
* assert(result);
* ```
*
* @example TOML front matter is not valid as JSON
* ```ts
* import { test } from "@std/front-matter/unstable-json";
* import { assertFalse } from "@std/assert";
*
* const result = test(
* `---toml
* title = 'Three dashes followed by format marks the spot'
* ---
* `);
* assertFalse(result);
* ```
*/
export function test(str: string): boolean {
return _test(str, ["json"]);
}
43 changes: 43 additions & 0 deletions front_matter/unstable_toml.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Copyright 2018-2026 the Deno authors. MIT license.
// This module is browser compatible.

import { test as _test } from "./test.ts";

/**
* Tests if a string has valid TOML front matter.
* Supports {@link https://toml.io | TOML}.
*
* @experimental **UNSTABLE**: New API, yet to be vetted.
*
* @param str String to test.
* @returns `true` if the string has valid TOML front matter, otherwise `false`.
*
* @example Test for valid TOML front matter
* ```ts
* import { test } from "@std/front-matter/unstable-toml";
* import { assert } from "@std/assert";
*
* const result = test(
* `---toml
* title = 'Three dashes followed by format marks the spot'
* ---
* `);
* assert(result);
* ```
*
* @example JSON front matter is not valid as TOML
* ```ts
* import { test } from "@std/front-matter/unstable-toml";
* import { assertFalse } from "@std/assert";
*
* const result = test(
* `---json
* {"title": "Three dashes followed by format marks the spot"}
* ---
* `);
* assertFalse(result);
* ```
*/
export function test(str: string): boolean {
return _test(str, ["toml"]);
}
40 changes: 40 additions & 0 deletions front_matter/unstable_yaml.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { extractFrontMatter } from "./_shared.ts";
import { parse, type ParseOptions } from "@std/yaml/parse";
import type { Extract } from "./types.ts";
import { EXTRACT_YAML_REGEXP } from "./_formats.ts";
import { test as _test } from "./test.ts";

export type { Extract };

Expand Down Expand Up @@ -42,3 +43,42 @@ export function extract<T>(text: string, options?: ParseOptions): Extract<T> {
const attrs = parse(frontMatter, options) as T;
return { frontMatter, body, attrs };
}

/**
* Tests if a string has valid YAML front matter.
* Supports {@link https://yaml.org | YAML}.
*
* @experimental **UNSTABLE**: New API, yet to be vetted.
*
* @param str String to test.
* @returns `true` if the string has valid YAML front matter, otherwise `false`.
*
* @example Test for valid YAML front matter
* ```ts
* import { test } from "@std/front-matter/unstable-yaml";
* import { assert } from "@std/assert";
*
* const result = test(
* `---
* title: Three dashes marks the spot
* ---
* `);
* assert(result);
* ```
*
* @example JSON front matter is not valid as YAML
* ```ts
* import { test } from "@std/front-matter/unstable-yaml";
* import { assertFalse } from "@std/assert";
*
* const result = test(
* `---json
* {"title": "Three dashes followed by format marks the spot"}
* ---
* `);
* assertFalse(result);
* ```
*/
export function test(str: string): boolean {
return _test(str, ["yaml"]);
}
24 changes: 23 additions & 1 deletion front_matter/yaml_test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
// Copyright 2018-2026 the Deno authors. MIT license.

import { extract } from "./yaml.ts";
import { extract as unstableExtract } from "./unstable_yaml.ts";
import {
extract as unstableExtract,
test as unstableTest,
} from "./unstable_yaml.ts";
import { assert } from "../assert/mod.ts";
import { assertEquals } from "@std/assert/equals";
import { assertThrows } from "../assert/throws.ts";

Expand Down Expand Up @@ -126,3 +130,21 @@ Deno.test("extractYaml() throws at missing newline before body", () => {
"Unexpected end of input",
);
});

Deno.test("(unstable)test() handles valid yaml input", () => {
assert(unstableTest("---yaml\nname = 'deno'\n---\n"));
assert(unstableTest("= yaml =\nname = 'deno'\n= yaml =\n"));
assert(unstableTest("= yaml =\nname = 'deno'\n= yaml =\ndeno is awesome\n"));
assert(unstableTest("---\nname: deno\n---\n"));
});

Deno.test("(unstable)test() handles invalid yaml input", () => {
assert(!unstableTest(""));
assert(!unstableTest("---"));
assert(!unstableTest("---yaml"));
assert(!unstableTest("= yaml ="));
assert(!unstableTest("---\n"));
assert(!unstableTest("---yaml\n"));
assert(!unstableTest("= yaml =\n"));
assert(!unstableTest("---\nasdasdasd"));
});
Loading