-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerateManifest.js
84 lines (79 loc) · 2.15 KB
/
generateManifest.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
const fs = require('fs')
const fm = require('front-matter')
const markdown = './markdown/'
const manifest = 'manifest.json'
let arr = []
const path = require('path')
var mila = require('markdown-it-link-attributes')
let md = require('markdown-it')({
html: true,
xhtmlOut: false,
breaks: true,
langPrefix: 'language-',
linkify: true,
typographer: false,
quotes: '“”‘’',
})
.use(mila, {
attrs: {
target: '_blank',
rel: 'noopener',
},
})
.use(require('markdown-it-footnote'))
.use(require('markdown-it-named-headers'))
.use(require('markdown-it-attrs'))
/**
* Sort array of objects by property
*
* https://stackoverflow.com/questions/1129216/sort-array-of-objects-by-string-property-value
*/
function dynamicSort(property) {
var sortOrder = 1
if (property[0] === '-') {
sortOrder = -1
property = property.substr(1)
}
return function (a, b) {
var result =
a[property] < b[property] ? -1 : a[property] > b[property] ? 1 : 0
return result * sortOrder
}
}
const readFiles = (dirname) => {
const readDirPr = new Promise((resolve, reject) => {
fs.readdir(dirname, (err, filenames) =>
err ? reject(err) : resolve(filenames)
)
})
return readDirPr.then((filenames) =>
Promise.all(
filenames.map((filename) => {
return new Promise((resolve, reject) => {
fs.readFile(dirname + filename, 'utf-8', (err, content) => {
let obj = {}
obj = fm(content)
/**
* Move attributes.position into sortable position ...
*/
obj.position = obj.attributes.position
/**
* ... then delete it from attributes.position
*/
delete obj.attributes.position
let html = md.render(obj.body)
obj.html = html.replace(/(\r\n|\n|\r)/gm, '')
resolve(obj)
})
})
})
).catch((error) => Promise.reject(error))
)
}
readFiles('./markdown/').then(
(allContents) => {
allContents.sort(dynamicSort('position'))
fs.writeFileSync('manifest.json', JSON.stringify(allContents))
},
(error) => console.log(error)
)