-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
341 lines (310 loc) · 8.96 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
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
/**
* TODO
* [] Add zip file download for oss (with hover animation for download or share?)
* [x] Update styles for posts
* [x] Add image rendering for posts
* [x] Migrate all content from blog.pauldariye.com
* [] Add night theme
* [x] Add progress indicator for posts
* [ ] Add view counter
* [] Add ga
* [] a11y
* [] audio/media/image styles --- keep it simple .. black/white/monotone --
* futuristic
* [] serviceworkers
* [x] lru-cache
* [] SEO
* [] add twitter, github, icons
* [] Code snippet copy to clipboard
* [] Add hearts
* [] Copy code snippet
*
* micro-site features
* [] custom styles for general layout and code
*/
const fs = require('fs')
const path = require('path')
const toPromise = require('denodeify')
const { send } = require('micro')
const mime = require('mime')
const { parse } = require('url')
const { green, red } = require('chalk')
const handlebars = require('handlebars')
const showdown = require('showdown')
const twitter = require('showdown-twitter')
const youtube = require('showdown-youtube')
const hljs = require('highlight.js')
const unslug = require('unslug')
const LRUCache = require('lru-cache')
const ssrCache = new LRUCache({
max: 100,
maxAge: 1000 * 60 * 60
})
const relativePathExtension = function (path) {
const self = this
this.relativePath = path || '/'
this.extension = function () {
return [
{
type: 'lang',
filter: function (text) {
const data = { relativePath: self.relativePath }
return handlebars.compile(text)(data)
}
}
]
}
}
const relativeExtension = new relativePathExtension()
showdown.extension('relative', relativeExtension.extension)
const videoExtension = function () {
return [
{
type: 'lang',
filter: function (text, converter, options) {
const regex = /!\[[^\]]*\]\(.*?\.(?:mp4).*?(?=\"|\))(\".*\")?\)/g
const matches = regex.exec(text)
console.log(matches)
return text
}
}
]
}
showdown.extension('video', videoExtension)
showdown.extension('codehighlight', function() {
function htmlunencode(text) {
return (
text
.replace(/&/g, '&')
.replace(/</g, '<')
.replace(/>/g, '>')
);
}
return [
{
type: 'output',
filter: function (text, converter, options) {
// use new shodown's regexp engine to conditionally parse codeblocks
var left = '<pre><code\\b[^>]*>',
right = '</code></pre>',
flags = 'g',
replacement = function (wholeMatch, match, left, right) {
// unescape match to prevent double escaping
match = htmlunencode(match);
return left + hljs.highlightAuto(match).value + right;
};
return showdown.helper.replaceRecursiveRegExp(text, replacement, left, right, flags);
}
}
];
});
const converter = new showdown.Converter({
parseImageDimension: true,
encodeEmails: true,
openLinksInNewWindow: true,
smartIndentationFix: true,
smoothLivePreview: true,
extensions: ['youtube', 'relative', 'codehighlight', 'video']
})
converter.setFlavor('github')
/**
* Handlebar helpers
*/
handlebars.registerHelper('year', () => new Date().getFullYear())
const defaultPort = 3000
const imageExtensions = new Set([
'.jpeg',
'.jpg',
'.png',
'.gif',
'.bmp',
'.tiff',
'.tif',
'.ico',
'.svg'
])
const downloadExtensions = new Set([
'.zip',
'.pdf'
])
const ignoredFiles = new Set(['.git', '.DS_Store', '404', '500', 'pdbot'])
const root = path.resolve(process.cwd(), '_pd')
const rootObj = path.parse(root)
const isDirectory = async directory => {
try {
const stats = await toPromise(fs.stat)(directory)
return stats.isDirectory()
} catch (err) {
return false
}
}
const exists = async filePath => {
try {
await toPromise(fs.stat)(filePath)
return true
} catch(err) {
return false
}
}
let cachedView = null
const getView = async () => {
if (!cachedView || process.env.NODE_ENV === 'development') {
try {
const file = await toPromise(fs.readFile)(
path.resolve(__dirname, './views/index.hbs'),
'utf8'
)
cachedView = handlebars.compile(file)
} catch(err) {
throw err
}
}
return cachedView
}
let cachedAsset = {}
const getAsset = async assetPath => {
if (!cachedAsset[assetPath] || process.env.NODE_ENV === 'development') {
try {
const file = await toPromise(fs.readFile)(
path.resolve(__dirname, './assets', assetPath),
'utf8'
)
cachedAsset[assetPath] = file
} catch(err) {
throw err
}
}
return cachedAsset[assetPath]
}
const toHtml = async (dirPath, filePath) => {
const file = await toPromise(fs.readFile)(filePath, 'utf8')
relativeExtension.relativePath = dirPath.replace(rootObj.name, '')
if (file) return converter.makeHtml(file)
}
const isMarkdown = pathObj => {
if (pathObj.ext === '.md') return true
return false
}
const getCacheKey = dirPath => `${dirPath}`
const renderDir = async directory => {
const files = await toPromise(fs.readdir)(directory)
const dirObj = path.parse(directory)
let dirPath = `${dirObj.dir}/${dirObj.base}`.replace(`${rootObj.dir}/`, ``)
let dirPathParts = dirPath.split('/')
const key = getCacheKey(dirPath)
if (ssrCache.has(key)) {
return ssrCache.get(key)
}
const data = {
directories: [],
images: [],
downloads: [],
path: [],
page: {},
assetsDir: '/assets',
folder: dirObj.name,
}
let url = []
for (let i = 0; i < dirPathParts.length; ++i) {
if (dirPathParts[i] !== rootObj.base) {
url.push(dirPathParts[i])
}
data.path.push({
url: url.join('/'),
name: dirPathParts[i]
})
}
for (let i = 0; i < files.length; ++i) {
if (ignoredFiles.has(files[i])) continue
const filePath = path.resolve(root, path.resolve(directory, files[i]))
const pathObj = path.parse(filePath)
const relativeFilePath = path.relative(
root,
path.resolve(directory, files[i])
)
if (await isDirectory(filePath)) {
data.directories.push({
relative: relativeFilePath,
name: files[i]
})
} else if (isMarkdown(pathObj)) {
data.page.title = unslug(pathObj.name)
data.page.canonical = `${dirPath.replace(rootObj.name, '')}/${pathObj.name}`
data.page.content = await toHtml(dirPath, filePath)
} else if (imageExtensions.has(pathObj.ext)) {
data.images.push({
relative: relativeFilePath,
name: files[i]
})
} else if (downloadExtensions.has(pathObj.ext)) {
data.downloads.push({
relative: relativeFilePath,
extension: pathObj.ext.replace('.', ''),
name: files[i]
})
}
}
const view = await getView()
const enriched = view(data)
ssrCache.set(key, enriched)
return enriched
}
const renderFile = async file => {
try {
const content = await toPromise(fs.readFile)(path.resolve(root, file))
return {
content,
mime: mime.getType(file)
}
} catch(err) {
throw err
}
}
module.exports = async (req, res) => {
const { pathname } = parse(req.url)
const pathObj = path.parse(path.join(root, pathname))
const reqPath = decodeURIComponent(path.format(pathObj))
if (pathname.startsWith('/assets')) {
const asset = await getAsset(pathname.replace('/assets/', ''))
res.setHeader('Content-Type', `${mime.getType(pathname)}; charset=utf-8`)
return send(res, 200, asset)
}
if (!await exists(reqPath)) {
const pathObj = path.parse(path.join(root, '/404'))
const reqPath = decodeURIComponent(path.format(pathObj))
const renderedDir = await renderDir(reqPath)
return send(res, 404, renderedDir)
}
if (pathObj.ext === '') {
const renderedDir = await renderDir(reqPath)
return send(res, 200, renderedDir)
} else if (imageExtensions.has(pathObj.ext)) {
try {
const image = await renderFile(reqPath)
res.setHeader('Content-Type', `${image.mime}; charset=utf-8`)
return send(res, 200, image.content)
} catch(err) {
const pathObj = path.parse(path.join(root, '/500'))
const reqPath = decodeURIComponent(path.format(pathObj))
const renderedDir = await renderDir(reqPath)
return send(res, 500, renderedDir)
}
} else if (downloadExtensions.has(pathObj.ext)) {
try {
const download = await renderFile(reqPath)
res.setHeader('Content-disposition', `attachment; filename=${pathObj.name}${pathObj.ext}`)
res.setHeader('Content-Type', `${download.mime}`)
return send(res, 200, download.content)
} catch(err) {
const pathObj = path.parse(path.join(root, '/500'))
const reqPath = decodeURIComponent(path.format(pathObj))
const renderedDir = await renderDir(reqPath)
return send(res, 500, renderedDir)
}
} else {
const pathObj = path.parse(path.join(root, '/500'))
const reqPath = decodeURIComponent(path.format(pathObj))
const renderedDir = await renderDir(reqPath)
return send(res, 500, renderedDir)
}
}