Skip to content

Commit

Permalink
1.remove useless snape;
Browse files Browse the repository at this point in the history
2.optimize the code style.
  • Loading branch information
Doradx committed Sep 5, 2023
1 parent e789f17 commit 09c2951
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 102 deletions.
97 changes: 45 additions & 52 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');


Expand Down Expand Up @@ -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');


Expand Down Expand Up @@ -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;
}
}

// 检查图片是否存在
Expand All @@ -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,", "");
Expand Down Expand Up @@ -282323,7 +282323,7 @@ function getImageSize(buffer) {


module.exports = {
migrateNotionImageFromURL
migrateNotionImageFromURL
}

/***/ }),
Expand Down Expand Up @@ -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;
Expand All @@ -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 });
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
78 changes: 39 additions & 39 deletions src/migrateNotionImage.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

// 检查图片是否存在
Expand All @@ -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,", "");
Expand Down Expand Up @@ -156,5 +156,5 @@ function getImageSize(buffer) {


module.exports = {
migrateNotionImageFromURL
migrateNotionImageFromURL
}
15 changes: 4 additions & 11 deletions src/notion.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 });
Expand Down

0 comments on commit 09c2951

Please sign in to comment.