@@ -4,29 +4,10 @@ import { extract as extractToml } from "./toml.ts";
4
4
import { extract as extractYaml } from "./yaml.ts" ;
5
5
import { extract as extractJson } from "./json.ts" ;
6
6
import type { Extract } from "./types.ts" ;
7
- import type { Format } from "./test.ts" ;
8
- import { EXTRACT_REGEXP_MAP , RECOGNIZE_REGEXP_MAP } from "./_formats.ts" ;
7
+ import { RECOGNIZE_REGEXP_MAP } from "./_formats.ts" ;
9
8
10
9
export type { Extract } ;
11
10
12
- /**
13
- * Recognizes the format of the front matter in a string.
14
- * Supports {@link https://yaml.org | YAML}, {@link https://toml.io | TOML} and
15
- * {@link https://www.json.org/ | JSON}.
16
- *
17
- * @param str String to recognize.
18
- * @param formats A list of formats to recognize. Defaults to all supported formats.
19
- */
20
- function recognize (
21
- str : string ,
22
- formats : Format [ ] ,
23
- ) : Format {
24
- for ( const format of formats ) {
25
- if ( RECOGNIZE_REGEXP_MAP . get ( format ) ?. test ( str ) ) return format ;
26
- }
27
- throw new TypeError ( "Unsupported front matter format" ) ;
28
- }
29
-
30
11
/**
31
12
* Extracts and parses {@link https://yaml.org | YAML}, {@link https://toml.io |
32
13
* TOML}, or {@link https://www.json.org/ | JSON} from the metadata of front
@@ -54,14 +35,16 @@ function recognize(
54
35
* @returns The extracted front matter and body content.
55
36
*/
56
37
export function extract < T > ( text : string ) : Extract < T > {
57
- const formats = [ ...EXTRACT_REGEXP_MAP . keys ( ) ] as Format [ ] ;
58
- const format = recognize ( text , formats ) ;
38
+ const format = [ ...RECOGNIZE_REGEXP_MAP . entries ( ) ]
39
+ . find ( ( [ _ , regexp ] ) => regexp . test ( text ) ) ?. [ 0 ] ;
59
40
switch ( format ) {
60
41
case "yaml" :
61
42
return extractYaml < T > ( text ) ;
62
43
case "toml" :
63
44
return extractToml < T > ( text ) ;
64
45
case "json" :
65
46
return extractJson < T > ( text ) ;
47
+ default :
48
+ throw new TypeError ( "Unsupported front matter format" ) ;
66
49
}
67
50
}
0 commit comments