forked from mendix/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjson.js
More file actions
296 lines (248 loc) · 9.98 KB
/
Copy pathjson.js
File metadata and controls
296 lines (248 loc) · 9.98 KB
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
const restify = require('restify');
const path = require('path');
const url = require('url');
const Promise = require('bluebird');
const YAML = require('yamljs');
const yamlFront = require('yaml-front-matter');
const _ = require('lodash');
const cheerio = require('cheerio');
const { normalizeSafe } = require('upath');
const { getFiles, readFile, isFile } = require('./helpers');
const commandLineHelpers = require('./helpers/command_line');
const log = commandLineHelpers.log('json server');
const CONTENTFOLDER = 'content';
const SNIPPETSFOLDER = 'snippets';
const GENERATEDFOLDER = '_site';
let server;
let mainFolder;
let SPACES;
const contentHandler = (req, res, next) => {
const contentPath = req.params[0];
log(`Handling content: ${contentPath}`);
const sourcePath = normalizeSafe(path.resolve(mainFolder, CONTENTFOLDER, contentPath));
const generatePath = normalizeSafe(path.resolve(mainFolder, GENERATEDFOLDER, contentPath));
const sourceRoot = normalizeSafe(path.resolve(mainFolder, CONTENTFOLDER));
const generateRoot = normalizeSafe(path.resolve(mainFolder, GENERATEDFOLDER));
const sourceFile = ['.md', '/index.md']
.map(suffix => {
const absPath = sourcePath + suffix;
return isFile(absPath) ? absPath : null
})
.filter(f => f !== null);
const targetFile = ['.html', '/index.html']
.map(suffix => {
const absPath = generatePath + suffix;
return isFile(absPath) ? absPath : null
})
.filter(f => f !== null);
if (sourceFile.length !== 1) {
log(`Handling content: ${contentPath}, source not found`);
res.send(404, 'source file not found');
return next();
}
if (targetFile.length !== 1) {
log(`Handling content: ${contentPath}, target not found`);
res.send(404, 'target file not found');
return next();
}
const source = sourceFile[0];
const target = targetFile[0];
Promise.join(
readFile(source).
then(content => {
const obj = {
pathMarkdown: normalizeSafe(source.replace(sourceRoot, '')),
markdown: content.toString(),
snippets: []
};
const parsed = path.parse(obj.pathMarkdown);
const dirName = parsed.dir.split('/')[1];
const spaceObj = SPACES[dirName];
let menu;
if (spaceObj) {
try {
menu = require(path.resolve(mainFolder, 'static/json', dirName + '.json'));
} catch (e) {
log(`Error getting menu file for ${dirName}: `, e);
menu = null;
}
}
let meta = null;
try {
meta = yamlFront.loadFront(content);
} catch (e) {
log('Error loading front matter for ' + source, e);
meta = null;
}
obj.space = spaceObj ? spaceObj.space : null;
if (meta !== null) {
_.merge(obj, _.omit(meta, ['__content', 'space']));
obj.markdown = meta['__content'];
}
if (obj.parent) {
obj.parent = normalizeSafe(path.join(parsed.dir, obj.parent));
} else if (spaceObj) {
if (obj.category && menu !== null && menu.pages) {
const parent = _.find(menu.pages, p => p.t === obj.category);
if (parent && parent.u) {
obj.parent = parent.u;
delete obj.category;
} else {
log(`Can't find a parent for page: ${obj.pathMarkdown}`);
delete obj.category;
}
} else if (!obj.category && !obj.parent && menu !== null && menu.pages && menu.categories) {
if (menu.categories.indexOf(obj.title) !== -1) {
obj.parent = `/${dirName}/`;
} else {
log(`Can't find a parent/category for page: ${obj.pathMarkdown}`);
}
}
}
const snippetRegEx = /{{% snippet file="([a-zA-Z0-9\/\+]+\.md)" %}}/gi;
const matches = obj.markdown.match(snippetRegEx);
if (matches && matches.length > 0) {
obj.snippets = matches.map(m => '/' + m.replace(snippetRegEx, '$1'));
}
return obj
}),
readFile(target).
then(content => {
const obj = {
pathHtml: normalizeSafe(target.replace(generateRoot, '')),
};
const images = [];
const links = [];
const $ = cheerio.load(content);
const cheerioContent = $('.mx__page__content');
if (cheerioContent) {
obj.html = cheerioContent.html();
$('img', cheerioContent).each((i, el) => {
const src = $(el).attr('src');
const parsed = url.parse(src);
if (!parsed.hostname) {
// We're only handling local files
if (src.indexOf('/') === 0) {
images.push(src);
} else {
const t = target.replace(generateRoot, '');
const u = path.parse(t);
const s = normalizeSafe(path.join(u.dir, src))
images.push(s);
}
}
});
$('a', cheerioContent).each((i, el) => {
const $el = $(el);
const href = $el.attr('href');
if (
!!href &&
href !== "" &&
href.indexOf('#') !== 0 &&
href.indexOf('http') !== 0 &&
href.indexOf('mailto') !== 0
) {
try {
if (href.indexOf('/') === 0) {
links.push(href);
} else {
const t = target.replace(generateRoot, '');
const u = path.parse(t);
const s = normalizeSafe(path.join(u.dir, href));
links.push(s);
}
} catch (e) {
console.log('Error parsing link: ', href, e);
}
}
});
}
obj.images = _.uniq(images);
obj.links = _.uniq(links);
return obj;
}),
(sourceObj, targetObj) => {
_.merge(sourceObj, targetObj, { path: normalizeSafe('/' + contentPath) });
res.send(200, sourceObj);
}
)
return next();
};
const pagesHandler = (req, res, next) => {
log(`Handling pages`);
const contentFolder = path.resolve(mainFolder, CONTENTFOLDER);
const normalizedFolder = normalizeSafe(contentFolder);
getFiles(contentFolder, '.md')
.then(filesPaths =>
filesPaths
.map(f => normalizeSafe(f))
.map(filePath => {
const parsed = path.parse(filePath);
const isIndex = parsed.name === 'index';
const normalized = filePath
.replace(normalizedFolder, '')
.replace('.md', '');
return isIndex ?
normalized
.replace('/index.md', '/')
.replace('/index', '/')
: normalized;
})
.map(p => normalizeSafe(p)))
.then(files => {
res.send(200, files.filter(p =>
p !== '/' && p !== '/search/' &&
p !== '/index' && p !== '/search/index'
));
})
.catch(e => {
res.send(501, e);
});
return next();
};
const snippetsHandler = (req, res, next) => {
log(`Handling snippets`);
const snippetsFolder = path.resolve(mainFolder, SNIPPETSFOLDER);
const normalized = normalizeSafe(snippetsFolder);
getFiles(snippetsFolder, '.md')
.then(filePaths => Promise.all(filePaths.map(filePath => readFile(filePath).then(contents => {
const newPath = normalizeSafe(filePath).replace(normalized, '');
return {
path: newPath,
content: contents.toString()
};
}))))
.then(files => {
res.send(200, files);
})
.catch(e => {
res.send(501, e);
})
return next();
};
const spacesHandler = (req, res, next) => {
log(`Handling spaces`);
const spaceArr = [];
Object.keys(SPACES).forEach((spaceID) => {
const obj = SPACES[spaceID];
obj.id = spaceID;
spaceArr.push(obj);
})
res.send(200, spaceArr);
return next();
};
const spawn = (folder) => {
mainFolder = path.resolve(folder);
SPACES = YAML.load(path.resolve(mainFolder, 'data/spaces.yml'));
server = restify.createServer();
server.get(/^\/content\/(.*)/, contentHandler);
server.get(/^\/pages/, pagesHandler);
server.get(/^\/snippets/, snippetsHandler);
server.get(/^\/spaces/, spacesHandler)
server.listen(7000, () => {
log(`Server listening on ${server.url}`);
});
}
module.exports = {
spawn
};