From 8d8b72bf1b706fa0b0f4ff27d3ca1716644abbcc Mon Sep 17 00:00:00 2001 From: purocean Date: Mon, 7 Mar 2022 21:51:01 +0800 Subject: [PATCH] fix: fix local image path recognition --- src/renderer/plugins/copy-content.tsx | 6 +++--- src/renderer/plugins/image-hosting-picgo.ts | 6 +++--- src/renderer/plugins/markdown-link.ts | 8 ++++---- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/renderer/plugins/copy-content.tsx b/src/renderer/plugins/copy-content.tsx index 7e06412e..f15f57c4 100644 --- a/src/renderer/plugins/copy-content.tsx +++ b/src/renderer/plugins/copy-content.tsx @@ -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) ) } diff --git a/src/renderer/plugins/image-hosting-picgo.ts b/src/renderer/plugins/image-hosting-picgo.ts index b0f45348..97c8abbf 100644 --- a/src/renderer/plugins/image-hosting-picgo.ts +++ b/src/renderer/plugins/image-hosting-picgo.ts @@ -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) } diff --git a/src/renderer/plugins/markdown-link.ts b/src/renderer/plugins/markdown-link.ts index 9f962004..c808e7e7 100644 --- a/src/renderer/plugins/markdown-link.ts +++ b/src/renderer/plugins/markdown-link.ts @@ -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)}`) }