-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
43 lines (36 loc) · 1021 Bytes
/
index.js
File metadata and controls
43 lines (36 loc) · 1021 Bytes
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
const path = require("path")
const dateRes = [
/\/(\d{4})-(\d{2})-(\d{2})-/,
/\/(\d{4})\/(\d{2})-(\d{2})-/,
/\/(\d{4})\/(\d{2})\/(\d{2})-/,
]
function guessDate(url) {
for (const re of dateRes) {
let mo = url.match(re)
if (mo !== null) {
return new Date(mo[1], mo[2]-1, mo[3])
}
}
}
function addData(content, data) {
let url = data._source.replace(".md", ".html")
if (path.extname(url) !== ".html") {
throw "Invalid source file extension: " + data._source
}
data.url = "/" + url.split(path.sep).join("/")
data.url = data.url.replace(/index\.html$/, "")
data.date = guessDate(data.url)
return content
}
module.exports = (config) => {
let oldpre = config.processors.pre
config.processors.pre = async (content, data) => {
return addData(await oldpre(content, data), data)
}
let md = require("markdown-it")()
config.processors.exts = {
// eslint-disable-next-line no-unused-vars
".md": (content, data) => md.render(content),
".html": null,
}
}