From a52e98db91b5ac0f8080c234a041446e4f1a9995 Mon Sep 17 00:00:00 2001 From: Yuliya Bagriy Date: Mon, 23 Nov 2020 20:32:23 -0500 Subject: [PATCH] feat: nonexitent links filtering closes #1 --- README.md | 2 ++ src/main.ts | 24 ++++++++++++++++++------ 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index e801dfa..d3aee48 100644 --- a/README.md +++ b/README.md @@ -26,6 +26,8 @@ Command will create an index note (check path in settings) with the content: **Include embeds** counts both `![[file]]` and `[[file]]` links. When disabled, it will count only `[[file]]` links. +**Nonexistent files only**. When enabled, the example above would generate a note with only `1 [[X]]`. + **Strict line breaks** corresponds to the same Editor setting: "off" = one line break, "on" = two line breaks. | on | off | diff --git a/src/main.ts b/src/main.ts index a9aefe4..bcc5f89 100644 --- a/src/main.ts +++ b/src/main.ts @@ -48,22 +48,21 @@ export default class LinkIndexer extends Plugin { } } - linkToFile(originFile: TFile, fallback: string) { - const rawLink = originFile ? this.app.metadataCache.fileToLinktext(originFile, this.settings.allUsedLinksPath, true) : fallback; - return this.settings.linkToFiles ? `[[${rawLink}]]` : rawLink; - } - grabLinks(uniqueLinks: Record, f: TFile, links: ReferenceCache[]) { links?.forEach((l) => { const link = getLinkpath(l.link); const originFile = this.app.metadataCache.getFirstLinkpathDest(link, f.path); + if (this.settings.nonexistentOnly && originFile) { + return; + } const origin = originFile ? originFile.path : link; if (uniqueLinks[origin]) { uniqueLinks[origin].count += 1; } else { + const rawLink = originFile ? this.app.metadataCache.fileToLinktext(originFile, this.settings.allUsedLinksPath, true) : link; uniqueLinks[origin] = { count: 1, - link: this.linkToFile(originFile, link) + link: this.settings.linkToFiles ? `[[${rawLink}]]` : rawLink }; } }); @@ -75,6 +74,7 @@ class LinkIndexerSettings { strictLineBreaks = true; includeEmbeds = true; linkToFiles = true; + nonexistentOnly = false; } class LinkIndexerSettingTab extends PluginSettingTab { @@ -110,6 +110,18 @@ class LinkIndexerSettingTab extends PluginSettingTab { await plugin.saveData(plugin.settings); }) ); + + new Setting(containerEl) + .setName('Nonexistent files only') + .setDesc('When disabled, links to both existing and nonexisting files are counted.') + .addToggle((value) => + value + .setValue(plugin.settings.nonexistentOnly) + .onChange(async (value) => { + plugin.settings.nonexistentOnly = value; + await plugin.saveData(plugin.settings); + }) + ); new Setting(containerEl)