-
-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathdocs.js
67 lines (49 loc) · 1.4 KB
/
docs.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
import { promisify } from 'util';
import { readFile } from 'fs/promises';
import path from 'path';
import g from 'glob';
const glob = promisify(g);
const globOptions = {};
async function getDocumentList(version) {
const versionedFiles = await glob(`docs/${version}/**/*.mdx`, globOptions);
const commonFiles = await glob('docs/*.mdx', globOptions);
const files = versionedFiles.map(file => {
let slug = path.basename(file, '.mdx');
if(slug === 'index') {
slug = '';
}
return {
slug,
version: path.dirname(file).replace(/docs\//, ''),
path: file
};
});
commonFiles.forEach(file => {
const slug = path.basename(file, '.mdx');
const isVersioned = files.find(vf => vf.slug === slug);
if(!isVersioned) {
files.push({
slug,
version: null,
path: file
});
}
});
return files;
}
async function getDocumentContent(version, slug) {
const files = await getDocumentList(version);
const file = files.find(vf => vf.slug === slug);
if(!file) throw new Error('Unable to locate the document requested');
const content = await readFile(path.join(process.cwd(), file.path), { encoding: 'utf-8' });
return content;
}
async function getNavigation(version) {
const content = (await import(`@/docs/${version}/_nav.js`)).default;
return content;
}
export {
getDocumentList,
getDocumentContent,
getNavigation
};