fix: fix local image path recognition

This commit is contained in:
purocean
2022-03-07 21:51:01 +08:00
parent 3cd0ab7227
commit 8d8b72bf1b
3 changed files with 10 additions and 10 deletions
+3 -3
View File
@@ -64,7 +64,7 @@ export default {
const originSrc = token.attrGet(ctx.constant.DOM_ATTR_NAME.ORIGIN_SRC)
if (srcAttr && originSrc) {
const res: Response = await ctx.api.fetchHttp(srcAttr)
const fileName = ctx.utils.path.basename(originSrc)
const fileName = ctx.utils.path.basename(ctx.utils.removeQuery(originSrc))
const file = new File(
[await res.blob()],
fileName,
@@ -79,8 +79,8 @@ export default {
}
if (url) {
markdown = markdown.replace(
new RegExp(ctx.utils.encodeMarkdownLink(originSrc), 'g'),
markdown = markdown.replaceAll(
ctx.utils.encodeMarkdownLink(originSrc),
ctx.utils.encodeMarkdownLink(url)
)
}
+3 -3
View File
@@ -152,12 +152,12 @@ export default {
throw new Error('No file opened.')
}
const filePath = el.getAttribute(DOM_ATTR_NAME.ORIGIN_SRC)!
const fileName = ctx.utils.path.basename(filePath)
const originSrc = el.getAttribute(DOM_ATTR_NAME.ORIGIN_SRC)!
const fileName = ctx.utils.path.basename(ctx.utils.removeQuery(originSrc))
const res: Response = await ctx.api.fetchHttp(el.getAttribute('src')!)
const file = new File([await res.blob()], fileName)
const url = await ctx.action.getActionHandler(uploadActionName)(file)
ctx.editor.replaceValue(ctx.utils.encodeMarkdownLink(filePath), `${url}`)
ctx.editor.replaceValue(ctx.utils.encodeMarkdownLink(originSrc), `${url}`)
} catch (error: any) {
ctx.ui.useToast().show('warning', error.message)
}
+4 -4
View File
@@ -2,7 +2,7 @@ import StateCore from 'markdown-it/lib/rules_core/state_core'
import Token from 'markdown-it/lib/token'
import { Plugin } from '@fe/context'
import store from '@fe/support/store'
import { sleep } from '@fe/utils'
import { removeQuery, sleep } from '@fe/utils'
import { isElectron } from '@fe/support/env'
import { DOM_ATTR_NAME, DOM_CLASS_NAME } from '@fe/support/constant'
import { basename, dirname, join, resolve } from '@fe/utils/path'
@@ -137,14 +137,14 @@ function convertLink (state: StateCore) {
const originAttr = isAnchor ? DOM_ATTR_NAME.ORIGIN_HREF : DOM_ATTR_NAME.ORIGIN_SRC
token.attrSet(originAttr, attrVal)
const val = attrVal.replace(/[#?].*$/, '')
const originPath = removeQuery(attrVal)
if (repo === '__help__') {
token.attrSet(attrName, `api/help/file?path=${encodeURIComponent(val)}`)
token.attrSet(attrName, `api/help/file?path=${encodeURIComponent(originPath)}`)
return
}
const filePath = resolve(basePath, val)
const filePath = resolve(basePath, originPath)
token.attrSet(attrName, `api/attachment/${encodeURIComponent(fileName)}?repo=${repo}&path=${encodeURIComponent(filePath)}`)
}