From 09c29510c7e92c9e4f94558b98495182608d1986 Mon Sep 17 00:00:00 2001 From: Dorad Date: Tue, 5 Sep 2023 17:25:40 +0800 Subject: [PATCH] 1.remove useless snape; 2.optimize the code style. --- dist/index.js | 97 ++++++++---------- dist/{vendor2 => vendor1}/cjpeg | Bin .../source/mozjpeg.tar.gz | Bin dist/{vendor1 => vendor2}/pngquant | Bin .../source/pngquant.tar.gz | Bin src/migrateNotionImage.js | 78 +++++++------- src/notion.js | 15 +-- 7 files changed, 88 insertions(+), 102 deletions(-) rename dist/{vendor2 => vendor1}/cjpeg (100%) mode change 100755 => 100644 rename dist/{vendor2 => vendor1}/source/mozjpeg.tar.gz (100%) rename dist/{vendor1 => vendor2}/pngquant (100%) mode change 100755 => 100644 rename dist/{vendor1 => vendor2}/source/pngquant.tar.gz (100%) diff --git a/dist/index.js b/dist/index.js index cfe72c3..cba9741 100644 --- a/dist/index.js +++ b/dist/index.js @@ -149851,7 +149851,7 @@ module.exports = new BinWrapper() .src(`${url}macos/cjpeg`, 'darwin') .src(`${url}linux/cjpeg`, 'linux') .src(`${url}win/cjpeg.exe`, 'win32') - .dest(__nccwpck_require__.ab + "vendor2") + .dest(__nccwpck_require__.ab + "vendor1") .use(process.platform === 'win32' ? 'cjpeg.exe' : 'cjpeg'); @@ -166015,7 +166015,7 @@ module.exports = new BinWrapper() .src(`${url}linux/x64/pngquant`, 'linux', 'x64') .src(`${url}freebsd/x64/pngquant`, 'freebsd', 'x64') .src(`${url}win/pngquant.exe`, 'win32') - .dest(__nccwpck_require__.ab + "vendor1") + .dest(__nccwpck_require__.ab + "vendor2") .use(process.platform === 'win32' ? 'pngquant.exe' : 'pngquant'); @@ -282189,47 +282189,47 @@ const imageminSvgo = __nccwpck_require__(14038); async function migrateNotionImageFromURL(ctx, url) { - // 检查图片是否为notion的图片 + // 检查图片是否为notion的图片 const urlReg = /^https:\/\/.*?amazonaws\.com\/.+\.(?:jpg|jpeg|png|gif|webp)\?.+/; - if (!urlReg.test(url)) { - console.log(`Image ${url} is not a notion image, skip`); - return url; + if (!urlReg.test(url)) { + console.log(`Image ${url} is not a notion image, skip`); + return url; + } + // 检查URL对应的图片是否已经存在 + const base_url = ctx.getConfig('pic-base-url') || null; + const uuidreg = /[a-fA-F0-9]{8}-(?:[a-fA-F0-9]{4}-){3}[a-fA-F0-9]{12}/g; + const uuid = url.match(uuidreg)?.pop(); + const ext = url.split('?')[0].split('.').pop()?.toLowerCase(); + const picUrl = `${base_url}${uuid}.${ext}`; + if (base_url) { + // get pic uuid from the url using regex + if (await checkPicExist(ctx, picUrl)) { + // console.log(`Image ${picUrl} already exists, skip`) + return picUrl; } - // 检查URL对应的图片是否已经存在 - const base_url = ctx.getConfig('pic-base-url') || null; - const uuidreg = /[a-fA-F0-9]{8}-(?:[a-fA-F0-9]{4}-){3}[a-fA-F0-9]{12}/g; - const uuid = url.match(uuidreg)?.pop(); - const ext = url.split('?')[0].split('.').pop()?.toLowerCase(); - const picUrl = `${base_url}${uuid}.${ext}`; - if (base_url) { - // get pic uuid from the url using regex - if (await checkPicExist(ctx, picUrl)) { - // console.log(`Image ${picUrl} already exists, skip`) - return picUrl; - } + } + // 不存在则上传图片 + try { + // 从URL获取图片信息 + let imageItem = await handlePicFromURL(ctx, url); + // 检查是否需要压缩图片 + if (ctx.getConfig('compress')) { + // 压缩图片 + imageItem = await compressPic(imageItem); } - // 不存在则上传图片 - try { - // 从URL获取图片信息 - let imageItem = await handlePicFromURL(ctx, url); - // 检查是否需要压缩图片 - if (ctx.getConfig('compress')) { - // 压缩图片 - imageItem = await compressPic(imageItem); - } - imageItem.fileName = `${uuid}${imageItem.extname}`; - // 上传图片 - const result = await ctx.upload([imageItem]); - if (result && result[0] && result[0].imgUrl) { - ctx.log.info(`Upload image ${result[0].imgUrl} success`); - return result[0].imgUrl; - } - ctx.log.error(`Upload image ${url} fail`); - return undefined; - } catch (e) { - ctx.log.error(`Upload image ${url} fail: ${e}`); - return undefined; + imageItem.fileName = `${uuid}${ext}`; + // 上传图片 + const result = await ctx.upload([imageItem]); + if (result && result[0] && result[0].imgUrl) { + ctx.log.info(`Upload image ${result[0].imgUrl} success`); + return result[0].imgUrl; } + ctx.log.error(`Upload image ${url} fail`); + return undefined; + } catch (e) { + ctx.log.error(`Upload image ${url} fail: ${e}`); + return undefined; + } } // 检查图片是否存在 @@ -282248,7 +282248,7 @@ async function checkPicExist(ctx, picUrl) { } // 从URL获取图片信息 -async function handlePicFromURL(ctx, url){ +async function handlePicFromURL(ctx, url) { try { if (url.includes("data:image/svg+xml")) { let data = url.replace("data:image/svg+xml;utf8,", ""); @@ -282323,7 +282323,7 @@ function getImageSize(buffer) { module.exports = { - migrateNotionImageFromURL + migrateNotionImageFromURL } /***/ }), @@ -282394,17 +282394,7 @@ function init(conf) { let picgo_config = { "picBed": config.picBed, - "picgo-plugin-pic-migrater": { - // only include notion image - include: `^(https://.*?amazonaws\.com\/.+\.(?:jpg|jpeg|png|gif|webp)\?.+)`, - exclude: `^(?=.*${domain.replace('.', '\.')}).*|.*\.ico$`, // exclude the domain and icon - }, - "pic-base-url": config.pic_base_url || null, - "settings.logLevel": [ - 'success', - 'error', - 'warn' - ] + "pic-base-url": config.pic_base_url || null } picgo_config["compress"] = config.pic_compress ? true : false; @@ -282413,6 +282403,9 @@ function init(conf) { picgo.setConfig({ 'picBed.transformer': 'base64' }); + picgo.setConfig({ + 'settings.logLevel': ['success', 'error'] + }) // passing notion client to the option n2m = new NotionToMarkdown({ notionClient: notion }); diff --git a/dist/vendor2/cjpeg b/dist/vendor1/cjpeg old mode 100755 new mode 100644 similarity index 100% rename from dist/vendor2/cjpeg rename to dist/vendor1/cjpeg diff --git a/dist/vendor2/source/mozjpeg.tar.gz b/dist/vendor1/source/mozjpeg.tar.gz similarity index 100% rename from dist/vendor2/source/mozjpeg.tar.gz rename to dist/vendor1/source/mozjpeg.tar.gz diff --git a/dist/vendor1/pngquant b/dist/vendor2/pngquant old mode 100755 new mode 100644 similarity index 100% rename from dist/vendor1/pngquant rename to dist/vendor2/pngquant diff --git a/dist/vendor1/source/pngquant.tar.gz b/dist/vendor2/source/pngquant.tar.gz similarity index 100% rename from dist/vendor1/source/pngquant.tar.gz rename to dist/vendor2/source/pngquant.tar.gz diff --git a/src/migrateNotionImage.js b/src/migrateNotionImage.js index 4cd8774..3338c0e 100644 --- a/src/migrateNotionImage.js +++ b/src/migrateNotionImage.js @@ -22,47 +22,47 @@ const imageminSvgo = require("imagemin-svgo"); async function migrateNotionImageFromURL(ctx, url) { - // 检查图片是否为notion的图片 + // 检查图片是否为notion的图片 const urlReg = /^https:\/\/.*?amazonaws\.com\/.+\.(?:jpg|jpeg|png|gif|webp)\?.+/; - if (!urlReg.test(url)) { - console.log(`Image ${url} is not a notion image, skip`); - return url; + if (!urlReg.test(url)) { + console.log(`Image ${url} is not a notion image, skip`); + return url; + } + // 检查URL对应的图片是否已经存在 + const base_url = ctx.getConfig('pic-base-url') || null; + const uuidreg = /[a-fA-F0-9]{8}-(?:[a-fA-F0-9]{4}-){3}[a-fA-F0-9]{12}/g; + const uuid = url.match(uuidreg)?.pop(); + const ext = url.split('?')[0].split('.').pop()?.toLowerCase(); + const picUrl = `${base_url}${uuid}.${ext}`; + if (base_url) { + // get pic uuid from the url using regex + if (await checkPicExist(ctx, picUrl)) { + // console.log(`Image ${picUrl} already exists, skip`) + return picUrl; } - // 检查URL对应的图片是否已经存在 - const base_url = ctx.getConfig('pic-base-url') || null; - const uuidreg = /[a-fA-F0-9]{8}-(?:[a-fA-F0-9]{4}-){3}[a-fA-F0-9]{12}/g; - const uuid = url.match(uuidreg)?.pop(); - const ext = url.split('?')[0].split('.').pop()?.toLowerCase(); - const picUrl = `${base_url}${uuid}.${ext}`; - if (base_url) { - // get pic uuid from the url using regex - if (await checkPicExist(ctx, picUrl)) { - // console.log(`Image ${picUrl} already exists, skip`) - return picUrl; - } + } + // 不存在则上传图片 + try { + // 从URL获取图片信息 + let imageItem = await handlePicFromURL(ctx, url); + // 检查是否需要压缩图片 + if (ctx.getConfig('compress')) { + // 压缩图片 + imageItem = await compressPic(imageItem); } - // 不存在则上传图片 - try { - // 从URL获取图片信息 - let imageItem = await handlePicFromURL(ctx, url); - // 检查是否需要压缩图片 - if (ctx.getConfig('compress')) { - // 压缩图片 - imageItem = await compressPic(imageItem); - } - imageItem.fileName = `${uuid}${imageItem.extname}`; - // 上传图片 - const result = await ctx.upload([imageItem]); - if (result && result[0] && result[0].imgUrl) { - ctx.log.info(`Upload image ${result[0].imgUrl} success`); - return result[0].imgUrl; - } - ctx.log.error(`Upload image ${url} fail`); - return undefined; - } catch (e) { - ctx.log.error(`Upload image ${url} fail: ${e}`); - return undefined; + imageItem.fileName = `${uuid}${ext}`; + // 上传图片 + const result = await ctx.upload([imageItem]); + if (result && result[0] && result[0].imgUrl) { + ctx.log.info(`Upload image ${result[0].imgUrl} success`); + return result[0].imgUrl; } + ctx.log.error(`Upload image ${url} fail`); + return undefined; + } catch (e) { + ctx.log.error(`Upload image ${url} fail: ${e}`); + return undefined; + } } // 检查图片是否存在 @@ -81,7 +81,7 @@ async function checkPicExist(ctx, picUrl) { } // 从URL获取图片信息 -async function handlePicFromURL(ctx, url){ +async function handlePicFromURL(ctx, url) { try { if (url.includes("data:image/svg+xml")) { let data = url.replace("data:image/svg+xml;utf8,", ""); @@ -156,5 +156,5 @@ function getImageSize(buffer) { module.exports = { - migrateNotionImageFromURL + migrateNotionImageFromURL } \ No newline at end of file diff --git a/src/notion.js b/src/notion.js index 4f47895..8a7bcf3 100644 --- a/src/notion.js +++ b/src/notion.js @@ -61,17 +61,7 @@ function init(conf) { let picgo_config = { "picBed": config.picBed, - "picgo-plugin-pic-migrater": { - // only include notion image - include: `^(https://.*?amazonaws\.com\/.+\.(?:jpg|jpeg|png|gif|webp)\?.+)`, - exclude: `^(?=.*${domain.replace('.', '\.')}).*|.*\.ico$`, // exclude the domain and icon - }, - "pic-base-url": config.pic_base_url || null, - "settings.logLevel": [ - 'success', - 'error', - 'warn' - ] + "pic-base-url": config.pic_base_url || null } picgo_config["compress"] = config.pic_compress ? true : false; @@ -80,6 +70,9 @@ function init(conf) { picgo.setConfig({ 'picBed.transformer': 'base64' }); + picgo.setConfig({ + 'settings.logLevel': ['success', 'error'] + }) // passing notion client to the option n2m = new NotionToMarkdown({ notionClient: notion });