From 29331f626562d6aba42c86c6272153536aa5fa45 Mon Sep 17 00:00:00 2001 From: bingryan Date: Sat, 30 Dec 2023 14:23:31 +0800 Subject: [PATCH] fix: attachment path --- src/main.ts | 2 +- src/utils.ts | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/src/main.ts b/src/main.ts index 3c9da73..679a1fe 100644 --- a/src/main.ts +++ b/src/main.ts @@ -138,7 +138,7 @@ class MarkdownExportSettingTab extends PluginSettingTab { new Setting(containerEl) .setName("Custom attachment path(optional)") - .setDesc("attachment path") + .setDesc("attachment path, relative to the output path") .addText((text) => text .setPlaceholder("Enter attachment path") diff --git a/src/utils.ts b/src/utils.ts index 7f13965..cbd837e 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -122,6 +122,18 @@ export function getResourceOsPath( return "."; } +/** + * + * @param path a/b/c.md + * @returns click path: unix: ../../ or windows: ..\..\, but need: ../ + */ +export function getClickSubRoute(p: string): string { + const parentLevels = p.split(path.sep).length - 1; + const parentRoute = ".." + path.sep; + + return parentRoute.repeat(parentLevels - 1); +} + export function fileExists(path: string): boolean { try { return fs.statSync(path).isFile(); @@ -318,8 +330,12 @@ export async function tryCopyMarkdownByRead( const imageLinkMd5 = md5(imageLink); const imageExt = path.extname(imageLink); // Unify the link separator in obsidian as a forward slash instead of the default back slash in windows, so that the referenced images can be displayed properly + + const clickSubRoute = getClickSubRoute(file.path); + const hashLink = path .join( + clickSubRoute, plugin.settings.attachment, imageLinkMd5.concat(imageExt) )