Skip to content

Commit

Permalink
feat: add a command to add tags to existing file
Browse files Browse the repository at this point in the history
  • Loading branch information
Jamalam360 committed Mar 19, 2024
1 parent 258cda4 commit 5eeca52
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 23 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ There is one config option: a mapping of paths to tags. The path uses a simplifi

Comments are not allowed, but are included here for documentation purposes.

```
```yml
*: all # apply the 'all' tag to all new markdown files
physics/: physics # apply the 'physics' tag to all new files in the 'physics' folder
```
## Installation
_Plugin is pending approval on Obsidian website._
This plugin is available through the Community Plugins tab in Obsidian ([link](https://obsidian.md/plugins?id=automatic-tags)).
49 changes: 32 additions & 17 deletions main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,25 +21,40 @@ export default class AutomaticTagsPlugin extends Plugin {
if (Object.entries(this.settings.tags).length === 0) return;

if (file instanceof TFile) {
const tags: string[] = [];

Object.entries(this.settings.tags).forEach(([k, v]) => {
if (this.matchesGlob(file.path, k)) {
tags.push(...v);
}
});

if (tags.length === 0) return;

await this.app.fileManager.processFrontMatter(file, (fm) => {
fm.tags = [...new Set([...(fm.tags || []), ...tags])];
});
this.tagFile(file);
}
}));
});

this.addCommand({
id: "add-tags",
name: "Add tags to existing notes",
callback: async () => {
this.app.vault.getMarkdownFiles().forEach((file) => {
this.tagFile(file);
});
}
});
}

async tagFile(file: TFile) {
const tags: string[] = [];

Object.entries(this.settings.tags).forEach(([k, v]) => {
if (this.matchesGlob(file.path, k)) {
tags.push(...v);
}
});

if (tags.length === 0) return;

await this.app.fileManager.processFrontMatter(file, (fm) => {
fm.tags = [...new Set([...(fm.tags || []), ...tags])];
});

}

onunload() {}
onunload() { }

async loadSettings() {
this.settings = Object.assign(
Expand Down Expand Up @@ -106,9 +121,9 @@ class AutomaticTagsSettingTab extends PluginSettingTab {
if (line.trim().length === 0) continue;
const key = line.split(":")[0];
result[key.trim()] = line
.substring(key.length + 1)
.split(",")
.map((v) => v.trim());
.substring(key.length + 1)
.split(",")
.map((v) => v.trim());
}

this.plugin.settings.tags = result;
Expand Down
3 changes: 1 addition & 2 deletions manifest.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
{
"id": "automatic-tags",
"name": "Automatic Tags",
"version": "1.0.2",
"minAppVersion": "0.15.0",
"description": "Add tags to new notes automatically based on their path.",
"author": "Jamalam",
"authorUrl": "https://jamalam.tech",
"isDesktopOnly": false
}
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "obsidian-automatic-tags",
"version": "1.0.2",
"version": "1.1.0",
"description": "Add tags to new notes automatically based on their path",
"main": "main.js",
"scripts": {
Expand Down
3 changes: 2 additions & 1 deletion versions.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"1.0.0": "0.15.0",
"1.0.1": "0.15.0",
"1.0.2": "0.15.0"
"1.0.2": "0.15.0",
"undefined": "0.15.0"
}

0 comments on commit 5eeca52

Please sign in to comment.