-
Notifications
You must be signed in to change notification settings - Fork 620
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
BREAKING(front-matter/unstable): move unstable overload of yaml `extr…
…act` to `unstable-yaml` (#5968)
- Loading branch information
Showing
5 changed files
with
55 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters