Skip to content

Commit

Permalink
feat: nonexitent links filtering
Browse files Browse the repository at this point in the history
closes #1
  • Loading branch information
aviskase committed Nov 24, 2020
1 parent 5900e21 commit a52e98d
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
Expand Down
24 changes: 18 additions & 6 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, IndexNode>, 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
};
}
});
Expand All @@ -75,6 +74,7 @@ class LinkIndexerSettings {
strictLineBreaks = true;
includeEmbeds = true;
linkToFiles = true;
nonexistentOnly = false;
}

class LinkIndexerSettingTab extends PluginSettingTab {
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit a52e98d

Please sign in to comment.