forked from mendix/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmenu_build.js
194 lines (173 loc) · 5.73 KB
/
menu_build.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
const path = require('path');
const Promise = require('bluebird');
const _ = require('lodash');
const yamlFront = require('yaml-front-matter');
const YAML = require('yamljs');
const { normalizeSafe } = require('upath');
const {getGenerateFiles, isFile, readFile, writeFile, readSourceFiles} = require('./helpers');
const commandLineHelpers = require('./helpers/command_line');
const PUBLISH_DRAFTS = typeof process.env.HUGO_ENV !== 'undefined' ? process.env.HUGO_ENV === 'test' : false;
const { cyan, red } = commandLineHelpers.colors;
const log = commandLineHelpers.log('menu build');
let errors = false;
let SPACES = {};
const getSourceFilesPromise = (opts) => {
return (files) => {
SPACES = YAML.load(opts.space);
return new Promise((resolve, reject) => {
const raw = _.map(files, p => {
const file = {
path: p
};
file.basePath = file.path.replace(opts.src, '');
const parsed = path.parse(file.basePath),
base = normalizeSafe(path.join(opts.src, parsed.dir, parsed.name)),
dirName = parsed.dir.split('/')[1];
if (!!dirName && SPACES[dirName]) {
file.url = normalizeSafe(path.join(parsed.dir, parsed.name === 'index' ? '/' : parsed.name));
if (file.url.split('/').length === 3 && parsed.name === 'index') {
file.main = true;
}
file.id = parsed.name;
file.space = SPACES[dirName].space;
file.dir = `${parsed.dir}/`;
if (isFile(base + '.md')) {
file.sourcePath = base + '.md';
} else if (isFile(base + '.html')) {
file.sourcePath = base + '.html';
}
} else {
// console.log(file);
}
return file;
});
files = _.filter(raw, file => !!file.sourcePath);
resolve(files);
})
};
}
const parseSourceFiles = files =>
readSourceFiles(files, (file, contents, resolve) => {
let meta = null;
try {
meta = yamlFront.loadFront(contents);
} catch(e) {
console.log(e);
}
if (meta !== null) {
file.meta = true;
_.merge(file, _.omit(meta, ['__content', 'space']));
}
resolve(file);
});
const filterAndBindSpace = (files) =>
_.map(_.keys(SPACES), (spaceKey) => {
const obj = SPACES[spaceKey];
obj.filename = `${spaceKey}.json`;
obj.files = _.filter(files, (file) => file.space === obj.space);
return obj;
}).filter(space => {
if (typeof space.draft === 'undefined' || PUBLISH_DRAFTS) {
return true;
}
if (space.draft && !PUBLISH_DRAFTS) {
return false;
}
return true;
});
const filterDrafts = (files) =>
_.filter(files, (file) => {
if (typeof file.draft !== 'undefined') {
return file.draft === PUBLISH_DRAFTS;
}
return true;
});
const checkSpaces = (spaceArray) =>
_.map(spaceArray, (space) => {
const categories = _.map(space.menu_categories, cat => cat.toLowerCase())
const files = space.files;
space.files = _.filter(files, file => {
if (!file.meta) {
log(`No metadata for file: ${cyan(file.basePath)}, is the block at the top correct?`);
errors = true;
return false;
}
if (!file.title) {
log(`Page ${cyan(file.basePath)} does not have a title. ${red('Is the metadata correct? It will be rendered, but not visible in the menu & search')}`);
errors = true;
return false;
}
if (file.category && !_.find(files, f => f.title === file.category) && categories.indexOf(file.category.toLowerCase()) === -1) {
log(`File ${cyan(file.basePath)} has category ${cyan(file.category)} which has no file associated to it or is not used in ${cyan('spaces.yml')}`);
return false;
}
if (file.parent && !_.find(files, f => f.url === normalizeSafe(path.join(file.dir, file.parent)))) {
log(`File ${cyan(file.basePath)} has parent ${cyan(file.parent)} which would resolve to ${cyan(normalizeSafe(path.join(file.dir, file.parent)))}, but this does not exist`);
return false;
}
return true
})
return space;
});
const writeSpace = opts => (space => writeFile(path.join(opts.destination, space.filename), JSON.stringify(space.content)))
const writeSpaces = opts => {
const keyMap = {
'title': 't',
'category': 'c',
'id': 'i',
'url': 'u',
'dir': 'd',
'parent': 'p',
'main': 'm',
'menu_title': 'mt',
'menu_order': 'mo',
'draft': 'dr'
};
return (spaceArray) => Promise.all(_.map(spaceArray, (space) => {
const pageObj = {
filename: space.filename,
content: {
categories: space.menu_categories,
pages: _.map(space.files, file => {
const f = _.pick(file, ['title', 'category', 'id', 'url', 'dir', 'parent', 'main', 'menu_title', 'menu_order', 'draft']);
const newObject = {};
_.keys(f).forEach(k => {
if (keyMap[k]) {
newObject[keyMap[k]] = f[k];
}
});
return newObject;
})
}
};
const writePromise = writeSpace(opts);
return writePromise(pageObj)
.then(() => {
return pageObj;
});
}));
}
const build = (opts) => new Promise((resolve, reject) => {
const {src, destination, spaceFile} = opts;
log(`Building drafts: ${cyan(PUBLISH_DRAFTS ? 'yes' : 'no')}`);
getGenerateFiles(src)
.then(getSourceFilesPromise(opts))
.then(parseSourceFiles)
.then(filterDrafts)
.then(filterAndBindSpace)
.then(checkSpaces)
.then(writeSpaces(opts))
.then(arr => {
return writeFile(path.join(opts.destination, 'spaces.json'), JSON.stringify(arr));
})
.then(() => {
resolve(errors);
})
.catch((e) => {
console.log(e);
reject(e);
});
})
module.exports = {
build
};