zoukankan      html  css  js  c++  java
  • nodejs编写docsify自动遍历当前目录下md文档生成_sidebar.md

     1. 在docsify文档目录(比如:docs)下,创建genSidebar.js,拷贝下面的代码到genSidebar.js中保存;

    var sidebarTxt='* [首页](/)
    ';
    var path = require('path');
    var curPath = path.resolve('./');
    
    function walkSync(currentDirPath, prefixBlank, 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() && ".git"!=path.basename(filePath) && '_' != path.basename(filePath).substr(0,1)) {
                sidebarTxt += prefixBlank +'* ' +path.basename(filePath)+'
    ';
                walkSync(filePath, prefixBlank+'  ', callback);
            }
        });
    }
    walkSync(curPath,'', function(filePath, stat) {
            if(".md" == path.extname(filePath).toLowerCase() 
                && '_' != path.basename(filePath).substr(0,1) 
                && 'README.md' != path.basename(filePath)){
                var relativeFilePath = filePath.substr(curPath.length);
                //console.log("file:"+ path.basename(filePath).slice(1));
                var itemText = relativeFilePath.substr(1, relativeFilePath.length-4);
                while(itemText.indexOf('/')>0){
                    itemText = itemText.substr(itemText.indexOf('/')+1);
                    sidebarTxt += '  ';
                }
    
            sidebarTxt += '- ['+itemText+']('+relativeFilePath+')
    ';
                
            }
            //console.log("file:"+ +path.extname(filePath));
    });
    
    var path = require('path');
    var fs = require('fs');
    fs.copyFile(path.resolve('./')+"/_sidebar.md",path.resolve('./')+"/_sidebar.md.bak",function(err){
            if(err) throw new Error('something wrong was happended') });
    //console.log(path.resolve('./')+"/_sidebar.md");
    
    console.log(sidebarTxt);
    fs.writeFile(path.resolve('./')+'/_sidebar.md', sidebarTxt,function(err){
        if(err){
            console.error(err);
        }
    });

    2. 执行下面的node命令即可生成:_sidebar.md

    node genSidebar.js
  • 相关阅读:
    [剑指Offer] 10.矩形覆盖
    [剑指Offer] 9.变态跳台阶
    [剑指Offer] 8.跳台阶
    [剑指Offer] 7.斐波那契数列
    ArtifactTransferException: Failure to transfer org.apache.openejb:javaee-api:jar:5.0-1
    -Dmaven.multiModuleProjectDirectory system property is not set. Check $M2_HO 解决办法
    java中判断list是否为空的用法
    PL/SQL快速选中一行并执行
    substring的用法
    Oracle---------sql 中取值两列中值最大的一列
  • 原文地址:https://www.cnblogs.com/ziwuxian/p/14391264.html
Copyright © 2011-2022 走看看