2022-02-22 18:35:06 +08:00
|
|
|
const fs = require('fs');
|
|
|
|
const path = require('path');
|
|
|
|
const glob = require('glob');
|
|
|
|
const matter = require('gray-matter');
|
|
|
|
|
|
|
|
module.exports = function getDocsFromDir(dir) {
|
|
|
|
// docs/
|
|
|
|
const baseDir = path.join(__dirname, '../docs/');
|
|
|
|
const docsDir = path.join(baseDir, dir);
|
|
|
|
|
|
|
|
function getMarkdownOrder(filepath) {
|
|
|
|
return (matter(fs.readFileSync(filepath, 'utf-8')).data || {}).order || 100;
|
|
|
|
}
|
|
|
|
|
2022-07-01 16:47:25 +08:00
|
|
|
function isDocHide(filepath) {
|
|
|
|
return (matter(fs.readFileSync(filepath, 'utf-8')).data || {}).hide || false;
|
|
|
|
}
|
|
|
|
|
2022-02-22 18:35:06 +08:00
|
|
|
const docs = glob.sync('*.md', {
|
|
|
|
cwd: docsDir,
|
|
|
|
// ignore: 'README.md',
|
|
|
|
});
|
|
|
|
|
|
|
|
const result = docs
|
2022-07-01 16:47:25 +08:00
|
|
|
.map((doc) => path.join(docsDir, doc))
|
|
|
|
.filter((doc) => !isDocHide(doc))
|
2022-02-22 18:35:06 +08:00
|
|
|
.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(/\.md/, '');
|
|
|
|
return id;
|
|
|
|
});
|
|
|
|
|
|
|
|
return result;
|
2022-02-23 11:24:50 +08:00
|
|
|
};
|