需求:闲来无事,想把自己收藏的一些书签按目录转成md文件。于是就有了下面这个小工具:bookmark2md首先上传源码://配置文件config.js/***OXOYO于2017/10/16创建。*/exports=module.exports={//书签文件bookmarksFile:'./bookmarks.html',//生成目录mdFilePath:'./md/',//排除目录unlessPath:['OXO','OYO']}//核心文件bookmark2md.js/***由OXOYO创建于2017/10/13。*/constfs=require('fs')constcheerio=require('cheerio')constconfig=require('./config')//console.log('config',config)consthtml=fs.readFileSync(config.bookmarksFile)const$=cheerio.load(html)//当前时间consttimeNow=(newDate()).getTime()//目录名对象constdirNameObj={}//分隔符constseparator='=>'//写入文件constwriteFile=function(fileName,content,filePath){filePath=filePath?filePath:'./md/'//写入文件fs.writeFile(filePath+fileName,content,function(err){if(err){returnconsole.error(err)}else{console.log(fileName+'created成功!')}})}//日期格式化constformatDate=(time,fmt='yyyy-MM-ddhh:mm')=>{//10位时间格式化lettimeStr=time+''if(timeStr.length<13){time=time*(Math.pow(10,13-timeStr.length))}time=parseInt(time)if(isNaN(time)){return''}letdate=newDate(time)letpadLeftZero=(str)=>{return('00'+str).substr(str.length)}letdoFormatDate=(date,fmt)=>{if(/(y+)/.test(fmt)){fmt=fmt.replace(RegExp.$1,(date.getFullYear()+'').substr(4-RegExp.$1.length))}letobj={'M+':date.getMonth()+1,'d+':date.getDate(),'h+':date.getHours(),'m+':date.getMinutes(),'s+':date.getSeconds()}for(letkinobj){if(newRegExp(`(${k})`).test(fmt)){letstr=obj[k]+''fmt=fmt.replace(RegExp.$1,RegExp.$1.length===1?str:padLeftZero(str))}}returnfmt}returndoFormatDate(date,fmt)}//遍历a标签$('a').each(function(i,el){letnode=$(el)letnodeText=node.text()letnodeHref=node.attr('href')letnodeCreateTime=formatDate(node.attr('add_date'))letnodeUpdateTime=formatDate(node.attr('last_modified'))//查父目录letparentDT=node.parents('dt')letparentDirPathArr=[]letparentDirPathStr=''parentDT.each(function(j,parent){letfirstChild=$(parent).children().first()if(firstChild.is('h3')){letdirName=firstChild.text()if(firstChild.attr('personal_toolbar_folder')){dirName=timeNow}parentDirPathArr.push(dirName)}})parentDirPathStr=parentDirPathArr.reverse()。join(separator)//console.log(parentDirPathStr)//初始化当前目录路径if(!dirNameObj[parentDirPathStr]){dirNameObj[parentDirPathStr]=[]}dirNameObj[parentDirPathStr].push({text:nodeText,href:nodeHref,create_time:nodeCreateTime,update_time:nodeUpdateTime})})setTimeout(function(){让fileObj={}Object.keys(dirNameObj).map(function(key){letflag=falsefor(letpathofconfig.unlessPath){if(key.toLowerCase().includes(path.toLowerCase())){flag=truebreak}}if(!flag){//console.log('key',key)letkeys=key.split(separator)//递归处理函数handle(index,elem,keys,len,obj){if(!obj[elem]){obj[elem]={dir:elem,child:{},list:[]}}if(index
