Skip to content

Commit 0e56653

Browse files
committed
fix: 修复 Github 无法正确上传图床的问题
1 parent 8c9ea99 commit 0e56653

1 file changed

Lines changed: 21 additions & 7 deletions

File tree

src/app/core/article/md-editor.tsx

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ export function MdEditor() {
4242
const { theme } = useTheme()
4343
const { currentLocale } = useI18n()
4444
const t = useTranslations('article.file.sync')
45+
const te = useTranslations('article.editor')
4546
// 移动端强制使用即时渲染模式
4647
const defaultMode = isMobileDevice() ? 'ir' : 'ir'
4748
const [localMode, setLocalMode] = useLocalStorage<'ir' | 'sv' | 'wysiwyg'>('useLocalMode', defaultMode)
@@ -383,10 +384,15 @@ export function MdEditor() {
383384
const filesUrls = await uploadImages(files)
384385
if (vditor && typeof vditor.insertValue === 'function') {
385386
for (let i = 0; i < filesUrls.length; i++) {
386-
vditor.insertValue(`![${files[i].name}](${filesUrls[i]})`)
387+
// 只插入有效的 URL
388+
if (filesUrls[i] && filesUrls[i] !== 'undefined') {
389+
vditor.insertValue(`![${files[i].name}](${filesUrls[i]})`)
390+
}
387391
}
388392
}
389-
return filesUrls.join('\n')
393+
// 过滤掉 undefined 并返回有效的 URL
394+
const validUrls = filesUrls.filter(url => url && url !== 'undefined')
395+
return validUrls.join('\n')
390396
} else {
391397
// 保存到当前笔记所在文件夹的静态资源目录
392398
const workspace = await getWorkspacePath()
@@ -572,15 +578,23 @@ export function MdEditor() {
572578
async function uploadImages(files: File[]) {
573579
const list = await Promise.all(
574580
files.map((file) => {
575-
return new Promise<string>(async(resolve, reject) => {
576-
if (!file.type.includes('image')) return
581+
return new Promise<string>((resolve, reject) => {
582+
// 过滤掉非图片文件和 null
583+
if (!file || !file.type || !file.type.includes('image')) {
584+
resolve(undefined as unknown as string)
585+
return
586+
}
577587
const toastNotification = toast({
578-
title: t('upload.uploading'),
588+
title: te('upload.uploading'),
579589
description: file.name,
580590
duration: 600000,
581591
})
582-
await uploadImage(file).then(async url => {
583-
resolve(url)
592+
uploadImage(file).then(url => {
593+
if (url) {
594+
resolve(url)
595+
} else {
596+
resolve(undefined as unknown as string)
597+
}
584598
}).catch(err => {
585599
reject(err)
586600
}).finally(() => {

0 commit comments

Comments
 (0)