当前位置: 首页 > 后端技术 > Node.js

小工具:Chrome书签文件到Markdown文件

时间:2023-04-03 23:24:00 Node.js

需求:闲来无事,想把自己收藏的一些书签按目录转成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(index6?6:count).fill('#')returnarr.join('')+''}lethandle=function(obj,count){if(obj['dir']){fileContent+=getSize(count)+obj['dir']+'\n\n'}//判断list是否为空if(obj.list&&放大器;obj.list.length){for(let[i,item]ofobj.list.entries()){letcreateTime=item.create_time?item.create_time+'':''fileContent+=createTime+'['+item.text+']('+item.href+')'+'\n\n'}}//判断是否存在于子节点if(obj.child&&Object.keys(obj.child).length){for(letkeyofObject.keys(obj.child)){handle(obj.child[key],count+1)}}}句柄(childFileObj[key],count)fileContentArr.push(fileContent)//创建md文件writeFile(fileName,fileContent,config.mdFilePath)}})//处理timeNow一层if(fileObj[timeNow].list&&fileObj[timeNow].list.length){letobj=fileObj[timeNow]letfileContent=''for(let[i,item]ofobj.list.entries()){让createTime=item.create_time?item.create_time+'':''fileContent+=createTime+'['+item.text+']('+item.href+')'+'\n\n'}fileContentArr.push(fileContent)}//生成README.mdwriteFile('README.md',fileContentArr.join(''),config.mdFilePath)},3000)预览前端书签整理:F2E-教程-收集用法1.clonegitclonehttps://github.com/OXOYO/bookmark2mdcdbookmark2mdnpmi2.导出Chrome书签并替换bookmark2md/bookmarks.html文件3.npmrunstart