forked from vkarpov15/thecodebarbarian.com
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
203 lines (166 loc) · 5.37 KB
/
index.js
File metadata and controls
203 lines (166 loc) · 5.37 KB
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
'use strict';
const acquit = require('acquit');
const fs = require('fs');
const markdown = require('marked');
const file = require('file');
const feed = require('feed');
const pug = require('pug');
const transform = require('acquit-require');
require('acquit-ignore')();
const highlight = require('highlight.js');
markdown.setOptions({
highlight: function(code, language) {
if (language && language.toLowerCase() === 'javascript') {
return highlight.highlight('JavaScript', code).value;
}
if (language && language.toLowerCase() === 'typescript') {
return highlight.highlight('TypeScript', code).value;
}
return code;
}
});
const posts = require('./lib/posts');
const postsConfig = [].concat(posts).reverse();
async function getPosts() {
const tests = [
...acquit.parse(fs.readFileSync('./test/20180621.test.js').toString()),
...acquit.parse(fs.readFileSync('./test/20180702.test.js').toString()),
...acquit.parse(fs.readFileSync('./test/20180710.test.js').toString())
];
await Promise.all(posts.map(async (post) => {
const md = await new Promise((resolve, reject) => {
fs.readFile(post.src, (err, res) => {
if (err != null) {
return reject(err);
}
resolve(res);
});
});
post.md = md.toString();
post.md = transform(post.md, tests);
}));
return posts;
}
var tags = {};
posts.forEach(function(post) {
(post.tags || []).forEach(function(tag) {
tags[tag] = tags[tag] || [];
tags[tag].unshift(post);
});
});
async function getCompiledPosts() {
const posts = await getPosts();
const filename = './lib/views/post.pug';
const postTemplate = pug.compile(fs.readFileSync(filename, 'utf8'), { filename });
for (const post of posts) {
post.rawPreview = post.md.substr(0, post.md.indexOf('\n'));
post.preview = markdown(post.md.substr(0, post.md.indexOf('\n')));
const output = postTemplate({
post: post,
content: markdown(post.md.toString()),
allPosts: postsConfig
});
post.compiled = output;
}
return posts;
}
async function generatePosts() {
console.log('Generating posts');
const compiledPosts = await getCompiledPosts();
for (const post of compiledPosts) {
console.log('Writing "' + post.title + '"');
const path = file.path.join(post.dest.directory, post.dest.name + '.html');
fs.writeFileSync(path, post.compiled);
}
}
async function generateTags() {
const filename = './lib/views/list.pug';
const listTemplate = pug.compile(fs.readFileSync(filename, 'utf8'), { filename });
for (const key of Object.keys(tags)) {
const output = listTemplate({
tag: key,
posts: [].concat(tags[key]),
allPosts: postsConfig,
pageNum: 0,
isLastPage: false
});
fs.writeFileSync('./bin/tag/' + key.toLowerCase() + '.html', output);
}
}
async function generateIndex() {
const filename = './lib/views/index.pug';
const index = pug.compile(fs.readFileSync(filename, 'utf8'), { filename });
const compiledPosts = await getCompiledPosts();
const posts = [].concat(compiledPosts).reverse();
const output = index({
posts,
allPosts: postsConfig
});
fs.writeFileSync('./bin/index.html', output);
fs.writeFileSync('./bin/CNAME', 'thecodebarbarian.com');
}
async function generateRecommendations() {
const filename = './lib/views/recommendations.pug';
const options = { filename };
options.filters = {
markdown: function(block) {
return markdown(block);
}
};
const recommendations = pug.compile(fs.readFileSync(filename, 'utf8'), options);
fs.writeFileSync('./bin/recommendations.html', recommendations(options));
}
async function generatePages() {
const compiledPosts = await getCompiledPosts();
const filename = './lib/views/list.pug';
const listTemplate = pug.compile(fs.readFileSync(filename, 'utf8'), { filename });
var pages = [];
var reversed = [].concat(compiledPosts).reverse();
for (var i = 8; i < posts.length; i += 8) {
pages.push(listTemplate({
tag: 'Page ' + Math.floor(i / 8),
posts: reversed.slice(i, i + 8),
allPosts: postsConfig,
pageNum: Math.floor(i / 8),
isLastPage: !(i + 8 < posts.length)
}));
}
for (const [i, page] of Object.entries(pages)) {
fs.writeFileSync('./bin/page/' + (+i + 1) + '.html', page);
}
}
async function generateFeed() {
const compiledPosts = await getCompiledPosts();
const posts = Array.from(compiledPosts);
const f = new feed({
title: 'TheCodeBarbarian.com',
description: 'Detailed articles about the MEAN stack and related topics',
link: 'http://thecodebarbarian.com',
image: 'http://thecodebarbarian.com/images/Barbarian_Head.png',
author: {
name: 'Valeri Karpov'
}
});
const reversed = posts.reverse();
for (let i = 0; i < reversed.length; ++i) {
f.addItem({
title: posts[i].title,
link: 'https://thecodebarbarian.com' + posts[i].dest.directory.substr('./bin'.length) + '/' + posts[i].dest.name + '.html',
date: posts[i].date.toDate()
});
}
fs.writeFileSync('./bin/feed.xml', f.render('rss-2.0').replace(new RegExp('<content:encoded/>', 'g'), ''));
}
module.exports = run;
run().catch(err => console.log(err));
async function run() {
await Promise.all([
generatePosts(),
generateTags(),
generateIndex(),
generateRecommendations(),
generatePages(),
generateFeed()
]);
console.log('Done');
}