|
1 |
+ |
import { describe, expect, it } from "bun:test"; |
|
2 |
+ |
import { parseFrontmatter } from "../src/lib/markdown"; |
|
3 |
+ |
|
|
4 |
+ |
describe("parseFrontmatter", () => { |
|
5 |
+ |
describe("delimiters", () => { |
|
6 |
+ |
it("parses YAML frontmatter (--- delimiter)", () => { |
|
7 |
+ |
const content = `--- |
|
8 |
+ |
title: Hello World |
|
9 |
+ |
--- |
|
10 |
+ |
Body content here.`; |
|
11 |
+ |
const { frontmatter, body } = parseFrontmatter(content); |
|
12 |
+ |
expect(frontmatter.title).toBe("Hello World"); |
|
13 |
+ |
expect(body).toBe("Body content here."); |
|
14 |
+ |
}); |
|
15 |
+ |
|
|
16 |
+ |
it("parses TOML frontmatter (+++ delimiter)", () => { |
|
17 |
+ |
const content = `+++ |
|
18 |
+ |
title = "Hugo Post" |
|
19 |
+ |
+++ |
|
20 |
+ |
Body content here.`; |
|
21 |
+ |
const { frontmatter, body } = parseFrontmatter(content); |
|
22 |
+ |
expect(frontmatter.title).toBe("Hugo Post"); |
|
23 |
+ |
expect(body).toBe("Body content here."); |
|
24 |
+ |
}); |
|
25 |
+ |
|
|
26 |
+ |
it("parses alternative frontmatter (*** delimiter)", () => { |
|
27 |
+ |
const content = `*** |
|
28 |
+ |
title: Alt Post |
|
29 |
+ |
*** |
|
30 |
+ |
Body content here.`; |
|
31 |
+ |
const { frontmatter, body } = parseFrontmatter(content); |
|
32 |
+ |
expect(frontmatter.title).toBe("Alt Post"); |
|
33 |
+ |
expect(body).toBe("Body content here."); |
|
34 |
+ |
}); |
|
35 |
+ |
|
|
36 |
+ |
it("throws when no frontmatter is present", () => { |
|
37 |
+ |
const content = "Just plain content with no frontmatter."; |
|
38 |
+ |
expect(() => parseFrontmatter(content)).toThrow( |
|
39 |
+ |
"Could not parse frontmatter", |
|
40 |
+ |
); |
|
41 |
+ |
}); |
|
42 |
+ |
}); |
|
43 |
+ |
|
|
44 |
+ |
describe("scalar values", () => { |
|
45 |
+ |
it("parses a string value", () => { |
|
46 |
+ |
const content = `--- |
|
47 |
+ |
title: My Post |
|
48 |
+ |
description: A short description |
|
49 |
+ |
--- |
|
50 |
+ |
`; |
|
51 |
+ |
const { frontmatter } = parseFrontmatter(content); |
|
52 |
+ |
expect(frontmatter.title).toBe("My Post"); |
|
53 |
+ |
expect(frontmatter.description).toBe("A short description"); |
|
54 |
+ |
}); |
|
55 |
+ |
|
|
56 |
+ |
it("strips double quotes from values", () => { |
|
57 |
+ |
const content = `--- |
|
58 |
+ |
title: "Quoted Title" |
|
59 |
+ |
--- |
|
60 |
+ |
`; |
|
61 |
+ |
const { frontmatter } = parseFrontmatter(content); |
|
62 |
+ |
expect(frontmatter.title).toBe("Quoted Title"); |
|
63 |
+ |
}); |
|
64 |
+ |
|
|
65 |
+ |
it("strips single quotes from values", () => { |
|
66 |
+ |
const content = `--- |
|
67 |
+ |
title: 'Single Quoted' |
|
68 |
+ |
--- |
|
69 |
+ |
`; |
|
70 |
+ |
const { frontmatter } = parseFrontmatter(content); |
|
71 |
+ |
expect(frontmatter.title).toBe("Single Quoted"); |
|
72 |
+ |
}); |
|
73 |
+ |
|
|
74 |
+ |
it("parses YAML folded multiline string", () => { |
|
75 |
+ |
const content = `--- |
|
76 |
+ |
excerpt: > |
|
77 |
+ |
This is a folded |
|
78 |
+ |
multiline string |
|
79 |
+ |
--- |
|
80 |
+ |
`; |
|
81 |
+ |
const { rawFrontmatter } = parseFrontmatter(content); |
|
82 |
+ |
expect(rawFrontmatter.excerpt).toBe("This is a folded multiline string\n"); |
|
83 |
+ |
}); |
|
84 |
+ |
|
|
85 |
+ |
it("parses YAML stripped folded multiline string", () => { |
|
86 |
+ |
const content = `--- |
|
87 |
+ |
excerpt: >- |
|
88 |
+ |
This is a stripped folded |
|
89 |
+ |
multiline string |
|
90 |
+ |
--- |
|
91 |
+ |
`; |
|
92 |
+ |
const { rawFrontmatter } = parseFrontmatter(content); |
|
93 |
+ |
expect(rawFrontmatter.excerpt).toBe("This is a stripped folded multiline string"); |
|
94 |
+ |
}); |
|
95 |
+ |
|
|
96 |
+ |
it("parses YAML literal multiline string", () => { |
|
97 |
+ |
const content = `--- |
|
98 |
+ |
excerpt: | |
|
99 |
+ |
This is a literal |
|
100 |
+ |
multiline string |
|
101 |
+ |
--- |
|
102 |
+ |
`; |
|
103 |
+ |
const { rawFrontmatter } = parseFrontmatter(content); |
|
104 |
+ |
expect(rawFrontmatter.excerpt).toBe("This is a literal\nmultiline string\n"); |
|
105 |
+ |
}); |
|
106 |
+ |
|
|
107 |
+ |
it("parses YAML kept literal multiline string", () => { |
|
108 |
+ |
const content = `--- |
|
109 |
+ |
excerpt: |+ |
|
110 |
+ |
This is a kept literal |
|
111 |
+ |
multiline string |
|
112 |
+ |
|
|
113 |
+ |
end: true |
|
114 |
+ |
--- |
|
115 |
+ |
`; |
|
116 |
+ |
const { rawFrontmatter } = parseFrontmatter(content); |
|
117 |
+ |
expect(rawFrontmatter.excerpt).toBe("This is a kept literal\nmultiline string\n\n"); |
|
118 |
+ |
}); |
|
119 |
+ |
|
|
120 |
+ |
it("parses boolean true", () => { |
|
121 |
+ |
const content = `--- |
|
122 |
+ |
draft: true |
|
123 |
+ |
--- |
|
124 |
+ |
`; |
|
125 |
+ |
const { frontmatter } = parseFrontmatter(content); |
|
126 |
+ |
expect(frontmatter.draft).toBe(true); |
|
127 |
+ |
}); |
|
128 |
+ |
|
|
129 |
+ |
it("parses boolean false", () => { |
|
130 |
+ |
const content = `--- |
|
131 |
+ |
draft: false |
|
132 |
+ |
--- |
|
133 |
+ |
`; |
|
134 |
+ |
const { frontmatter } = parseFrontmatter(content); |
|
135 |
+ |
expect(frontmatter.draft).toBe(false); |
|
136 |
+ |
}); |
|
137 |
+ |
|
|
138 |
+ |
it('parses string "true" in draft field as boolean true', () => { |
|
139 |
+ |
const content = `--- |
|
140 |
+ |
draft: true |
|
141 |
+ |
--- |
|
142 |
+ |
`; |
|
143 |
+ |
const { rawFrontmatter } = parseFrontmatter(content); |
|
144 |
+ |
expect(rawFrontmatter.draft).toBe(true); |
|
145 |
+ |
}); |
|
146 |
+ |
}); |
|
147 |
+ |
|
|
148 |
+ |
describe("arrays", () => { |
|
149 |
+ |
it("parses inline YAML arrays", () => { |
|
150 |
+ |
const content = `--- |
|
151 |
+ |
tags: [typescript, bun, testing] |
|
152 |
+ |
--- |
|
153 |
+ |
`; |
|
154 |
+ |
const { frontmatter } = parseFrontmatter(content); |
|
155 |
+ |
expect(frontmatter.tags).toEqual(["typescript", "bun", "testing"]); |
|
156 |
+ |
}); |
|
157 |
+ |
|
|
158 |
+ |
it("parses inline YAML arrays with quoted items", () => { |
|
159 |
+ |
const content = `--- |
|
160 |
+ |
tags: ["typescript", "bun", "testing"] |
|
161 |
+ |
--- |
|
162 |
+ |
`; |
|
163 |
+ |
const { frontmatter } = parseFrontmatter(content); |
|
164 |
+ |
expect(frontmatter.tags).toEqual(["typescript", "bun", "testing"]); |
|
165 |
+ |
}); |
|
166 |
+ |
|
|
167 |
+ |
it("parses YAML block arrays", () => { |
|
168 |
+ |
const content = `--- |
|
169 |
+ |
tags: |
|
170 |
+ |
- typescript |
|
171 |
+ |
- bun |
|
172 |
+ |
- testing |
|
173 |
+ |
--- |
|
174 |
+ |
`; |
|
175 |
+ |
const { frontmatter } = parseFrontmatter(content); |
|
176 |
+ |
expect(frontmatter.tags).toEqual(["typescript", "bun", "testing"]); |
|
177 |
+ |
}); |
|
178 |
+ |
|
|
179 |
+ |
it("parses YAML block arrays with quoted items", () => { |
|
180 |
+ |
const content = `--- |
|
181 |
+ |
tags: |
|
182 |
+ |
- "typescript" |
|
183 |
+ |
- 'bun' |
|
184 |
+ |
--- |
|
185 |
+ |
`; |
|
186 |
+ |
const { frontmatter } = parseFrontmatter(content); |
|
187 |
+ |
expect(frontmatter.tags).toEqual(["typescript", "bun"]); |
|
188 |
+ |
}); |
|
189 |
+ |
|
|
190 |
+ |
it("parses inline TOML arrays", () => { |
|
191 |
+ |
const content = `+++ |
|
192 |
+ |
tags = ["typescript", "bun"] |
|
193 |
+ |
+++ |
|
194 |
+ |
`; |
|
195 |
+ |
const { frontmatter } = parseFrontmatter(content); |
|
196 |
+ |
expect(frontmatter.tags).toEqual(["typescript", "bun"]); |
|
197 |
+ |
}); |
|
198 |
+ |
}); |
|
199 |
+ |
|
|
200 |
+ |
describe("publish date fallbacks", () => { |
|
201 |
+ |
it("uses publishDate field directly", () => { |
|
202 |
+ |
const content = `--- |
|
203 |
+ |
publishDate: 2024-01-15 |
|
204 |
+ |
--- |
|
205 |
+ |
`; |
|
206 |
+ |
const { frontmatter } = parseFrontmatter(content); |
|
207 |
+ |
expect(frontmatter.publishDate).toBe("2024-01-15"); |
|
208 |
+ |
}); |
|
209 |
+ |
|
|
210 |
+ |
it("falls back to pubDate", () => { |
|
211 |
+ |
const content = `--- |
|
212 |
+ |
pubDate: 2024-02-01 |
|
213 |
+ |
--- |
|
214 |
+ |
`; |
|
215 |
+ |
const { frontmatter } = parseFrontmatter(content); |
|
216 |
+ |
expect(frontmatter.publishDate).toBe("2024-02-01"); |
|
217 |
+ |
}); |
|
218 |
+ |
|
|
219 |
+ |
it("falls back to date", () => { |
|
220 |
+ |
const content = `--- |
|
221 |
+ |
date: 2024-03-10 |
|
222 |
+ |
--- |
|
223 |
+ |
`; |
|
224 |
+ |
const { frontmatter } = parseFrontmatter(content); |
|
225 |
+ |
expect(frontmatter.publishDate).toBe("2024-03-10"); |
|
226 |
+ |
}); |
|
227 |
+ |
|
|
228 |
+ |
it("falls back to createdAt", () => { |
|
229 |
+ |
const content = `--- |
|
230 |
+ |
createdAt: 2024-04-20 |
|
231 |
+ |
--- |
|
232 |
+ |
`; |
|
233 |
+ |
const { frontmatter } = parseFrontmatter(content); |
|
234 |
+ |
expect(frontmatter.publishDate).toBe("2024-04-20"); |
|
235 |
+ |
}); |
|
236 |
+ |
|
|
237 |
+ |
it("falls back to created_at", () => { |
|
238 |
+ |
const content = `--- |
|
239 |
+ |
created_at: 2024-05-30 |
|
240 |
+ |
--- |
|
241 |
+ |
`; |
|
242 |
+ |
const { frontmatter } = parseFrontmatter(content); |
|
243 |
+ |
expect(frontmatter.publishDate).toBe("2024-05-30"); |
|
244 |
+ |
}); |
|
245 |
+ |
|
|
246 |
+ |
it("prefers publishDate over other fallbacks", () => { |
|
247 |
+ |
const content = `--- |
|
248 |
+ |
publishDate: 2024-01-01 |
|
249 |
+ |
date: 2023-01-01 |
|
250 |
+ |
--- |
|
251 |
+ |
`; |
|
252 |
+ |
const { frontmatter } = parseFrontmatter(content); |
|
253 |
+ |
expect(frontmatter.publishDate).toBe("2024-01-01"); |
|
254 |
+ |
}); |
|
255 |
+ |
}); |
|
256 |
+ |
|
|
257 |
+ |
describe("rawFrontmatter", () => { |
|
258 |
+ |
it("returns all raw fields", () => { |
|
259 |
+ |
const content = `--- |
|
260 |
+ |
title: Raw Test |
|
261 |
+ |
custom: value |
|
262 |
+ |
--- |
|
263 |
+ |
`; |
|
264 |
+ |
const { rawFrontmatter } = parseFrontmatter(content); |
|
265 |
+ |
expect(rawFrontmatter.title).toBe("Raw Test"); |
|
266 |
+ |
expect(rawFrontmatter.custom).toBe("value"); |
|
267 |
+ |
}); |
|
268 |
+ |
|
|
269 |
+ |
it("preserves atUri in both frontmatter and rawFrontmatter", () => { |
|
270 |
+ |
const content = `--- |
|
271 |
+ |
title: Post |
|
272 |
+ |
atUri: at://did:plc:abc123/app.bsky.feed.post/xyz |
|
273 |
+ |
--- |
|
274 |
+ |
`; |
|
275 |
+ |
const { frontmatter, rawFrontmatter } = parseFrontmatter(content); |
|
276 |
+ |
expect(frontmatter.atUri).toBe( |
|
277 |
+ |
"at://did:plc:abc123/app.bsky.feed.post/xyz", |
|
278 |
+ |
); |
|
279 |
+ |
expect(rawFrontmatter.atUri).toBe( |
|
280 |
+ |
"at://did:plc:abc123/app.bsky.feed.post/xyz", |
|
281 |
+ |
); |
|
282 |
+ |
}); |
|
283 |
+ |
}); |
|
284 |
+ |
|
|
285 |
+ |
describe("FrontmatterMapping", () => { |
|
286 |
+ |
it("maps a custom title field", () => { |
|
287 |
+ |
const content = `--- |
|
288 |
+ |
name: My Mapped Title |
|
289 |
+ |
--- |
|
290 |
+ |
`; |
|
291 |
+ |
const { frontmatter } = parseFrontmatter(content, { title: "name" }); |
|
292 |
+ |
expect(frontmatter.title).toBe("My Mapped Title"); |
|
293 |
+ |
}); |
|
294 |
+ |
|
|
295 |
+ |
it("maps a custom description field", () => { |
|
296 |
+ |
const content = `--- |
|
297 |
+ |
summary: Custom description |
|
298 |
+ |
--- |
|
299 |
+ |
`; |
|
300 |
+ |
const { frontmatter } = parseFrontmatter(content, { |
|
301 |
+ |
description: "summary", |
|
302 |
+ |
}); |
|
303 |
+ |
expect(frontmatter.description).toBe("Custom description"); |
|
304 |
+ |
}); |
|
305 |
+ |
|
|
306 |
+ |
it("maps a custom publishDate field", () => { |
|
307 |
+ |
const content = `--- |
|
308 |
+ |
publishedOn: 2024-06-15 |
|
309 |
+ |
--- |
|
310 |
+ |
`; |
|
311 |
+ |
const { frontmatter } = parseFrontmatter(content, { |
|
312 |
+ |
publishDate: "publishedOn", |
|
313 |
+ |
}); |
|
314 |
+ |
expect(frontmatter.publishDate).toBe("2024-06-15"); |
|
315 |
+ |
}); |
|
316 |
+ |
|
|
317 |
+ |
it("maps a custom coverImage field", () => { |
|
318 |
+ |
const content = `--- |
|
319 |
+ |
heroImage: /images/cover.jpg |
|
320 |
+ |
--- |
|
321 |
+ |
`; |
|
322 |
+ |
const { frontmatter } = parseFrontmatter(content, { |
|
323 |
+ |
coverImage: "heroImage", |
|
324 |
+ |
}); |
|
325 |
+ |
expect(frontmatter.ogImage).toBe("/images/cover.jpg"); |
|
326 |
+ |
}); |
|
327 |
+ |
|
|
328 |
+ |
it("maps a custom tags field", () => { |
|
329 |
+ |
const content = `--- |
|
330 |
+ |
categories: [news, updates] |
|
331 |
+ |
--- |
|
332 |
+ |
`; |
|
333 |
+ |
const { frontmatter } = parseFrontmatter(content, { tags: "categories" }); |
|
334 |
+ |
expect(frontmatter.tags).toEqual(["news", "updates"]); |
|
335 |
+ |
}); |
|
336 |
+ |
|
|
337 |
+ |
it("maps a custom draft field", () => { |
|
338 |
+ |
const content = `--- |
|
339 |
+ |
unpublished: true |
|
340 |
+ |
--- |
|
341 |
+ |
`; |
|
342 |
+ |
const { frontmatter } = parseFrontmatter(content, { draft: "unpublished" }); |
|
343 |
+ |
expect(frontmatter.draft).toBe(true); |
|
344 |
+ |
}); |
|
345 |
+ |
|
|
346 |
+ |
it("falls back to standard field name when mapped field is absent", () => { |
|
347 |
+ |
const content = `--- |
|
348 |
+ |
title: Standard Title |
|
349 |
+ |
--- |
|
350 |
+ |
`; |
|
351 |
+ |
const { frontmatter } = parseFrontmatter(content, { title: "heading" }); |
|
352 |
+ |
expect(frontmatter.title).toBe("Standard Title"); |
|
353 |
+ |
}); |
|
354 |
+ |
}); |
|
355 |
+ |
|
|
356 |
+ |
describe("body", () => { |
|
357 |
+ |
it("returns the body content after the closing delimiter", () => { |
|
358 |
+ |
const content = `--- |
|
359 |
+ |
title: Post |
|
360 |
+ |
--- |
|
361 |
+ |
# Heading |
|
362 |
+ |
|
|
363 |
+ |
Some paragraph text.`; |
|
364 |
+ |
const { body } = parseFrontmatter(content); |
|
365 |
+ |
expect(body).toBe("# Heading\n\nSome paragraph text."); |
|
366 |
+ |
}); |
|
367 |
+ |
|
|
368 |
+ |
it("returns an empty body when there is no content after frontmatter", () => { |
|
369 |
+ |
const content = `--- |
|
370 |
+ |
title: Post |
|
371 |
+ |
--- |
|
372 |
+ |
`; |
|
373 |
+ |
const { body } = parseFrontmatter(content); |
|
374 |
+ |
expect(body).toBe(""); |
|
375 |
+ |
}); |
|
376 |
+ |
}); |
|
377 |
+ |
}); |