-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
pasteTemplate.ts
34 lines (32 loc) · 1.01 KB
/
pasteTemplate.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import { IPasteStyle } from '#/types/enum'
import { handleUrlEncode } from '#/utils/common'
const formatCustomLink = (customLink: string, item: ImgInfo) => {
const fileName = item.fileName!.replace(new RegExp(`\\${item.extname}$`), '')
const url = item.url || item.imgUrl
const extName = item.extname
const formatObj = {
url,
fileName,
extName
}
const keys = Object.keys(formatObj) as ['url', 'fileName', 'extName']
keys.forEach(item => {
if (customLink.indexOf(`$${item}`) !== -1) {
const reg = new RegExp(`\\$${item}`, 'g')
customLink = customLink.replace(reg, formatObj[item])
}
})
return customLink
}
export default (style: IPasteStyle, item: ImgInfo, customLink: string | undefined) => {
const url = handleUrlEncode(item.url || item.imgUrl)
const _customLink = customLink || '$url'
const tpl = {
markdown: `![](${url})`,
HTML: `<img src="${url}"/>`,
URL: url,
UBB: `[IMG]${url}[/IMG]`,
Custom: formatCustomLink(_customLink, item)
}
return tpl[style]
}