forked from tldr-pages/tldr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build-index.js
50 lines (42 loc) · 997 Bytes
/
build-index.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
'use strict';
var glob = require('glob');
function parsePlatform(pagefile) {
return pagefile.split(/\//)[1];
}
function parsePagename(pagefile) {
return pagefile.split(/\//)[2].replace(/\.md$/, '');
}
function buildShortPagesIndex(files) {
var reducer = function (index, file) {
var os = parsePlatform(file);
var page = parsePagename(file);
if (index[page]) {
index[page].push(os);
} else {
index[page] = [os];
}
return index;
};
return files.reduce(reducer, {});
}
function buildPagesIndex(shortIndex) {
return Object.keys(shortIndex)
.sort()
.map(function (page) {
return {
name: page,
platform: shortIndex[page]
};
});
}
function saveIndex(index) {
var indexFile = {
commands: index
};
console.log(JSON.stringify(indexFile));
}
glob('pages/**/*.md', function (er, files) {
var shortIndex = buildShortPagesIndex(files);
var index = buildPagesIndex(shortIndex);
saveIndex(index);
});