-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.js
83 lines (73 loc) · 1.9 KB
/
utils.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
const fs = require('fs')
const YAML = require('yaml')
// Def custom tags
const env = {
tag: '!env',
resolve: str => str
}
const include = {
tag: '!include',
resolve: str => ''
}
const path = {
tag: '!path',
resolve: str => str
}
const from = {
tag: '!from',
resolve: str => ''
}
// Exported functions
const parseChapters = (foliantConfig, sourceDir, listOfFiles) => {
// Get foliant config
const config = getFoliantConfig(foliantConfig)
eachRecursive(config.chapters, listOfFiles, sourceDir)
return listOfFiles
}
const existIncludes = (foliantConfig) => {
// Exist includes in list of preprocessors
const config = getFoliantConfig(foliantConfig)
return config.preprocessors.includes('includes')
}
const getFoliantConfig = (foliantConfig) => {
// Get foliant config
const configContent = fs.readFileSync(foliantConfig, 'utf8')
return YAML.parse(configContent, { customTags: [env, include, path, from] })
}
const updateListOfFiles = (sourceDir, IncludesMapPath, listOfFiles) => {
// Get files from includes map
const includesMapContent = JSON.parse(fs.readFileSync(IncludesMapPath, 'utf8'))
eachRecursive(includesMapContent, listOfFiles, sourceDir)
// Remove duplicates
listOfFiles = [...new Set(listOfFiles)]
return listOfFiles
}
function eachRecursive (obj, list, sourceDir) {
for (const k in obj) {
if (typeof obj[k] === 'string') {
const s = obj[k]
if (s.endsWith('.md')) {
if (s.startsWith(sourceDir)) {
if (fs.existsSync(s)) {
list.push(s)
}
} else {
if (!s.startsWith('http')) {
if (fs.existsSync(`${sourceDir}/${s}`)) {
list.push(`${sourceDir}/${s}`)
}
}
}
}
} else {
eachRecursive(obj[k], list, sourceDir)
}
}
}
// Export functions
module.exports = {
parseChapters,
updateListOfFiles,
getFoliantConfig,
existIncludes
}