Skip to content

Commit

Permalink
feat: exclude by globs
Browse files Browse the repository at this point in the history
closes #11
  • Loading branch information
aviskase committed Dec 5, 2020
1 parent c939aec commit dcab441
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 5 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ If both exclude *from* and *to* are set to `B`, the the output will be:
1 [[X]]
```

**Exclude links from paths** and **Exclude links to paths** works similarly to filename exclusion, but accept glob patterns. Check [picomatch docs](https://www.npmjs.com/package/picomatch#globbing-features) for detailed information. Useful, when you want to exclude some directories, for example, exclude everything from directory *Dailies* is `Dailies/**/*`.


## Compatibility
v0.0.1 was developed against Obsidian v0.9.12, but it may work in earlier versions (v0.9.7+).
Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,15 @@
"@rollup/plugin-node-resolve": "^9.0.0",
"@rollup/plugin-typescript": "^6.0.0",
"@types/node": "^14.14.8",
"@types/picomatch": "^2.2.1",
"obsidian": "https://github.com/obsidianmd/obsidian-api/tarball/master",
"rollup": "^2.33.2",
"standard-version": "^9.0.0",
"tslib": "^2.0.3",
"typescript": "^4.0.3"
},
"dependencies": {
"deepmerge": "^4.2.2"
"deepmerge": "^4.2.2",
"picomatch": "^2.2.2"
}
}
38 changes: 34 additions & 4 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import deepmerge from 'deepmerge';
import picomatch from 'picomatch';
import { Plugin, PluginSettingTab, Setting, Vault, normalizePath, TFile, getLinkpath, ReferenceCache, Notice } from 'obsidian';

interface IndexNode {
Expand Down Expand Up @@ -63,7 +64,7 @@ export default class LinkIndexer extends Plugin {

const files = this.app.vault.getMarkdownFiles();
files.forEach((f) => {
if (this.isExcluded(f, preset.excludeFromFilenames)) return;
if (this.isExcluded(f, preset.excludeFromFilenames, preset.excludeFromGlobs)) 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 +82,18 @@ export default class LinkIndexer extends Plugin {
}
}

isExcluded(f: TFile, filenamePatterns: string[]) {
return this.globalExcludes.find((g) => pathEqual(g, f.path)) || filenamePatterns.some((p) => new RegExp(p).test(f.name));
isExcluded(f: TFile, filenamePatterns: string[], globPatterns: string[]) {
const isGloballyExcluded = this.globalExcludes.some((g) => pathEqual(g, f.path));
const isFilenameExcluded = filenamePatterns.some((p) => new RegExp(p).test(f.name));
const isGlobExcluded = picomatch.isMatch(f.path, globPatterns);
return isGloballyExcluded || isFilenameExcluded || isGlobExcluded;
}

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 (originFile && (preset.nonexistentOnly || this.isExcluded(originFile, preset.excludeToFilenames))) {
if (originFile && (preset.nonexistentOnly || this.isExcluded(originFile, preset.excludeToFilenames, preset.excludeToGlobs))) {
return;
}
const origin = originFile ? originFile.path : link;
Expand All @@ -114,7 +118,9 @@ class UsedLinks {
linkToFiles = true;
nonexistentOnly = false;
excludeToFilenames: string[] = [];
excludeToGlobs: string[] = [];
excludeFromFilenames: string[] = [];
excludeFromGlobs: string[] = [];

constructor() {
this.name = Date.now().toString();
Expand Down Expand Up @@ -228,6 +234,18 @@ class LinkIndexerSettingTab extends PluginSettingTab {
})
);

new Setting(containerEl)
.setName('Exclude links from paths')
.setDesc('Expects path globs. Checks for file path including filename.')
.addTextArea((text) =>
text
.setValue(report.excludeFromGlobs.join('\n'))
.onChange(async (value) => {
report.excludeFromGlobs = 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.')
Expand All @@ -240,6 +258,18 @@ class LinkIndexerSettingTab extends PluginSettingTab {
})
);

new Setting(containerEl)
.setName('Exclude links to paths')
.setDesc('Expects path globs. Checks for file path including filename.')
.addTextArea((text) =>
text
.setValue(report.excludeToGlobs.join('\n'))
.onChange(async (value) => {
report.excludeToGlobs = 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

0 comments on commit dcab441

Please sign in to comment.