generated from obsidianmd/obsidian-sample-plugin
-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.ts
131 lines (105 loc) · 2.81 KB
/
main.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import { App, Plugin, PluginSettingTab, Setting, TFile } from "obsidian";
interface AutomaticTagsSettings {
tags: Record<string, string[]>;
}
const DEFAULT_SETTINGS: AutomaticTagsSettings = {
tags: {},
};
export default class AutomaticTagsPlugin extends Plugin {
settings: AutomaticTagsSettings;
async onload() {
await this.loadSettings();
this.addSettingTab(new AutomaticTagsSettingTab(this.app, this));
this.app.workspace.onLayoutReady(() => {
this.registerEvent(this.app.vault.on("create", async (file) => {
if (Object.entries(this.settings.tags).length === 0) return;
if (file instanceof TFile) {
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() { }
async loadSettings() {
this.settings = Object.assign(
{},
DEFAULT_SETTINGS,
await this.loadData()
);
}
async saveSettings() {
await this.saveData(this.settings);
}
matchesGlob(path: string, glob: string): boolean {
const regex = glob
.replace(/\./g, "\\.")
.replace(/\*/g, ".*")
.replace(/\//g, "\\/");
return new RegExp(regex).test(path);
}
}
class AutomaticTagsSettingTab extends PluginSettingTab {
plugin: AutomaticTagsPlugin;
constructor(app: App, plugin: AutomaticTagsPlugin) {
super(app, plugin);
this.plugin = plugin;
}
display(): void {
const { containerEl } = this;
containerEl.empty();
new Setting(containerEl)
.setName("Tags")
.setDesc("Tags to be automatically added to notes, in a simplified glob format")
.addTextArea((area) =>
area
.setValue(this.getTagsString())
.setPlaceholder(
"*: all\nfolder/subfolder: tag1, tag2\nother/folder: tag3"
)
.onChange(async (newValue) => {
this.setTagsString(newValue);
await this.plugin.saveSettings();
})
);
}
getTagsString(): string {
let result = "";
Object.entries(this.plugin.settings.tags).forEach(([k, v]) => {
result += `${k}: ${v.join(", ")}\n`;
});
return result;
}
setTagsString(value: string): void {
const result: Record<string, string[]> = {};
for (const line of value.split("\n")) {
if (line.trim().length === 0) continue;
const key = line.split(":")[0];
result[key.trim()] = line
.substring(key.length + 1)
.split(",")
.map((v) => v.trim());
}
this.plugin.settings.tags = result;
}
}