Skip to content

Commit

Permalink
🐛 Fix: paste url encoding bug
Browse files Browse the repository at this point in the history
ISSUES CLOSED: #454
  • Loading branch information
Molunerfinn committed Jun 28, 2020
1 parent a693544 commit 59d3eba
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/main/utils/common.ts
Original file line number Diff line number Diff line change
@@ -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)
}
Expand Down
16 changes: 16 additions & 0 deletions src/universal/utils/common.ts
Original file line number Diff line number Diff line change
@@ -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
}
5 changes: 3 additions & 2 deletions src/universal/utils/pasteTemplate.ts
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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})`,
Expand Down

0 comments on commit 59d3eba

Please sign in to comment.