|
1 | 1 | // Copyright 2018-2025 the Deno authors. MIT license.
|
2 | 2 |
|
3 |
| -import { extract } from "./any.ts"; |
4 |
| - |
| 3 | +import { extract } from "@std/front-matter/any"; |
5 | 4 | import { assertEquals } from "../assert/equals.ts";
|
6 | 5 | import { assertThrows } from "../assert/throws.ts";
|
7 | 6 |
|
8 |
| -Deno.test("extract() extracts type error on invalid input", () => { |
9 |
| - assertThrows(() => extract("")); |
10 |
| - assertThrows(() => extract("---")); |
11 |
| - assertThrows(() => extract(`---yaml`)); |
12 |
| - assertThrows(() => extract(`= yaml =`)); |
13 |
| - assertThrows(() => extract("---\n")); |
14 |
| - assertThrows(() => extract(`---yaml\n`)); |
15 |
| - assertThrows(() => extract(`= yaml =\n`)); |
16 |
| - assertThrows(() => extract("---\nasdasdasd")); |
17 |
| -}); |
18 |
| - |
19 |
| -Deno.test("extract() parses yaml delineate by `---`", () => { |
| 7 | +Deno.test("extract() handles yaml data", () => { |
20 | 8 | const input = `---
|
21 |
| -title: Three dashes marks the spot |
22 |
| -tags: |
23 |
| - - yaml |
24 |
| - - front-matter |
25 |
| - - dashes |
26 |
| -expanded-description: with some --- crazy stuff in it |
27 |
| ---- |
28 |
| -don't break |
| 9 | + foo: bar |
29 | 10 | ---
|
30 |
| -Also this shouldn't be a problem |
31 |
| -`; |
32 |
| - const content = extract<Record<string, unknown>>(input); |
33 |
| - assertEquals( |
34 |
| - content.frontMatter, |
35 |
| - `title: Three dashes marks the spot |
36 |
| -tags: |
37 |
| - - yaml |
38 |
| - - front-matter |
39 |
| - - dashes |
40 |
| -expanded-description: with some --- crazy stuff in it`, |
41 |
| - ); |
42 |
| - assertEquals( |
43 |
| - content.body, |
44 |
| - "don't break\n---\nAlso this shouldn't be a problem\n", |
45 |
| - ); |
46 |
| - assertEquals(content.attrs.title, "Three dashes marks the spot"); |
47 |
| - assertEquals(content.attrs.tags, ["yaml", "front-matter", "dashes"]); |
48 |
| - assertEquals( |
49 |
| - content.attrs["expanded-description"], |
50 |
| - "with some --- crazy stuff in it", |
51 |
| - ); |
52 |
| -}); |
53 |
| - |
54 |
| -Deno.test("extract() parses yaml delineate by `---yaml`", () => { |
55 |
| - const input = `---yaml |
56 |
| -title: Three dashes marks the spot |
57 |
| -tags: |
58 |
| - - yaml |
59 |
| - - front-matter |
60 |
| - - dashes |
61 |
| -expanded-description: with some --- crazy stuff in it |
62 |
| ---- |
63 |
| -don't break |
64 |
| ---- |
65 |
| -Also this shouldn't be a problem |
66 |
| -`; |
67 |
| - const content = extract<Record<string, unknown>>(input); |
68 |
| - assertEquals( |
69 |
| - content.frontMatter, |
70 |
| - `title: Three dashes marks the spot |
71 |
| -tags: |
72 |
| - - yaml |
73 |
| - - front-matter |
74 |
| - - dashes |
75 |
| -expanded-description: with some --- crazy stuff in it`, |
76 |
| - ); |
77 |
| - assertEquals( |
78 |
| - content.body, |
79 |
| - "don't break\n---\nAlso this shouldn't be a problem\n", |
80 |
| - ); |
81 |
| - assertEquals(content.attrs.title, "Three dashes marks the spot"); |
82 |
| - assertEquals(content.attrs.tags, ["yaml", "front-matter", "dashes"]); |
83 |
| - assertEquals( |
84 |
| - content.attrs["expanded-description"], |
85 |
| - "with some --- crazy stuff in it", |
86 |
| - ); |
| 11 | +Hello, world!`; |
| 12 | + const actual = extract(input); |
| 13 | + const expected = { |
| 14 | + attrs: { |
| 15 | + foo: "bar", |
| 16 | + }, |
| 17 | + body: "Hello, world!", |
| 18 | + frontMatter: "foo: bar", |
| 19 | + }; |
| 20 | + assertEquals(actual, expected); |
87 | 21 | });
|
88 | 22 |
|
89 |
| -Deno.test("extract() extracts type error on invalid json input", () => { |
90 |
| - assertThrows(() => extract("")); |
91 |
| - assertThrows(() => extract("---")); |
92 |
| - assertThrows(() => extract(`---json`)); |
93 |
| - assertThrows(() => extract(`= json =`)); |
94 |
| - assertThrows(() => extract("---\n")); |
95 |
| - assertThrows(() => extract(`---json\n`)); |
96 |
| - assertThrows(() => extract(`= json =\n`)); |
97 |
| - assertThrows(() => extract("---\nasdasdasd")); |
| 23 | +Deno.test("extract() handles toml data", () => { |
| 24 | + const input = `+++ |
| 25 | + foo = "bar" |
| 26 | ++++ |
| 27 | +Hello, world!`; |
| 28 | + const actual = extract(input); |
| 29 | + const expected = { |
| 30 | + attrs: { |
| 31 | + foo: "bar", |
| 32 | + }, |
| 33 | + body: "Hello, world!", |
| 34 | + frontMatter: 'foo = "bar"', |
| 35 | + }; |
| 36 | + assertEquals(actual, expected); |
98 | 37 | });
|
99 | 38 |
|
100 |
| -Deno.test("json() parses json delineate by ---json", () => { |
| 39 | +Deno.test("extract() handles json data", () => { |
101 | 40 | const input = `---json
|
102 | 41 | {
|
103 |
| - "title": "Three dashes followed by the format marks the spot", |
104 |
| - "tags": [ |
105 |
| - "json", |
106 |
| - "front-matter" |
107 |
| - ], |
108 |
| - "expanded-description": "with some ---json 🙃 crazy stuff in it" |
| 42 | + "foo": "bar" |
109 | 43 | }
|
110 | 44 | ---
|
111 |
| -don't break |
112 |
| ---- |
113 |
| -{Also: "---json this shouldn't be a problem"} |
114 |
| -`; |
| 45 | +Hello, world!`; |
115 | 46 |
|
116 |
| - const content = extract<Record<string, unknown>>(input); |
117 |
| - assertEquals( |
118 |
| - content.frontMatter, |
119 |
| - `{ |
120 |
| - "title": "Three dashes followed by the format marks the spot", |
121 |
| - "tags": [ |
122 |
| - "json", |
123 |
| - "front-matter" |
124 |
| - ], |
125 |
| - "expanded-description": "with some ---json 🙃 crazy stuff in it" |
126 |
| -}`, |
127 |
| - ); |
128 |
| - assertEquals( |
129 |
| - content.body, |
130 |
| - "don't break\n---\n{Also: \"---json this shouldn't be a problem\"}\n", |
131 |
| - ); |
132 |
| - assertEquals( |
133 |
| - content.attrs.title, |
134 |
| - "Three dashes followed by the format marks the spot", |
135 |
| - ); |
136 |
| - assertEquals(content.attrs.tags, ["json", "front-matter"]); |
137 |
| - assertEquals( |
138 |
| - content.attrs["expanded-description"], |
139 |
| - "with some ---json 🙃 crazy stuff in it", |
140 |
| - ); |
| 47 | + const actual = extract(input); |
| 48 | + const expected = { |
| 49 | + attrs: { foo: "bar" }, |
| 50 | + body: "Hello, world!", |
| 51 | + frontMatter: '{\n "foo": "bar"\n}', |
| 52 | + }; |
| 53 | + assertEquals(actual, expected); |
141 | 54 | });
|
142 | 55 |
|
143 |
| -Deno.test("extract() extracts type error on invalid toml input", () => { |
144 |
| - assertThrows(() => extract("")); |
145 |
| - assertThrows(() => extract("---")); |
146 |
| - assertThrows(() => extract(`---toml`)); |
147 |
| - assertThrows(() => extract(`= toml =`)); |
148 |
| - assertThrows(() => extract("---\n")); |
149 |
| - assertThrows(() => extract(`---toml\n`)); |
150 |
| - assertThrows(() => extract(`= toml =\n`)); |
151 |
| - assertThrows(() => extract("---\nasdasdasd")); |
152 |
| -}); |
153 |
| - |
154 |
| -Deno.test("extract() parses toml delineate by ---toml", () => { |
155 |
| - const input = `---toml |
156 |
| -title = 'Three dashes followed by the format marks the spot' |
157 |
| -tags = ['toml', 'front-matter'] |
158 |
| -'expanded-description' = 'with some ---toml 👌 crazy stuff in it' |
159 |
| ---- |
160 |
| -don't break |
| 56 | +Deno.test("extract() throws on unsupported format", () => { |
| 57 | + const input = `---unsupported |
| 58 | + foo: bar |
161 | 59 | ---
|
162 |
| -Also = '---toml this shouldn't be a problem' |
163 |
| -`; |
164 |
| - const content = extract<Record<string, unknown>>(input); |
165 |
| - assertEquals( |
166 |
| - content.frontMatter, |
167 |
| - `title = 'Three dashes followed by the format marks the spot' |
168 |
| -tags = ['toml', 'front-matter'] |
169 |
| -'expanded-description' = 'with some ---toml 👌 crazy stuff in it'`, |
170 |
| - ); |
171 |
| - assertEquals( |
172 |
| - content.body, |
173 |
| - "don't break\n---\nAlso = '---toml this shouldn't be a problem'\n", |
174 |
| - ); |
175 |
| - assertEquals( |
176 |
| - content.attrs.title, |
177 |
| - "Three dashes followed by the format marks the spot", |
178 |
| - ); |
179 |
| - assertEquals(content.attrs.tags, ["toml", "front-matter"]); |
180 |
| - assertEquals( |
181 |
| - content.attrs["expanded-description"], |
182 |
| - "with some ---toml 👌 crazy stuff in it", |
| 60 | +Hello, world!`; |
| 61 | + |
| 62 | + assertThrows( |
| 63 | + () => extract(input), |
| 64 | + TypeError, |
| 65 | + "Unsupported front matter format", |
183 | 66 | );
|
184 | 67 | });
|
0 commit comments