-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
121 lines (104 loc) · 4.37 KB
/
main.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
const fs = require('fs-extra')
const path = require('path')
const yaml = require('js-yaml')
const { promisify } = require('util')
const glob = promisify(require('glob'))
const sm = require('sitemap')
const nunjucks = require('nunjucks')
//const nunjucksRender = promisify(nunjucks.render)
const config = require('./site.config')
const distPath = config.build.distPath
const layouts = config.layouts
module.exports = {
build: () => {
let env = nunjucks.configure('layouts')
env.addFilter('sortByTime', function (arr, reversed, attr) {
return arr.sort(function compare(a, b) {
if (Date.parse(a[attr]) > Date.parse(b[attr])) {
return reversed ? -1 : 1
}
if (Date.parse(a[attr]) < Date.parse(b[attr])) {
return reversed ? 1 : -1
}
// if time was not definied put it at the end
if (!a[attr]) {
return 1
}
return 0
})
})
env.addFilter('joinArray', function (arr, del, arrDel) {
return arr.map(item =>
item.join(del)
).join(arrDel)
})
// copy assets folder
fs.copy('static', `${distPath}/static`)
.catch(err => { console.error(err) })
// read page templates
// looking for .yaml files, return paths of found files
// cwd: The current working directory in which to search
glob('**', { cwd: 'content' }) //glob('**/*.yaml', { cwd: 'content' })
.then((files) => {
config.preHook(files)
return files
})
.then((files) => {
let urls = []
files.forEach((file) => {
if (file.endsWith('.yaml')) {
// get info data
const fileInfo = path.parse(file)
// destination path without filename and extension
const destDir = path.join(distPath, fileInfo.dir)
// defines a current working directory in which to search
// create destination directory
fs.mkdirsSync(destDir)
// render layout with page content
let layout
let data = yaml.safeLoad(fs.readFileSync(`content/${file}`))
data.Page = {}
// allow layouts to access absolute path
data.Page.path = fileInfo
// check if a costum layout file was specified (with a rule)
for (let rule of layouts) {
if (`content/${file}`.includes(rule.path)) {
layout = rule.layout
let url = {
url: rule.path,
changefreq: 'weekly',
priority: rule.priority,
lastmodrealtime: true,
lastmodfile: `content/${file}`
}
urls.push(url)
let ruleData = rule.data
if (ruleData) {
if (ruleData.indexOf('static/') === -1)
ruleData = `static/${ruleData}`
if (ruleData.indexOf('.yaml') === -1)
ruleData = `${ruleData}.yaml`
data.Data = (yaml.safeLoad(fs.readFileSync(`${distPath}/${ruleData}`)))
}
break
}
}
if (layout) {
fs.writeFile(`${destDir}/${fileInfo.name}.html`, nunjucks.render(layout, data))
}
} else if (file.indexOf('.') !== -1) {
fs.copy(`content/${file}`, `${distPath}/${file}`)
.catch(err => { console.error(err) })
}
})
let sitemap = sm.createSitemap({
hostname: 'https://www.shortlib.com',
cacheTime: 600000, //600 sec (10 min) cache purge period
urls: urls
})
fs.writeFile(`${distPath}/sitemap.xml`, sitemap.toString())
.catch((err) => { console.error(err) })
})
.catch((err) => { console.error(err) })
}
}