diff --git a/front_matter/deno.json b/front_matter/deno.json index ff70d0f7c4c1..443fda982885 100644 --- a/front_matter/deno.json +++ b/front_matter/deno.json @@ -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" } diff --git a/front_matter/json_test.ts b/front_matter/json_test.ts index 70cf511cbc54..76265c1d75a9 100644 --- a/front_matter/json_test.ts +++ b/front_matter/json_test.ts @@ -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("")); @@ -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")); +}); diff --git a/front_matter/toml_test.ts b/front_matter/toml_test.ts index 9e4770ca15b8..bb2bafb96530 100644 --- a/front_matter/toml_test.ts +++ b/front_matter/toml_test.ts @@ -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("")); @@ -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")); +}); diff --git a/front_matter/unstable_json.ts b/front_matter/unstable_json.ts new file mode 100644 index 000000000000..45c7a2bf42f4 --- /dev/null +++ b/front_matter/unstable_json.ts @@ -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"]); +} diff --git a/front_matter/unstable_toml.ts b/front_matter/unstable_toml.ts new file mode 100644 index 000000000000..69299ba5aa0f --- /dev/null +++ b/front_matter/unstable_toml.ts @@ -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"]); +} diff --git a/front_matter/unstable_yaml.ts b/front_matter/unstable_yaml.ts index 10532b9dbf78..71dfb697993e 100644 --- a/front_matter/unstable_yaml.ts +++ b/front_matter/unstable_yaml.ts @@ -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 }; @@ -42,3 +43,42 @@ export function extract(text: string, options?: ParseOptions): Extract { 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"]); +} diff --git a/front_matter/yaml_test.ts b/front_matter/yaml_test.ts index d8d584d295b6..1a9ecfb0973d 100644 --- a/front_matter/yaml_test.ts +++ b/front_matter/yaml_test.ts @@ -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"; @@ -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")); +});