From 4dd262ebcb3d2933efdab4e1652dd5cbb9aa76a5 Mon Sep 17 00:00:00 2001 From: Daniel Agafonov Date: Thu, 14 Aug 2025 07:38:26 -0700 Subject: [PATCH] feat: add commands to remove external and internal links from file and selection --- main.ts | 41 +++++++++++++++++++++++++++++++++++++---- 1 file changed, 37 insertions(+), 4 deletions(-) diff --git a/main.ts b/main.ts index f2fa8c5..cc2697e 100644 --- a/main.ts +++ b/main.ts @@ -47,6 +47,7 @@ export default class HyperlinkRemover extends Plugin { } } }); + this.addCommand({ id: 'remove-links-from-file', name: 'Remove links from file', @@ -62,6 +63,36 @@ export default class HyperlinkRemover extends Plugin { } }); + this.addCommand({ + id: 'remove-external-links-from-file', + name: 'Remove external links from file', + editorCallback: (editor: Editor, view: MarkdownView) => { + const content = editor.getValue(); + const updatedContent = this.processText(content, 'external'); + if (content !== updatedContent) { + editor.setValue(updatedContent); + new Notice('External links removed from file'); + } else { + new Notice('No external links found in the file'); + } + } + }); + + this.addCommand({ + id: 'remove-internal-links-from-file', + name: 'Remove internal links from file', + editorCallback: (editor: Editor, view: MarkdownView) => { + const content = editor.getValue(); + const updatedContent = this.processText(content, 'internal'); + if (content !== updatedContent) { + editor.setValue(updatedContent); + new Notice('Internal links removed from file'); + } else { + new Notice('No internal links found in the file'); + } + } + }); + // Context menu / Remove links / Selection this.registerEvent( this.app.workspace.on("editor-menu", (menu, editor, view) => { @@ -109,17 +140,19 @@ export default class HyperlinkRemover extends Plugin { } - processText(text: string): string { + processText(text: string, hyperlinkType?: 'both' | 'internal' | 'external'): string { let result = text; - if (this.settings.removeHyperlinks) { + // Process hyperlinks if enabled in settings or if specific mode is provided + if (this.settings.removeHyperlinks || hyperlinkType) { // Parse whitelist from comma-separated string const whitelist = this.settings.hyperlinkWhitelist .split(',') .map(item => item.trim()) .filter(item => item.length > 0); - result = removeHyperlinks(result, this.settings.keepHyperlinkText, whitelist, this.settings.hyperlinkType); + const linkType = hyperlinkType || this.settings.hyperlinkType; + result = removeHyperlinks(result, this.settings.keepHyperlinkText, whitelist, linkType); } if (this.settings.removeWikilinks) { @@ -200,7 +233,7 @@ class HyperlinkRemoverSettingTab extends PluginSettingTab { if (this.plugin.settings.removeHyperlinks) { new Setting(containerEl) .setName('Hyperlink Type') - .setDesc('Choose which types of hyperlinks to remove') + .setDesc('Choose which types of hyperlinks to remove from context menu. (command mode overrides this setting)') .addDropdown(dropdown => dropdown .addOption('both', 'Both Internal and External') .addOption('internal', 'Internal Links Only')