From 7be3eee3b3ebb33b9b8408a644ebf847cb5d5337 Mon Sep 17 00:00:00 2001 From: Andre Light <7719153+lightmotive@users.noreply.github.com> Date: Sun, 12 Apr 2026 17:55:26 -0600 Subject: [PATCH] Fix: encode spaces as %20 in generated Markdown link URLs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Obsidian doesn't parse Markdown links with literal spaces in the URL portion (e.g. `[text](file#My Heading)`). This encodes spaces as `%20` in the URL part of all Markdown-format links — heading links, block links, and file links — so they render correctly. Closes #29 Co-Authored-By: Claude Opus 4.6 (1M context) --- src/main.ts | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/main.ts b/src/main.ts index e8c406f..068fa27 100644 --- a/src/main.ts +++ b/src/main.ts @@ -569,12 +569,12 @@ export default class EasyCopy extends Plugin { // displayText = "^"+displayText; let blockIdLink = this.settings.linkFormat === LinkFormat.WIKILINK ? `[[${filename}#^${blockId}|${displayText}]]` - : `[${displayText}](${filename}#^${blockId})`; // markdown 格式不能加,不然会变成内联脚注语法 [^xxx] + : `[${displayText}](${this.encodeMarkdownLinkUrl(filename)}#^${blockId})`; // markdown 格式不能加,不然会变成内联脚注语法 [^xxx] if (!autoDisplayText) { blockIdLink = this.settings.linkFormat === LinkFormat.WIKILINK ? `[[${filename}#^${blockId}]]` - : `[](${filename}#^${blockId})`; + : `[](${this.encodeMarkdownLinkUrl(filename)}#^${blockId})`; } // 自动生成嵌入块 @@ -659,7 +659,8 @@ export default class EasyCopy extends Plugin { } } else { // Markdown链接格式 - headingReferenceLink = `[${displayText}](${filename}#${selectedHeading})`; + headingReferenceLink = `[${displayText}](${this.encodeMarkdownLinkUrl(`${filename}#${selectedHeading}`)})`; + } // 复制到剪贴板 @@ -706,7 +707,7 @@ export default class EasyCopy extends Plugin { } else { let path = file.path.replace(/\\/g, '/'); if (path.endsWith('.md')) path = path.slice(0, -3); - link = `[${display}](${path})`; + link = `[${display}](${this.encodeMarkdownLinkUrl(path)})`; } navigator.clipboard.writeText(link); if (this.settings.showNotice) { @@ -819,4 +820,8 @@ export default class EasyCopy extends Plugin { const lang = window.localStorage.getItem("language") || 'en'; return lang; } + + private encodeMarkdownLinkUrl(url: string): string { + return url.replace(/ /g, '%20'); + } }