-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbook.js
26 lines (21 loc) · 988 Bytes
/
book.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
import fs from 'node:fs/promises';
import path from 'node:path';
import markdownit from 'markdown-it';
import matter from 'gray-matter';
(async() => {
const files = await fs.readdir(path.join(process.cwd(), 'static/books'));
const mdFiles = files.filter(file => path.extname(file).toLowerCase() === '.md');
const output = [];
for (const mdFile of mdFiles) {
const rawContent = await fs.readFile(path.join(process.cwd(), 'static/books', mdFile), 'utf8');
const { content: body, data: attributes } = matter(rawContent);
const { title, description = '', image, author } = attributes;
const slug = mdFile.split('.md')[0];
const md = markdownit({
breaks: true
});
const reviewHtml = md.render(body);
output.push({ title, description, slug, image, author, reviewHtml });
}
fs.writeFile(path.join(process.cwd(), 'src/lib/books.json'), JSON.stringify(output, null, " "), null);
})();