Skip to content

Commit

Permalink
feat: exclude based on filenames
Browse files Browse the repository at this point in the history
partial support for #11
  • Loading branch information
aviskase committed Dec 1, 2020
1 parent ec23bcb commit 4a9a58e
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 5 deletions.
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,30 @@ Off:
1 X
```

**Exclude links from files** and **Exclude links to files** allow skipping files during indexing. Both accept regex patterns. If you need several excludes, add them on separate lines. Exclusion is checked only for existing files and only for filename without path.

For example, if exclude *from* is set to `B`, the plugin won't count any links in this file and the output would be:

```
2 [[B]]
1 [[C]]
1 [[X]]
```

If exclude *to* is set to `B`, then any links to this file will be ignored, and the output will be:

```
2 [[C]]
1 [[X]]
```

If both exclude *from* and *to* are set to `B`, the the output will be:

```
1 [[C]]
1 [[X]]
```


## Compatibility
v0.0.1 was developed against Obsidian v0.9.12, but it may work in earlier versions (v0.9.7+).
Expand Down
50 changes: 45 additions & 5 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export default class LinkIndexer extends Plugin {

const files = this.app.vault.getMarkdownFiles();
files.forEach((f) => {
if (this.isExcludedFrom(f)) return;
if (this.isExcluded(f, preset.excludeFromFilenames)) return;
this.grabLinks(uniqueLinks, f, this.app.metadataCache.getFileCache(f).links, preset)
if (preset.includeEmbeds) {
this.grabLinks(uniqueLinks, f, this.app.metadataCache.getFileCache(f).embeds, preset)
Expand All @@ -81,15 +81,15 @@ export default class LinkIndexer extends Plugin {
}
}

isExcludedFrom(f: TFile) {
return this.globalExcludes.includes(f.path);
isExcluded(f: TFile, filenamePatterns: string[]) {
return this.globalExcludes.find((g) => pathEqual(g, f.path)) || filenamePatterns.some((p) => new RegExp(p).test(f.name));
}

grabLinks(uniqueLinks: Record<string, IndexNode>, f: TFile, links: ReferenceCache[], preset: UsedLinks) {
links?.forEach((l) => {
const link = getLinkpath(l.link);
const originFile = this.app.metadataCache.getFirstLinkpathDest(link, f.path);
if (preset.nonexistentOnly && originFile) {
if (originFile && (preset.nonexistentOnly || this.isExcluded(originFile, preset.excludeToFilenames))) {
return;
}
const origin = originFile ? originFile.path : link;
Expand All @@ -113,6 +113,8 @@ class UsedLinks {
includeEmbeds = true;
linkToFiles = true;
nonexistentOnly = false;
excludeToFilenames: string[] = [];
excludeFromFilenames: string[] = [];

constructor() {
this.name = Date.now().toString();
Expand Down Expand Up @@ -213,6 +215,31 @@ class LinkIndexerSettingTab extends PluginSettingTab {
await this.saveData({ refreshUI: false });
})
);

new Setting(containerEl)
.setName('Exclude links from files')
.setDesc('Expects regex patterns. Checks for filename without path.')
.addTextArea((text) =>
text
.setValue(report.excludeFromFilenames.join('\n'))
.onChange(async (value) => {
report.excludeFromFilenames = value.split('\n').filter((v) => v);
await this.saveData({ refreshUI: false });
})
);

new Setting(containerEl)
.setName('Exclude links to files')
.setDesc('Expects regex patterns. Checks for filename without path.')
.addTextArea((text) =>
text
.setValue(report.excludeToFilenames.join('\n'))
.onChange(async (value) => {
report.excludeToFilenames = value.split('\n').filter((v) => v);
await this.saveData({ refreshUI: false });
})
);

const deleteButton = new Setting(containerEl).addButton((extra) => {
return extra.setButtonText('Delete preset').onClick(async() => {
const index = plugin.settings.usedLinks.findIndex((r) => r.name === report.name);
Expand Down Expand Up @@ -242,4 +269,17 @@ class LinkIndexerSettingTab extends PluginSettingTab {
plugin.reloadSettings();
if (options.refreshUI) this.display();
}
}
}


function pathEqual(a: string, b: string) {
if (a === b) return true

return removeDots(normalizePath(a)) === removeDots(normalizePath(b))
}

function removeDots(value: string) {
return value.replace(/\\/g, '/')
.replace(/^\.\//, '')
.replace(/\/\.\//, '/')
}

0 comments on commit 4a9a58e

Please sign in to comment.