-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
62 lines (57 loc) · 1.67 KB
/
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
51
52
53
54
55
56
57
58
59
60
61
62
'use strict';
const glob = require('glob').sync;
const argv = require('yargs-parser')(process.argv.slice(2));
const { getFormattedDoc } = require('./build-helpers');
const { getDocumentsRoot, getDocumentsGlob } = require('./fs-helpers');
const createRedirects = require('./create-redirects');
const { readConfig } = require('./read-config');
const path = require('path');
const isServer = argv._.indexOf('serve') !== -1;
const isSearch = argv._.indexOf('build-search') !== -1;
const shouldCreateRedirects = argv._.indexOf('create-redirect') !== -1;
const { serve, build } = require('./server');
const { buildSearch } = require('./build-search');
const markdownExtension = '.md';
readConfig().then((configs) => {
if (isSearch) {
return buildSearch(configs);
} else {
return Promise.all(
configs.map(async (config) => {
await compile(config);
})
);
}
});
async function compile(config) {
const filePathsDontExist = [];
const documentsRoot = getDocumentsRoot(config);
const getKey = (path) =>
path.slice(documentsRoot.length + 1, -markdownExtension.length);
const getPath = (key) => documentsRoot + (key.startsWith('/')?'':'/') + key + markdownExtension;
const allDocs = {};
const getDoc = await getFormattedDoc({ allDocs, getPath, config, filePathsDontExist });
if (shouldCreateRedirects) {
createRedirects(config);
return;
}
if (isServer) {
serve({
config,
getDoc,
getPath,
getKey,
allDocs,
filePathsDontExist,
});
} else {
build({
config,
getDoc,
docs: glob(getDocumentsGlob(config, markdownExtension)),
getKey,
allDocs,
filePathsDontExist,
});
}
}