Skip to content

Commit

Permalink
BREAKING(front-matter/unstable): move unstable overload of yaml `extr…
Browse files Browse the repository at this point in the history
…act` to `unstable-yaml` (#5968)
  • Loading branch information
kt3k authored Sep 12, 2024
1 parent c6b71a1 commit f621694
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 35 deletions.
1 change: 1 addition & 0 deletions _tools/check_docs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ const ENTRY_POINTS = [
"../fmt/duration.ts",
"../fmt/printf.ts",
"../front_matter/mod.ts",
"../front_matter/unstable_yaml.ts",
"../fs/mod.ts",
"../html/mod.ts",
"../html/unstable_is_valid_custom_element_name.ts",
Expand Down
1 change: 1 addition & 0 deletions front_matter/deno.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"./test": "./test.ts",
"./toml": "./toml.ts",
"./yaml": "./yaml.ts",
"./unstable-yaml": "./unstable_yaml.ts",
"./types": "./types.ts"
}
}
45 changes: 45 additions & 0 deletions front_matter/unstable_yaml.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.

import { extractAndParse, type Parser } from "./_shared.ts";
import { parse, type ParseOptions } from "@std/yaml/parse";
import type { Extract } from "./types.ts";
import { EXTRACT_YAML_REGEXP } from "./_formats.ts";

export type { Extract };

/**
* Extracts and parses {@link https://yaml.org | YAML} from the metadata of
* front matter content.
*
* @experimental **UNSTABLE**: New API, yet to be vetted.
*
* @example Extract YAML front matter
* ```ts
* import { extract } from "@std/front-matter/unstable-yaml";
* import { assertEquals } from "@std/assert";
*
* const output = `---yaml
* date: 2022-01-01
* ---
* Hello, world!`;
* const result = extract(output, { schema: "json" });
*
* assertEquals(result, {
* frontMatter: "date: 2022-01-01",
* body: "Hello, world!",
* attrs: { date: "2022-01-01" },
* });
* ```
*
* @typeParam T The type of the parsed front matter.
* @param text The text to extract YAML front matter from.
* @param options The options to pass to `@std/yaml/parse`.
* @returns The extracted YAML front matter and body content.
*/
export function extract<T>(text: string, options?: ParseOptions): Extract<T> {
return extractAndParse(
text,
EXTRACT_YAML_REGEXP,
((s) => parse(s, options)) as Parser,
);
}
38 changes: 5 additions & 33 deletions front_matter/yaml.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.

import { extractAndParse, type Parser } from "./_shared.ts";
import { parse, type ParseOptions } from "@std/yaml/parse";
import { parse } from "@std/yaml/parse";
import type { Extract } from "./types.ts";
import { EXTRACT_YAML_REGEXP } from "./_formats.ts";

Expand Down Expand Up @@ -29,45 +29,17 @@ export type { Extract };
* });
* ```
*
* @typeParam T The type of the parsed front matter.
* @param text The text to extract YAML front matter from.
* @returns The extracted YAML front matter and body content.
*/
export function extract<T>(text: string): Extract<T>;
/**
* Extracts and parses {@link https://yaml.org | YAML} from the metadata of
* front matter content.
*
* @experimental **UNSTABLE**: New API, yet to be vetted.
*
* @example Extract YAML front matter
* ```ts
* import { extract } from "@std/front-matter/yaml";
* import { assertEquals } from "@std/assert";
*
* const output = `---yaml
* date: 2022-01-01
* ---
* Hello, world!`;
* const result = extract(output, { schema: "json" });
*
* assertEquals(result, {
* frontMatter: "date: 2022-01-01",
* body: "Hello, world!",
* attrs: { date: "2022-01-01" },
* });
* ```
* Note: If you need to pass the options to the yaml parse,
* use the new version of this API from `@std/yaml/unstable-yaml`.
*
* @typeParam T The type of the parsed front matter.
* @param text The text to extract YAML front matter from.
* @param options The options to pass to `@std/yaml/parse`.
* @returns The extracted YAML front matter and body content.
*/
export function extract<T>(text: string, options?: ParseOptions): Extract<T>;
export function extract<T>(text: string, options?: ParseOptions): Extract<T> {
export function extract<T>(text: string): Extract<T> {
return extractAndParse(
text,
EXTRACT_YAML_REGEXP,
((s) => parse(s, options)) as Parser,
((s) => parse(s)) as Parser,
);
}
5 changes: 3 additions & 2 deletions front_matter/yaml_test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.

import { extract } from "./yaml.ts";
import { extract as unstableExtract } from "./unstable_yaml.ts";
import {
runExtractTypeErrorTests,
runExtractYamlTests1,
Expand All @@ -26,9 +27,9 @@ Deno.test("extractYaml() allows whitespaces after the header", () => {
assertEquals(extract("= yaml = \nfoo: 0\n---\n").attrs, { foo: 0 });
});

Deno.test("extractYaml() parses with schema options", () => {
Deno.test("(unstable)extractYaml() parses with schema options", () => {
assertEquals(
extract("---\ndate: 2024-08-20\n---\n", { schema: "json" }).attrs,
unstableExtract("---\ndate: 2024-08-20\n---\n", { schema: "json" }).attrs,
{
date: "2024-08-20",
},
Expand Down

0 comments on commit f621694

Please sign in to comment.