1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
| #!/usr/bin/env node
console.log('开始检测更新时间和图片'); var fs = require("fs"); var file = "./txt"; var RegExp=/(updated:\s*)((\d{2}(([02468][048])|([13579][26]))[\-\/\s]?((((0?[13578])|(1[02]))[\-\/\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\-\/\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\-\/\s]?((0?[1-9])|([1-2][0-9])))))|(\d{2}(([02468][1235679])|([13579][01345789]))[\-\/\s]?((((0?[13578])|(1[02]))[\-\/\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\-\/\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\-\/\s]?((0?[1-9])|(1[0-9])|(2[0-8]))))))(\s((([0-1][0-9])|(2?[0-3]))\:([0-5]?[0-9])((\s)|(\:([0-5]?[0-9])))))/g; var imgReg=/\!\[[\s\S]*?\]\([\s\S]*?\)/g;
fs.readdir("./",function(err,files){ var len=files.length; var file=null; for(var i=0;i<len;i++){ file=files[i]; if(file.indexOf(".md")>-1){ writeFileTime(file,fs); } } });
function writeFileTime(file,fs){ fs.readFile(file, 'utf8',function(err, data) { if (err) return console.log("读取文件内容错误:",err); if(RegExp.test(data)){ fs.stat(file,function(err, stats) { if (err) return console.log("读取文件信息错误:",err); var updateds=data.match(RegExp); var updated=updateds[0].replace("updated: ","").replace(/-/g,"/"); if(new Date(stats.mtime).getTime()-new Date(Date.parse(updated))>1000*60*5){ console.log(file,"时间处理:"+updateds[0]); var result= data.replace(updateds[0],"updated: "+getFormatDate(stats.mtime)); fs.writeFile(file, result, 'utf8',function(err) { if (err) return console.log("写文件错误:",err); fs.utimes(file,new Date(stats.atime),new Date(stats.mtime),function(err){ if (err) return console.log("修改时间失败:",err); console.log("成功更新时间:"+updateds[0].replace(updateds[0],"updated: "+getFormatDate(stats.mtime))); }); }); } }); } if(imgReg.test(data)){ var imgpaths=data.match(imgReg); for (var i = imgpaths.length - 1; i >= 0; i--) { var imgpath=imgpaths[i]; if (imgpath.indexOf("xuanfong1.github.io\\source\\_posts\\image")!=-1 || imgpath.indexOf("xuanfong1.github.io/source/_posts/image")!=-1) { console.log(file,"处理:"+imgpath); var filenametemp= imgpath.split('\\').pop(); var filename=filenametemp.slice(0,filenametemp.length-1); var result=data.replace(imgpath,"![](http://ohdtoul5i.bkt.clouddn.com/"+filename+")"); fs.writeFile(file, result, 'utf8',function(err) { if (err) return console.log("写文件错误:",err); console.log("成功替换:"+imgpath.replace(imgpath,"![](http://ohdtoul5i.bkt.clouddn.com/"+filename+")")); }); } if (imgpath.indexOf(imgpath.indexOf("xuanfong1.github.io/source/_posts/image")!=-1) { console.log(file,"处理:"+imgpath); var filenametemp= imgpath.split('/').pop(); var filename=filenametemp.slice(0,filenametemp.length-1); var result=data.replace(imgpath,"![](http://ohdtoul5i.bkt.clouddn.com/"+filename+")"); fs.writeFile(file, result, 'utf8',function(err) { if (err) return console.log("写文件错误:",err); console.log("成功替换:"+imgpath.replace(imgpath,"![](http://ohdtoul5i.bkt.clouddn.com/"+filename+")")); }); } } } }); }
function getFormatDate(timeStr, dateSeparator, timeSeparator) { dateSeparator = dateSeparator ? dateSeparator : "-"; timeSeparator = timeSeparator ? timeSeparator : ":"; var date = new Date(timeStr), year = date.getFullYear(), month = date.getMonth(), day = date.getDate(), hour = date.getHours(), minute = date.getMinutes(), seconds = date.getSeconds(), Y = year + dateSeparator, M = ((month + 1) > 9 ? (month + 1) : ('0' + (month + 1))) + dateSeparator, D = (day > 9 ? day : ('0' + day)) + ' ', h = (hour > 9 ? hour : ('0' + hour)) + timeSeparator, m = (minute > 9 ? minute : ('0' + minute)) + timeSeparator, s = (seconds > 9 ? seconds : ('0' + seconds)), formatDate = Y + M + D + h + m + s; return formatDate; }
|