diff --git a/app/src/editor/rename.ts b/app/src/editor/rename.ts index 56ca3a675..d74d844bf 100644 --- a/app/src/editor/rename.ts +++ b/app/src/editor/rename.ts @@ -6,7 +6,7 @@ import {removeEmbed} from "../protyle/wysiwyg/removeEmbed"; import {insertHTML} from "../protyle/util/insertHTML"; import {genEmptyBlock} from "../block/util"; import {isMobile} from "../util/functions"; -import {getDisplayName, pathPosix, setNotebookName} from "../util/pathName"; +import {getAssetName, getDisplayName, pathPosix, setNotebookName} from "../util/pathName"; import {fetchPost} from "../util/fetch"; import {escapeHtml} from "../util/escape"; @@ -100,7 +100,7 @@ export const renameAsset = (assetPath: string) => { dialog.bindInput(inputElement, () => { (btnsElement[1] as HTMLButtonElement).click(); }); - const oldName = assetPath.substring(7, assetPath.length - pathPosix().extname(assetPath).length - 23); + const oldName = getAssetName(assetPath); inputElement.value = oldName; inputElement.focus(); inputElement.select(); diff --git a/app/src/protyle/hint/extend.ts b/app/src/protyle/hint/extend.ts index 26af5ffb4..34a9e94f7 100644 --- a/app/src/protyle/hint/extend.ts +++ b/app/src/protyle/hint/extend.ts @@ -11,7 +11,7 @@ import {hasClosestBlock, hasClosestByClassName} from "../util/hasClosest"; import {getContenteditableElement, getTopAloneElement} from "../wysiwyg/getBlock"; import {replaceFileName} from "../../editor/rename"; import {transaction} from "../wysiwyg/transaction"; -import {getDisplayName} from "../../util/pathName"; +import {getAssetName, getDisplayName, pathPosix} from "../../util/pathName"; import {genEmptyElement} from "../../block/util"; import {updateListOrder} from "../wysiwyg/list"; import {escapeHtml} from "../../util/escape"; @@ -425,8 +425,8 @@ export const hintRenderWidget = (value: string, protyle: IProtyle) => { export const hintRenderAssets = (value: string, protyle: IProtyle) => { focusByRange(protyle.toolbar.range); - const type = value.substring(value.lastIndexOf(".")); - const filename = value.replace("assets/", ""); + const type = pathPosix().extname(value).toLowerCase(); + const filename = value.startsWith("assets/") ? getAssetName(value) : value; let fileMD = ""; if (Constants.SIYUAN_ASSETS_AUDIO.includes(type)) { fileMD += ``; @@ -435,7 +435,7 @@ export const hintRenderAssets = (value: string, protyle: IProtyle) => { } else if (Constants.SIYUAN_ASSETS_VIDEO.includes(type)) { fileMD += ``; } else { - fileMD += `[${filename}](${value})`; + fileMD += `[${value.startsWith("assets/") ? filename + type : value}](${value})`; } insertHTML(protyle.lute.SpinBlockDOM(fileMD), protyle); protyle.toolbar.subElement.classList.add("fn__none"); diff --git a/app/src/protyle/upload/index.ts b/app/src/protyle/upload/index.ts index b6735730c..53b117519 100644 --- a/app/src/protyle/upload/index.ts +++ b/app/src/protyle/upload/index.ts @@ -4,6 +4,7 @@ import {Constants} from "../../constants"; import {destroy} from "../util/destroy"; import {fetchPost} from "../../util/fetch"; import {getEditorRange} from "../util/selection"; +import {pathPosix} from "../../util/pathName"; export class Upload { public element: HTMLElement; @@ -99,18 +100,12 @@ const genUploadedLabel = (responseText: string, protyle: IProtyle) => { const keys = Object.keys(response.data.succMap); keys.forEach((key, index) => { const path = response.data.succMap[key]; - const lastIndex = key.lastIndexOf("."); - let type = key.substr(lastIndex); - let filename = protyle.options.upload.filename(key.substr(0, lastIndex)) + type; - if (-1 === lastIndex) { - type = ""; - filename = protyle.options.upload.filename(key); - } - type = type.toLowerCase(); + const type = pathPosix().extname(key).toLowerCase(); + const filename = protyle.options.upload.filename(key); if (Constants.SIYUAN_ASSETS_AUDIO.includes(type)) { succFileText += ``; } else if (Constants.SIYUAN_ASSETS_IMAGE.includes(type)) { - succFileText += `![${filename}](${path})`; + succFileText += `![${filename.substring(0, filename.length - type.length)}](${path})`; } else if (Constants.SIYUAN_ASSETS_VIDEO.includes(type)) { succFileText += ``; } else { diff --git a/app/src/util/pathName.ts b/app/src/util/pathName.ts index f8dc8fccb..75d1d5658 100644 --- a/app/src/util/pathName.ts +++ b/app/src/util/pathName.ts @@ -28,6 +28,10 @@ export const getDisplayName = (filePath: string, basename = true, removeSY = fal return name; }; +export const getAssetName = (assetPath: string) => { + return assetPath.substring(7, assetPath.length - pathPosix().extname(assetPath).length - 23) +} + export const isLocalPath = (link: string) => { if (!link) { return false;