From 802bec24b5cb5e83b8cffc4ef9cbb76d19d38502 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=B1=E8=BF=9B=E7=A6=84?= Date: Tue, 8 Mar 2022 13:53:54 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8D=87=E7=BA=A7hexo=E7=9B=B8=E5=85=B3?= =?UTF-8?q?=E4=BE=9D=E8=B5=96=E5=8C=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- _config.yml | 3 +- deploy_utils/image_synchronize.js | 136 ----- deploy_utils/list_images.js | 52 -- gulpfile.js | 89 --- package.json | 25 +- scripts/filter.js | 11 - yarn.lock | 959 ++++++++++++++++++++---------- 7 files changed, 652 insertions(+), 623 deletions(-) delete mode 100644 deploy_utils/image_synchronize.js delete mode 100644 deploy_utils/list_images.js delete mode 100644 gulpfile.js delete mode 100644 scripts/filter.js diff --git a/_config.yml b/_config.yml index 8f27c17..97fb113 100644 --- a/_config.yml +++ b/_config.yml @@ -36,7 +36,8 @@ skip_render: new_post_name: :title.md # File name of new posts default_layout: post titlecase: false # Transform title into titlecase -external_link: true # Open external links in new tab +external_link: + enable: true # Open external links in new tab filename_case: 0 render_drafts: false post_asset_folder: false diff --git a/deploy_utils/image_synchronize.js b/deploy_utils/image_synchronize.js deleted file mode 100644 index 0f35951..0000000 --- a/deploy_utils/image_synchronize.js +++ /dev/null @@ -1,136 +0,0 @@ -const fs = require('fs'), - path = require('path'), - nos = require('@xgheaven/nos-node-sdk') - -class ImageSynchronizer { - /** - * 构造方法 - * @param {Object} setting NosClient的设置项 - * @param {Array} imagesList 本地图片的列表 - * @param {String} rootPath 本地文件根路径 - */ - constructor(setting, imagesList, rootPath) { - // 网易云对象存储调用接口client - this.client = new nos.NosClient(setting) - this.imagesList = imagesList - this.rootPath = rootPath - } - /** - * 执行文件同步 - * @param {String} prefix 图片目录前缀 - */ - synchronize(prefix) { - return this._queryObjects({limit: this.imagesList.length+1, prefix}, function(pendingUploadFiles){ - this._uploadObject(pendingUploadFiles) - }, function(pendingDeleteFiles){ - this._deleteObjects(pendingDeleteFiles) - }) - } - /** - * 查询所有对象存储库已存在的文件 - * @param {Object} params 查询的参数 - * @param {Function} uploadCallback 处理待上传文件的回调函数 - * @param {Function} deleteCallback 处理待删除文件的回调函数 - */ - async _queryObjects(params, uploadCallback, deleteCallback) { - // 列出所有已存储的对象 - const ret = await this.client.listObject(params) - // ret 包括 items(元素),limit(请求的数量),nextMarker(下一个标记) - let storageItems = ret.items.filter(item => { - return /^images.+?\.(png|jpe?g|gif)$/.test(item.key) - }).sort((item1, item2) => { - if (item1.key > item2.key) { - return 1 - } - else if (item1.key < item2.key) { - return -1 - } - return 0 - }); - // 待上传的文件列表 - let pendingUploadFiles = this.imagesList.filter(item => { - let index = this._binarySearch(storageItems, item.name, 'key', 0, storageItems.length - 1) - if (index === -1) { - // 文件名不存在, 代表是新文件 - item.type = 'new' - return true - } - else if (storageItems[index].eTag !== item.md5) { - // 文件名存在, 但是hash值不同, 代表有变化 - item.type = 'change' - return true - } - return false - }); - // 处理待上传的文件 - uploadCallback.call(this, pendingUploadFiles); - // 待删除的文件列表( 仓库中存在, 本地不存在 ) - let pendingDeleteFiles = storageItems.filter(item => { - return this._binarySearch(this.imagesList, item.key, 'name', 0, this.imagesList.length - 1) === -1; - }) - // 处理待删除的文件 - deleteCallback.call(this, pendingDeleteFiles.map(item => item.key)) - } - /** - * 上传文件对象 - * @param {Array} filesList 待上传的文件列表 - * @param {Number} index 索引值 - */ - _uploadObject(filesList, index=0) { - if(index >= filesList.length) return - - this.client.putObject({ - objectKey: filesList[index].name, - body: fs.createReadStream(path.resolve(this.rootPath, filesList[index].name)), // 支持 Buffer/Readable/string - }).then(result => { - // eTag是上传后远端校验的md5值, 用于和本地进行比对 - let eTag = result.eTag.replace(/"/g,'') - if(filesList[index].md5 === eTag) { - console.log(`${filesList[index].name} 上传成功, md5:${eTag} 类型: ${filesList[index].type}`) - } else { - console.warn(`${filesList[index].name} 上传出错, md5值不一致`) - console.warn(`===> 本地文件: ${filesList[index].md5}, 接口返回: ${eTag}`) - } - this._uploadObject(filesList, ++index) - }) - } - /** - * 批量删除文件 - * @param {Array} fileNamesList 文件名数组 - */ - _deleteObjects(fileNamesList) { - if(!Array.isArray(fileNamesList) || !fileNamesList.length) return - - this.client.deleteMultiObject({ - objectKeys: fileNamesList - }).then(err => { - console.log('===> 文件删除成功') - fileNamesList.forEach(item => console.log(item)) - }) - } - /** - * 二分法查找 - * @param {Array} arr 执行查找的数组 - * @param {Object} target 要找到的目标元素 - * @param {String} key 数组元素上的键 - * @param {Number} start 查找的范围 起点 - * @param {Number} end 查找的范围 终点 - */ - _binarySearch(arr, target, key, start, end) { - if(!Array.isArray(arr) || !arr.length) { - return -1 - } - if(start >= end) { - return arr[start][key] === target ? start : -1 - } - let index = Math.ceil((start + end)/2) - if(arr[index][key] === target) { - return index - } else if(arr[index][key] > target) { - return this._binarySearch(arr, target, key, start, index-1) - } else { - return this._binarySearch(arr, target, key, index+1, end) - } - } -} -module.exports = ImageSynchronizer \ No newline at end of file diff --git a/deploy_utils/list_images.js b/deploy_utils/list_images.js deleted file mode 100644 index 8fc2689..0000000 --- a/deploy_utils/list_images.js +++ /dev/null @@ -1,52 +0,0 @@ -const fs = require('fs') -const path = require('path') -const crypto = require('crypto') - - -function sortName(item1, item2) { - if(item1.name > item2.name) { - return 1 - } else if(item1.name < item2.name) { - return -1 - } - return 0 -} - -/** - * 递归遍历目录中的所有文件 - * @param {String} imageFolderPath 文件夹路径 - * @param {Array} images 图片列表 - * @param {String} rootPath 根路径 - */ -function readDirSync(imageFolderPath, images, rootPath, callback, count={fileCount:0, finishCount:0}){ - var files = fs.readdirSync(imageFolderPath); - files.forEach(item => { - var fileInfo = fs.statSync(`${imageFolderPath}/${item}`) - if(fileInfo.isDirectory()){ - // 该文件是一个目录, 则遍历该目录内容 - readDirSync(`${imageFolderPath}/${item}`, images, rootPath, callback, count) - } else { - count.fileCount ++ - var stream = fs.createReadStream(`${imageFolderPath}/${item}`) - var fsHash = crypto.createHash('md5') - - stream.on('data', data => { - fsHash.update(data) - }) - stream.on('end', () => { - count.finishCount ++ - images.push({ - name: `${imageFolderPath}/${item}`.replace(rootPath, ''), - md5: fsHash.digest('hex') - }) - if(count.fileCount === count.finishCount && typeof callback === 'function') { - callback(images.sort(sortName)) - } - }) - } - }) -} - -module.exports = function (rootPath, imageFloder, callback) { - readDirSync(path.resolve(rootPath, imageFloder).replace(/\\/g,'/'), [], rootPath, callback) -} diff --git a/gulpfile.js b/gulpfile.js deleted file mode 100644 index f9f5ab1..0000000 --- a/gulpfile.js +++ /dev/null @@ -1,89 +0,0 @@ -const gulp = require('gulp'), - htmlmin = require('gulp-htmlmin'), // html压缩组件 - htmlclean = require('gulp-htmlclean'), // html清理组件 - plumber = require('gulp-plumber'), // 容错组件(发生错误不跳出任务,并报出错误内容) - Hexo = require('hexo'), - log = require('fancy-log') // gulp的日志输出 - -// 程序执行的传参 -const argv = require('optimist') - // .demand(['accessKey', 'accessSecret', 'deployPath']) - .describe('accessKey', '网易云对象存储key') - .describe('accessSecret', '网易云对象存储secret') - .describe('deployPath', '静态化后发布的目录') - .argv - -const hexo = new Hexo(process.cwd(), {}) - -// 创建静态页面 (等同 hexo generate) -gulp.task('generate', async function() { - try { - await hexo.init() - await hexo.call('clean') - await hexo.call('generate', { watch: false }) - return hexo.exit() - } catch (err) { - return hexo.exit(err) - } -}) - -// 压缩public目录下的html文件 -gulp.task('compressHtml', () => { - const cleanOptions = { - protect: /<\!--%fooTemplate\b.*?%-->/g, //忽略处理 - unprotect: /