-
Notifications
You must be signed in to change notification settings - Fork 28
/
build.js
176 lines (153 loc) · 3.63 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import {
basename,
extname,
join
} from 'node:path';
import {
readdirSync,
readFileSync,
createWriteStream,
writeFileSync,
} from 'node:fs';
import { ensureDirSync } from 'fs-extra';
import { createElement as x } from 'react';
import { renderToStaticNodeStream } from 'react-dom/server';
import matter from 'gray-matter';
import { remark } from 'remark';
import remarkHTML from 'remark-html';
import remarkExcerpt from 'remark-excerpt';
import remarkGFM from "remark-gfm";
import Html from './src/Html.js';
import Home from './src/Home.js';
import NotFound from './src/404.js';
import Blog from './src/Blog.js';
import Post from './src/Post.js';
const config = {
title: 'Jxnblk | Brent Jackson',
description: 'Aspiring indie game developer, software engineer, and proud cat parent – he/him',
};
async function parseFile (filename) {
const slug = basename(filename, ".md");
const path = 'blog/' + slug;
const raw = readFileSync(filename); // join('src', 'posts', filename));
const { content, data } = matter(raw);
const vf = await remark()
.use(remarkGFM)
.use(remarkHTML, { sanitize: false })
.process(content);
const html = String(vf);
const evf = await remark()
// .use(remarkGFM)
.use(remarkExcerpt)
.use(remarkHTML, { sanitize: false })
.process(content);
const excerpt = String(evf);
return {
filename,
path,
slug,
raw,
html,
excerpt,
description: data.description || data.excerpt || null,
...data,
content: Post({ ...data, html })
};
}
async function getPages () {
const mds = [
...readdirSync('src/posts')
.filter(f => extname(f) == '.md')
.map(f => join('src', 'posts', f)),
...readdirSync('src/devlog')
.filter(f => extname(f) == '.md')
.map(f => join('src', 'devlog', f)),
];
const promises = mds.map(parseFile);
// console.log(`Found ${mds.length} blog posts`);
const posts = await Promise.all(promises);
const pages = [ ...posts ];
pages.push({
path: 'blog',
content: Blog({ posts }),
});
pages.push({
path: '',
content: Home({ posts }),
});
pages.push({
path: '404',
content: NotFound(),
})
return pages;
};
const pages = await getPages();
console.log(pages.length);
pages.forEach(page => {
if (page.path) ensureDirSync(page.path);
let filename = join(page.path, 'index.html');
if (page.path == '404') {
filename = '404.html';
};
const write = createWriteStream(filename);
const stream = renderToStaticNodeStream(x(Html, page));
write.write('<!DOCTYPE html>\n');
stream.pipe(write);
write.on('finish', () => {
console.log(page.path);
});
});
// api
//
function pageToData ({
path,
slug,
title,
description,
date,
excerpt,
html,
draft,
tags,
image,
}) {
return {
path,
slug,
title,
description,
date,
excerpt,
html,
draft,
tags,
image,
};
}
const exclude = ["", "blog", "404", "blog/notes"];
const posts = pages.filter(p => !exclude.includes(p.path))
.map(pageToData)
.sort((a, b) => b.date - a.date);
const index = posts.map(p => ({
path: p.path,
slug: p.slug,
title: p.title,
date: p.date,
excerpt: p.excerpt,
draft: p.draft,
tags: p.tags,
}));
const about = pageToData(await parseFile("about.md"));
const all = {
posts,
about
}
const indexJSON = JSON.stringify(index);
const allJSON = JSON.stringify(all);
writeFileSync(join('api', 'index.json'), indexJSON);
writeFileSync(join('api', 'all.json'), allJSON);
posts.forEach(post => {
ensureDirSync(join('api', post.path));
const json = JSON.stringify(post);
writeFileSync(join('api', post.path, 'index.json'), json);
});