diff --git a/src/main/utils/common.ts b/src/main/utils/common.ts index 245f20e..0029050 100644 --- a/src/main/utils/common.ts +++ b/src/main/utils/common.ts @@ -1,7 +1,7 @@ import db from '#/datastore' import { clipboard } from 'electron' -export function handleCopyUrl (str: string): void { +export const handleCopyUrl = (str: string): void => { if (db.get('settings.autoCopy') !== false) { clipboard.writeText(str) } diff --git a/src/universal/utils/common.ts b/src/universal/utils/common.ts index 36762c4..896b565 100644 --- a/src/universal/utils/common.ts +++ b/src/universal/utils/common.ts @@ -1 +1,17 @@ export const isUrl = (url: string): boolean => (url.startsWith('http://') || url.startsWith('https://')) +export const isUrlEncode = (url: string): boolean => { + url = url || '' + try { + return url !== decodeURI(url) + } catch (e) { + // if some error caught, try to let it go + return true + } +} + +export const handleUrlEncode = (url: string): string => { + if (!isUrlEncode(url)) { + url = encodeURI(url) + } + return url +} diff --git a/src/universal/utils/pasteTemplate.ts b/src/universal/utils/pasteTemplate.ts index 72c09fb..79bfe28 100644 --- a/src/universal/utils/pasteTemplate.ts +++ b/src/universal/utils/pasteTemplate.ts @@ -1,9 +1,10 @@ import db from '#/datastore' import { IPasteStyle } from '#/types/enum' +import { handleUrlEncode } from './common' const formatCustomLink = (customLink: string, item: ImgInfo) => { let fileName = item.fileName!.replace(new RegExp(`\\${item.extname}$`), '') - let url = item.url || item.imgUrl + const url = handleUrlEncode(item.url || item.imgUrl) const formatObj = { url, fileName @@ -19,7 +20,7 @@ const formatCustomLink = (customLink: string, item: ImgInfo) => { } export default (style: IPasteStyle, item: ImgInfo) => { - let url = item.url || item.imgUrl + const url = handleUrlEncode(item.url || item.imgUrl) const customLink = db.get('settings.customLink') || '$url' const tpl = { 'markdown': `![](${url})`,