Skip to content

Commit

Permalink
Add github CDN switch
Browse files Browse the repository at this point in the history
  • Loading branch information
yaleiyale committed Feb 26, 2023
1 parent a7714e3 commit 983ab0f
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 5 deletions.
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"id": "emo-uploader",
"name": "Emo",
"version": "2.13",
"version": "2.14",
"minAppVersion": "0.15.6",
"description": "Embed markdown online file/image links. This plugin is for uploading images to hosting or files to github in Obsidian.",
"author": "yaleiyale",
Expand Down
17 changes: 17 additions & 0 deletions src/fragment/fragment-github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import Emo from '../main'
import { EmoFragment } from '../base/emo-fragment'
import { HostingProvider } from '../config'
import { t } from '../lang/helpers'
import { CDNprovider } from '../parms/parms-github'

export class GithubFragment extends EmoFragment {
constructor (el: HTMLElement, plugin: Emo) {
Expand Down Expand Up @@ -91,5 +92,21 @@ export class GithubFragment extends EmoFragment {
await plugin.saveSettings()
})
})

const supportList: string[] = []
for (const key in CDNprovider) {
supportList.push(CDNprovider[key as keyof typeof CDNprovider])
}

new Setting(el)
.setName(t('cdn'))
.addDropdown((dropdown) => {
supportList.forEach((record) => { dropdown.addOption(record, record) })
dropdown.setValue(parms.cdn)
.onChange(async (value) => {
parms.cdn = value as CDNprovider
await plugin.saveSettings()
})
})
}
}
1 change: 1 addition & 0 deletions src/lang/locale/en.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export default {
token: 'token',
message: 'message',
path: 'path',
cdn: 'CDN',
'random filename': 'random filename',
'random filename desc': 'Random file names will greatly avoid duplicate file names. If you are sure that you need to use the original file name during this upload, make sure that no duplicate naming will occur under the path you choose.',
// fragment-imgbb.ts
Expand Down
1 change: 1 addition & 0 deletions src/lang/locale/zh-cn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export default {
token: '令牌',
message: '提交信息',
path: '路径',
cdn: 'CDN',
'random filename': '随机文件名',
'random filename desc': '采用随机文件名将极大避免重名。如果确定在上传过程中需要使用原文件名,请确保所选路径下不会出现重名问题。',
// fragment-imgbb.ts
Expand Down
1 change: 1 addition & 0 deletions src/lang/locale/zh-tw.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export default {
token: '令牌',
message: '提交資訊',
path: '路徑',
cdn: 'CDN',
'random filename': '隨機檔名',
'random filename desc': '採用隨機檔名將極大避免重名。如果確定在上傳過程中需要使用原檔名,請確保所選路徑下不會出現重名問題。',
// fragment-imgbb.ts
Expand Down
11 changes: 9 additions & 2 deletions src/parms/parms-github.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
import { EmoParms } from '../base/emo-parms'

export enum CDNprovider { // Github CDN
jsdelivr = 'jsdelivr',
statically = 'statically',
raw = 'raw'
}
export interface GithubParms extends EmoParms {
required: Required
path: string
random: boolean
cdn: CDNprovider
}

interface Required {
owner: string
repo: string
Expand All @@ -21,5 +27,6 @@ export const GITHUB_DEFAULT_PARMS: GithubParms = {
message: 'from emo-uploader·Github'
},
path: '',
random: true
random: true,
cdn: CDNprovider.raw
}
20 changes: 18 additions & 2 deletions src/uploader/uploader-github.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { request, RequestUrlParam } from 'obsidian'
import { GithubParms } from '../parms/parms-github'
import { CDNprovider, GithubParms } from '../parms/parms-github'
import { getBase64, getRandomFileName } from '../utils/file-helper'
import { EmoUploader } from '../base/emo-uploader'

Expand Down Expand Up @@ -39,7 +39,23 @@ export class GithubUploader extends EmoUploader {

return await new Promise((resolve, reject) => {
request(req).then(() => {
const markdownText = `![gh](https://fastly.jsdelivr.net/gh/${this.parms.required.owner}/${this.parms.required.repo}@${this.parms.required.branch}/${filePath})`
let markdownText: string
console.log(this.parms.cdn)
switch (this.parms.cdn) {
case CDNprovider.jsdelivr:
markdownText = `![gh](https://cdn.jsdelivr.net/gh/${this.parms.required.owner}/${this.parms.required.repo}@${this.parms.required.branch}/${filePath})`
break
case CDNprovider.statically:
markdownText = `![gh](https://cdn.statically.io/gh/${this.parms.required.owner}/${this.parms.required.repo}/${this.parms.required.branch}/${filePath})`
break
case CDNprovider.raw:
markdownText = `![gh](https://raw.githubusercontent.com/${this.parms.required.owner}/${this.parms.required.repo}/${this.parms.required.branch}/${filePath})`
break
default:
// use raw
markdownText = `![gh](https://raw.githubusercontent.com/${this.parms.required.owner}/${this.parms.required.repo}/${this.parms.required.branch}/${filePath})`
break
}
resolve(markdownText)
}).catch(err => {
reject(err)
Expand Down

0 comments on commit 983ab0f

Please sign in to comment.