mirror of
https://github.com/alphahasher/obsidian-remove-links.git
synced 2026-07-22 13:00:26 +00:00
feat: add commands to remove external and internal links from file and selection
This commit is contained in:
parent
d90e13539d
commit
4dd262ebcb
1 changed files with 37 additions and 4 deletions
41
main.ts
41
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')
|
||||
|
|
|
|||
Loading…
Reference in a new issue