forked from alibaba/lowcode-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgetDocsFromDir.js
More file actions
66 lines (58 loc) · 1.65 KB
/
getDocsFromDir.js
File metadata and controls
66 lines (58 loc) · 1.65 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
const fs = require('fs');
const path = require('path');
const glob = require('glob');
const matter = require('gray-matter');
module.exports = function getDocsFromDir(dir, cateList) {
// docs/
const baseDir = path.join(__dirname, '../docs/');
const docsDir = path.join(baseDir, dir);
function isNil(value) {
return value === undefined || value === null;
}
function getMarkdownOrder(filepath) {
const { data } = matter(fs.readFileSync(filepath, 'utf-8'));
const { sidebar_position } = data || {};
return isNil(sidebar_position) ? 100 : sidebar_position;
}
const docs = glob.sync('*.md?(x)', {
cwd: docsDir,
// ignore: 'README.md',
});
const result = docs
.filter((doc) => !/^index.md(x)?$/.test(doc))
.map((doc) => {
return path.join(docsDir, doc);
})
.sort((a, b) => {
const orderA = getMarkdownOrder(a);
const orderB = getMarkdownOrder(b);
return orderA - orderB;
})
.map((filepath) => {
// /Users/xxx/site/docs/guide/basic/router.md => guide/basic/router
const id = path
.relative(baseDir, filepath)
.replace(/\\/g, '/')
.replace(/\.mdx?/, '');
return id;
});
(cateList || []).forEach((item) => {
const { dir, subCategory, ...otherConfig } = item;
const indexList = glob.sync('index.md?(x)', {
cwd: path.join(baseDir, dir),
});
if (indexList.length > 0) {
otherConfig.link = {
type: 'doc',
id: `${dir}/index`,
};
}
result.push({
type: 'category',
collapsed: false,
...otherConfig,
items: getDocsFromDir(dir, subCategory),
});
});
return result;
};