2024-01-01 21:11:32 +00:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2023-09-14 09:19:57 +00:00
|
|
|
|
2024-08-08 21:02:08 +00:00
|
|
|
import { EXTRACT_REGEXP_MAP, type Format } from "./_formats.ts";
|
2023-10-06 06:27:26 +00:00
|
|
|
|
2024-05-28 01:14:52 +00:00
|
|
|
export type { Format };
|
2023-09-14 09:19:57 +00:00
|
|
|
|
|
|
|
/**
|
2024-05-28 01:14:52 +00:00
|
|
|
* Tests if a string has valid front matter.
|
|
|
|
* Supports {@link https://yaml.org | YAML}, {@link https://toml.io | TOML} and
|
|
|
|
* {@link https://www.json.org/ | JSON}.
|
2023-09-14 09:19:57 +00:00
|
|
|
*
|
|
|
|
* @param str String to test.
|
|
|
|
* @param formats A list of formats to test for. Defaults to all supported formats.
|
2024-05-28 01:14:52 +00:00
|
|
|
* @returns `true` if the string has valid front matter, otherwise `false`.
|
2023-09-14 09:19:57 +00:00
|
|
|
*
|
2024-05-28 01:14:52 +00:00
|
|
|
* @example Test for valid YAML front matter
|
2023-09-14 09:19:57 +00:00
|
|
|
* ```ts
|
2024-04-29 02:57:30 +00:00
|
|
|
* import { test } from "@std/front-matter/test";
|
refactor(assert,async,bytes,cli,collections,crypto,csv,data-structures,datetime,dotenv,encoding,expect,fmt,front-matter,fs,html,http,ini,internal,io,json,jsonc,log,media-types,msgpack,net,path,semver,streams,testing,text,toml,ulid,url,uuid,webgpu,yaml): import from `@std/assert` (#5199)
* refactor: import from `@std/assert`
* update
2024-06-30 08:30:10 +00:00
|
|
|
* import { assert } from "@std/assert";
|
2023-09-14 09:19:57 +00:00
|
|
|
*
|
2024-05-28 01:14:52 +00:00
|
|
|
* const result = test(
|
|
|
|
* `---
|
|
|
|
* title: Three dashes marks the spot
|
|
|
|
* ---
|
|
|
|
* `);
|
|
|
|
* assert(result);
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* @example Test for valid TOML front matter
|
|
|
|
* ```ts
|
|
|
|
* import { test } from "@std/front-matter/test";
|
refactor(assert,async,bytes,cli,collections,crypto,csv,data-structures,datetime,dotenv,encoding,expect,fmt,front-matter,fs,html,http,ini,internal,io,json,jsonc,log,media-types,msgpack,net,path,semver,streams,testing,text,toml,ulid,url,uuid,webgpu,yaml): import from `@std/assert` (#5199)
* refactor: import from `@std/assert`
* update
2024-06-30 08:30:10 +00:00
|
|
|
* import { assert } from "@std/assert";
|
2024-05-28 01:14:52 +00:00
|
|
|
*
|
|
|
|
* const result = test(
|
|
|
|
* `---toml
|
|
|
|
* title = 'Three dashes followed by format marks the spot'
|
|
|
|
* ---
|
|
|
|
* `);
|
|
|
|
* assert(result);
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* @example Test for valid JSON front matter
|
|
|
|
* ```ts
|
|
|
|
* import { test } from "@std/front-matter/test";
|
refactor(assert,async,bytes,cli,collections,crypto,csv,data-structures,datetime,dotenv,encoding,expect,fmt,front-matter,fs,html,http,ini,internal,io,json,jsonc,log,media-types,msgpack,net,path,semver,streams,testing,text,toml,ulid,url,uuid,webgpu,yaml): import from `@std/assert` (#5199)
* refactor: import from `@std/assert`
* update
2024-06-30 08:30:10 +00:00
|
|
|
* import { assert } from "@std/assert";
|
2024-05-28 01:14:52 +00:00
|
|
|
*
|
|
|
|
* const result = test(
|
|
|
|
* `---json
|
|
|
|
* {"title": "Three dashes followed by format marks the spot"}
|
|
|
|
* ---
|
|
|
|
* `);
|
|
|
|
* assert(result);
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* @example JSON front matter is not valid as YAML
|
|
|
|
* ```ts
|
|
|
|
* import { test } from "@std/front-matter/test";
|
refactor(assert,async,bytes,cli,collections,crypto,csv,data-structures,datetime,dotenv,encoding,expect,fmt,front-matter,fs,html,http,ini,internal,io,json,jsonc,log,media-types,msgpack,net,path,semver,streams,testing,text,toml,ulid,url,uuid,webgpu,yaml): import from `@std/assert` (#5199)
* refactor: import from `@std/assert`
* update
2024-06-30 08:30:10 +00:00
|
|
|
* import { assertFalse } from "@std/assert";
|
2024-05-28 01:14:52 +00:00
|
|
|
*
|
|
|
|
* const result = test(
|
|
|
|
* `---json
|
|
|
|
* {"title": "Three dashes followed by format marks the spot"}
|
|
|
|
* ---
|
|
|
|
* `, ["yaml"]);
|
|
|
|
* assertFalse(result);
|
2023-09-14 09:19:57 +00:00
|
|
|
* ```
|
|
|
|
*/
|
2024-07-09 11:51:24 +00:00
|
|
|
export function test(str: string, formats?: Format[]): boolean {
|
2024-07-10 10:32:26 +00:00
|
|
|
if (!formats) formats = [...EXTRACT_REGEXP_MAP.keys()] as Format[];
|
2023-09-14 09:19:57 +00:00
|
|
|
|
|
|
|
for (const format of formats) {
|
2024-07-10 10:32:26 +00:00
|
|
|
const regexp = EXTRACT_REGEXP_MAP.get(format);
|
2024-07-09 11:51:24 +00:00
|
|
|
if (!regexp) {
|
|
|
|
throw new TypeError(`Unable to test for ${format} front matter format`);
|
2023-09-14 09:19:57 +00:00
|
|
|
}
|
2024-07-09 11:51:24 +00:00
|
|
|
const match = regexp.exec(str);
|
2023-09-14 09:19:57 +00:00
|
|
|
if (match?.index === 0) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|