🐛 Fix: paste url encoding bug

ISSUES CLOSED: #454
This commit is contained in:
Molunerfinn 2020-06-28 15:30:58 +08:00
parent a693544291
commit 59d3eba86d
3 changed files with 20 additions and 3 deletions

View File

@ -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)
}

View File

@ -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
}

View File

@ -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})`,