From cd9c1e06bdc5cf2f2df0c9ddc889f9730dd33caf Mon Sep 17 00:00:00 2001 From: Moy Date: Tue, 21 Oct 2025 17:08:27 +0800 Subject: [PATCH] feat: add simplified-heading-to-note-link setting Introduce a new boolean setting simplifiedHeadingToNoteLink to allow simplifying heading links into note links when the filename matches the heading. Update default settings to enable the option. Add related i18n keys and translations across en/zh/zh-tw: - note-link-simplified - simplified-heading-to-note-link - simplified-heading-to-note-link-desc Update settings UI to expose the toggle with description and save behavior, and re-render the settings panel when toggled. These changes improve UX by letting users copy a simpler note link instead of a filename#heading link when the filename corresponds to the heading (commonly for top-level headings). --- src/i18n.ts | 18 ++++++++++++++---- src/main.ts | 20 +++++++++++++++++--- src/settingTab.ts | 13 +++++++++++++ src/type.ts | 2 ++ 4 files changed, 46 insertions(+), 7 deletions(-) diff --git a/src/i18n.ts b/src/i18n.ts index 42ca26f..00d8047 100644 --- a/src/i18n.ts +++ b/src/i18n.ts @@ -10,10 +10,11 @@ export type TranslationKey = | 'no-file' | 'no-content' | 'inline-code-copied' | 'block-id-copied' | 'note-link-copied' | 'heading-copied' | 'strikethrough-copied' | 'inline-latex-copied' | 'bold-copied' | 'highlight-copied' | 'italic-copied' | 'link-text-copied' | 'link-url-copied' - | 'wiki-link-copied' | 'callout-copied' + | 'wiki-link-copied' | 'callout-copied' | 'note-link-simplified' | 'format' | 'add-to-menu' | 'add-to-menu-desc' | 'show-notice' | 'show-notice-desc' | 'add-extra-commands' | 'add-extra-commands-desc' | 'use-heading-as-display' | 'use-heading-as-display-desc' | 'heading-link-separator' | 'heading-link-separator-desc' | 'block-id' + | 'simplified-heading-to-note-link' | 'simplified-heading-to-note-link-desc' | 'use-frontmatter-as-display' | 'use-frontmatter-as-display-desc' | 'frontmatter-key' | 'frontmatter-key-desc' | 'link-format'| 'link-format-desc' | 'markdown-link' | 'wiki-link' | 'contextual-copy' | 'copy-current-file-link' | 'file-link-copied' @@ -69,6 +70,7 @@ export const translations: Record> = { 'link-url-copied': 'Link URL copied!', 'wiki-link-copied': 'Wiki link copied!', 'callout-copied': 'Callout copied!', + 'note-link-simplified': 'Link simplified (filename matches heading)', // 设置界面 'format': 'Format', @@ -84,8 +86,10 @@ export const translations: Record> = { 'add-extra-commands-desc': 'Enable to add "Copy current note link" and "Generate & copy current block link" commands to the command palette', 'show-notice': 'Show notice', 'show-notice-desc': 'Show notification when content is copied', - 'use-heading-as-display': 'Heading Link: Use heading as display text', + 'use-heading-as-display': 'Heading Link: Only use heading as display text', 'use-heading-as-display-desc': 'Use the heading text as display text in copied heading links', + 'simplified-heading-to-note-link': 'Simplify link when filename matches heading', + 'simplified-heading-to-note-link-desc': 'When the filename matches the heading text (ignoring spaces), create a note link instead of a heading link', 'heading-link-separator': 'Heading Link: Separator between filename and heading', 'heading-link-separator-desc': 'Customize the separator symbol between filename and heading (only shown when "Use heading as display text" is disabled)', 'link-format': 'Link format', @@ -160,6 +164,7 @@ export const translations: Record> = { 'link-url-copied': '链接地址已复制!', 'wiki-link-copied': 'Wiki链接已复制!', 'callout-copied': '标注内容已复制!', + 'note-link-simplified': '链接已简化(文件名与标题相匹配)', // 设置界面 'format': '格式', @@ -171,8 +176,10 @@ export const translations: Record> = { 'add-extra-commands-desc': '启用后,将在命令面板中添加“复制当前笔记的链接”和“生成并复制当前块的链接”命令', 'show-notice': '显示通知', 'show-notice-desc': '复制内容时显示通知提示', - 'use-heading-as-display': '标题链接:使用标题作为显示文本', + 'use-heading-as-display': '标题链接:仅使用标题作为显示文本', 'use-heading-as-display-desc': '在复制的标题链接中,使用标题文本作为显示文本', + 'simplified-heading-to-note-link': '文件名匹配标题时简化链接', + 'simplified-heading-to-note-link-desc': '当文件名与标题文本匹配时(相同或包含),直接创建笔记链接而不是标题链接', 'heading-link-separator': '标题链接:文件名与标题间的连接符', 'heading-link-separator-desc': '自定义文件名与标题之间的连接符号(仅在禁用"使用标题作为显示文本"时显示)', 'link-format': '链接格式', @@ -259,6 +266,7 @@ export const translations: Record> = { 'link-url-copied': '連結地址已複製!', 'wiki-link-copied': 'Wiki連結已複製!', 'callout-copied': '标注内容已複製!', + 'note-link-simplified': '連結已簡化(檔案名與標題相匹配)', // 設置界面 'format': '格式', @@ -268,8 +276,10 @@ export const translations: Record> = { 'add-to-menu-desc': '將命令添加到右鍵菜單', 'show-notice': '顯示通知', 'show-notice-desc': '複製內容時顯示通知提示', - 'use-heading-as-display': '標題連結:使用標題作為顯示文本', + 'use-heading-as-display': '標題連結:僅使用標題作為顯示文本', 'use-heading-as-display-desc': '在複製的標題連結中,使用標題文本作為顯示文本', + 'simplified-heading-to-note-link': '檔案名匹配標題時簡化連結', + 'simplified-heading-to-note-link-desc': '當檔案名與標題文本匹配時(相同或包含),直接建立筆記連結而不是標題連結', 'heading-link-separator': '標題連結:檔案名與標題間的連接符', 'heading-link-separator-desc': '自定義檔案名與標題之間的連接符號(僅在禁用「使用標題作為顯示文本」時顯示)', 'link-format': '連結格式', diff --git a/src/main.ts b/src/main.ts index 5137d28..6c65fb1 100644 --- a/src/main.ts +++ b/src/main.ts @@ -621,6 +621,20 @@ export default class EasyCopy extends Plugin { let headingReferenceLink = ""; let noteFlag = 0; + + let linkContent = `${filename}#${selectedHeading}`; + + function compareIgnoreCase(a: string, b: string): boolean { + return a.toLowerCase() === b.toLowerCase() || a.toLowerCase().includes(b.toLowerCase()); + } + + // 特殊情况:如果文件名包含标题,则不添加指向标题的 # 部分 + // 我自己的情况——会把 SomeThing 给拆成 Some Thing 来做标题,所以也考虑空格替换的部分 + if (filename === selectedHeading || compareIgnoreCase(filename, selectedHeading) || compareIgnoreCase(filename, selectedHeading.replace(/\s+/g, ''))) { + linkContent = filename; + new Notice(this.t('note-link-simplified')); + noteFlag = 1; + } // 根据设置选择链接格式 if (this.settings.linkFormat === LinkFormat.WIKILINK) { @@ -630,11 +644,11 @@ export default class EasyCopy extends Plugin { headingReferenceLink = `[[${filename}]]`; noteFlag = 1; } else { - if (displayText === `${filename}#${selectedHeading}`) { + if (displayText === linkContent) { // 特殊情况:当显示文本与 "文件名#标题" 相同时,省略显示文本 - headingReferenceLink = `[[${filename}#${selectedHeading}]]`; + headingReferenceLink = `[[${linkContent}]]`; } else { - headingReferenceLink = `[[${filename}#${selectedHeading}|${displayText}]]`; + headingReferenceLink = `[[${linkContent}|${displayText}]]`; } } } else { diff --git a/src/settingTab.ts b/src/settingTab.ts index 9f87f6a..a4d6251 100644 --- a/src/settingTab.ts +++ b/src/settingTab.ts @@ -88,6 +88,19 @@ export class EasyCopySettingTab extends PluginSettingTab { ); } + // 后续新增:文件名包含标题时,简化为复制文件链接(通常用于复制一级标题时) + new Setting(containerEl) + .setName(this.plugin.t('simplified-heading-to-note-link')) + .setDesc(this.plugin.t('simplified-heading-to-note-link-desc')) + .addToggle(toggle => toggle + .setValue(this.plugin.settings.simplifiedHeadingToNoteLink) + .onChange(async (value) => { + this.plugin.settings.simplifiedHeadingToNoteLink = value; + await this.plugin.saveSettings(); + this.display(); // 重新渲染设置界面以显示或隐藏连接符设置 + })); + + // 新增:是否使用 frontmatter 属性作为显示文本 new Setting(containerEl) .setName(this.plugin.t('use-frontmatter-as-display')) diff --git a/src/type.ts b/src/type.ts index 7d280be..efff540 100644 --- a/src/type.ts +++ b/src/type.ts @@ -34,6 +34,7 @@ export interface EasyCopySettings { showNotice: boolean; useHeadingAsDisplayText: boolean; headingLinkSeparator: string; // 文件名和标题间的连接符 + simplifiedHeadingToNoteLink: boolean; // 是否简化标题到笔记链接 linkFormat: LinkFormat; customizeTargets: boolean; enableInlineCode: boolean; @@ -63,6 +64,7 @@ export const DEFAULT_SETTINGS: EasyCopySettings = { showNotice: true, useHeadingAsDisplayText: true, headingLinkSeparator: '#', + simplifiedHeadingToNoteLink: true, linkFormat: LinkFormat.WIKILINK, customizeTargets: false, enableInlineCode: true,