-
Notifications
You must be signed in to change notification settings - Fork 2
/
build.js
61 lines (52 loc) · 1.79 KB
/
build.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
import fs from "node:fs/promises"
import glob from "tiny-glob"
import fm from "front-matter"
import { marked } from "marked"
const srcDir = "src"
const assetDir = "assets"
const distDir = "dist/api"
const assetDistDir = "dist/assets"
// prepare
await Promise.allSettled([
fs.rm(distDir, { recursive: true }),
fs.rm(assetDistDir, { recursive: true }),
])
await Promise.allSettled([
fs.mkdir(distDir, { recursive: true }),
fs.mkdir(assetDistDir, { recursive: true }),
])
// markdown
async function proc(srcDir, distFile) {
const files = await glob(`${srcDir}/**/*.md`)
console.log(files)
const data = { data: [] }
await Promise.all(
files.map(async path => {
const content = fm((await fs.readFile(path)).toString())
data.data.push({
slug: content.attributes.slug || path.slice(`${srcDir}/`.length, -3),
title: content.attributes.title,
description: content.attributes.description,
author: content.attributes.author,
noRobots: content.attributes.noRobots,
noTitleFormat: content.attributes.noTitleFormat,
date: content.attributes.date,
dateUpd: content.attributes.dateUpd,
showMeta: content.attributes.showMeta,
body: marked.parse(content.body),
})
})
)
data.data.sort((a, b) => new Date(b.date) - new Date(a.date))
await fs.writeFile(distFile, JSON.stringify(data))
}
for (const path of await glob(`${srcDir}/*`))
if ((await fs.stat(path)).isDirectory())
proc(path, `${distDir}/${path.slice(`${srcDir}/`.length)}.json`)
// assets
for (const path of await glob(`${assetDir}/**/*`)) {
const pathRel = path.slice(`${assetDir}/`.length)
if ((await fs.stat(path)).isDirectory())
await fs.mkdir(`${assetDistDir}/${pathRel}`)
else await fs.copyFile(path, `${assetDistDir}/${pathRel}`)
}