forked from sveltejs/svelte
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
80 lines (66 loc) · 1.99 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
// @ts-check
import { extractFrontmatter } from '@sveltejs/site-kit/markdown';
import { CONTENT_BASE_PATHS } from '../../../constants.js';
import { render_content } from '../renderer.js';
/**
* @param {import('./types').BlogData} blog_data
* @param {string} slug
*/
export async function get_processed_blog_post(blog_data, slug) {
for (const post of blog_data) {
if (post.slug === slug) {
return {
...post,
content: await render_content(post.file, post.content)
};
}
}
return null;
}
const BLOG_NAME_REGEX = /^(\d{4}-\d{2}-\d{2})-(.+)\.md$/;
/** @returns {Promise<import('./types').BlogData>} */
export async function get_blog_data(base = CONTENT_BASE_PATHS.BLOG) {
const { readdir, readFile } = await import('node:fs/promises');
/** @type {import('./types').BlogData} */
const blog_posts = [];
for (const file of (await readdir(base)).reverse()) {
if (!BLOG_NAME_REGEX.test(file)) continue;
const { date, date_formatted, slug } = get_date_and_slug(file);
const { metadata, body } = extractFrontmatter(await readFile(`${base}/${file}`, 'utf-8'));
blog_posts.push({
date,
date_formatted,
content: body,
description: metadata.description,
draft: metadata.draft === 'true',
slug,
title: metadata.title,
file,
author: {
name: metadata.author,
url: metadata.authorURL
}
});
}
return blog_posts;
}
/** @param {import('./types').BlogData} blog_data */
export function get_blog_list(blog_data) {
return blog_data.map(({ slug, date, title, description, draft }) => ({
slug,
date,
title,
description,
draft
}));
}
/** @param {string} filename */
function get_date_and_slug(filename) {
const match = BLOG_NAME_REGEX.exec(filename);
if (!match) throw new Error(`Invalid filename for blog: '${filename}'`);
const [, date, slug] = match;
const [y, m, d] = date.split('-');
const date_formatted = `${months[+m - 1]} ${+d} ${y}`;
return { date, date_formatted, slug };
}
const months = 'Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec'.split(' ');