Skip to content

Commit

Permalink
feat: option to include embeds
Browse files Browse the repository at this point in the history
closes #5
  • Loading branch information
aviskase committed Nov 18, 2020
1 parent eaa126e commit 4d987ce
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 15 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ Command will create an index note (check path in settings) with the content:

### Output options

**Include embeds** counts both `![[file]]` and `[[file]]` links. When disabled, it will count only `[[file]]` links.

**Strict line breaks** corresponds to the same Editor setting: "off" = one line break, "on" = two line breaks.

| on | off |
Expand Down
50 changes: 35 additions & 15 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Plugin, PluginSettingTab, Setting, Vault, normalizePath, TFile, getLinkpath } from 'obsidian';
import { Plugin, PluginSettingTab, Setting, Vault, normalizePath, TFile, getLinkpath, ReferenceCache } from 'obsidian';

interface IndexNode {
count: number;
Expand Down Expand Up @@ -31,20 +31,10 @@ export default class LinkIndexer extends Plugin {
const uniqueLinks: Record<string, IndexNode> = {};
const files = this.app.vault.getMarkdownFiles();
files.forEach((f) => {
const links = this.app.metadataCache.getFileCache(f).links;
links?.forEach((l) => {
const link = getLinkpath(l.link);
const originFile = this.app.metadataCache.getFirstLinkpathDest(link, f.path);
const origin = originFile ? originFile.path : link;
if (uniqueLinks[origin]) {
uniqueLinks[origin].count += 1;
} else {
uniqueLinks[origin] = {
count: 1,
link: this.linkToFile(originFile, link)
};
}
});
this.grabLinks(uniqueLinks, f, this.app.metadataCache.getFileCache(f).links)
if (this.settings.includeEmbeds) {
this.grabLinks(uniqueLinks, f, this.app.metadataCache.getFileCache(f).embeds)
}
});
const sortedLinks = Object.entries(uniqueLinks).sort((a, b) => b[1].count - a[1].count);
const separator = this.settings.strictLineBreaks ? '\n\n' : '\n';
Expand All @@ -62,11 +52,28 @@ export default class LinkIndexer extends Plugin {
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);
const origin = originFile ? originFile.path : link;
if (uniqueLinks[origin]) {
uniqueLinks[origin].count += 1;
} else {
uniqueLinks[origin] = {
count: 1,
link: this.linkToFile(originFile, link)
};
}
});
}
}

class LinkIndexerSettings {
allUsedLinksPath = './all_used_links.md';
strictLineBreaks = true;
includeEmbeds = true;
linkToFiles = true;
}

Expand All @@ -92,6 +99,19 @@ class LinkIndexerSettingTab extends PluginSettingTab {
})
);

new Setting(containerEl)
.setName('Include embeds')
.setDesc('When disabled, only direct links are counted. Enable to include embedded (trascluded) links.')
.addToggle((value) =>
value
.setValue(plugin.settings.includeEmbeds)
.onChange(async (value) => {
plugin.settings.includeEmbeds = value;
await plugin.saveData(plugin.settings);
})
);


new Setting(containerEl)
.setName('Strict line breaks')
.setDesc('Corresponds to the same Editor setting: "off" = one line break, "on" = two line breaks.')
Expand Down

0 comments on commit 4d987ce

Please sign in to comment.