Fix: encode spaces as %20 in generated Markdown link URLs

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) <noreply@anthropic.com>
This commit is contained in:
Andre Light 2026-04-12 17:55:26 -06:00 committed by Moy
parent f8c341c51b
commit 7be3eee3b3

View file

@ -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');
}
}