docsify是轻量级的文档构建工具,无需生成html就能在网页展示markdown
我想着,将所有的markdown弄到一起,并用docsify 生成网站,用github pages 预览
但是找了半天好像没有自动生成目录树(并附带链接)的插件,找半天找到了gitee上面有人自己写的Docsify 笔记博客 : 自动生成sidebar 树状目录
然后我跑了之后不符合我的要求,在上面改…但是写完之后才发现,issues里面有人讲了改法,尴尬…
下面是我改的
var sidebarTxt = '- [**书不尽意**](/README.md)\n';
var path = require('path');
var curPath = path.resolve('./');
var baseDirArr = [];
function walkSync(currentDirPath, callback) {
var fs = require('fs'),
path = require('path');
fs.readdirSync(currentDirPath).forEach(function (name) {
var filePath = path.join(currentDirPath, name);
var stat = fs.statSync(filePath);
if (stat.isFile()) {
callback(filePath, stat);
} else if (stat.isDirectory() && !filePath.includes(".git")) {
walkSync(filePath, callback);
}
});
}
walkSync(curPath, function (filePath, stat) {
if (".md" == path.extname(filePath).toLowerCase()
&& "_" != path.basename(filePath).substr(0, 1)
&& path.basename(filePath).includes(`.md`)) {
var relativeFilePath = filePath.substr(curPath.length + 1);
if (relativeFilePath == path.basename(filePath)) {
return;
}
var relativeFilePathArr = relativeFilePath.split('\\')
for (var i = 0; i < relativeFilePathArr.length; i++) {
if (baseDirArr[i] == relativeFilePathArr[i]) {
continue;
}
baseDirArr[i] = relativeFilePathArr[i]
for (var j = 0; j < i; j++) {
sidebarTxt += ' '
}
if (i != relativeFilePathArr.length - 1) {
sidebarTxt += '- **' + relativeFilePathArr[i] + '**\n';
}
if (i == relativeFilePathArr.length - 1) {
sidebarTxt += '- [' + path.basename(relativeFilePathArr[i],".md") + '](/' + relativeFilePath + ')\n';
}
}
}
});
var path = require('path');
var fs = require('fs');
console.log(sidebarTxt);
fs.writeFile(path.resolve('./') + '/_sidebar.md', sidebarTxt, function (err) {
if (err) {
console.error(err);
}
});
|