generated from obsidianmd/obsidian-sample-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.ts
47 lines (40 loc) · 1.59 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
/*
* This plugin was developed with the assistance of ChatGPT, a language model created by OpenAI.
* https://openai.com/
*/
import { Plugin, TFolder } from 'obsidian';
export default class MyPlugin extends Plugin {
private createListener: (abstractFile: any) => void;
async onload() {
console.log('My plugin has loaded!');
// Check if a .nomedia file already exists in the vault root
const rootNomediaPath = `${this.app.vault.getRoot().path}/.nomedia`;
const rootNomediaExists = await this.app.vault.adapter.exists(rootNomediaPath);
if (!rootNomediaExists) {
this.app.vault.create(rootNomediaPath, '');
console.log(`Created .nomedia file in ${this.app.vault.getRoot().path}`);
} else {
console.log(`.nomedia file already exists in ${this.app.vault.getRoot().path}`);
}
// Create .nomedia files in new folders
this.createListener = async (abstractFile) => {
if (abstractFile instanceof TFolder) {
const folderNomediaPath = `${abstractFile.path}/.nomedia`;
const folderNomediaExists = await this.app.vault.adapter.exists(folderNomediaPath);
if (!folderNomediaExists) {
await this.app.vault.create(folderNomediaPath, '');
console.log(`Created .nomedia file in ${abstractFile.path}`);
} else {
console.log(`.nomedia file already exists in ${abstractFile.path}`);
}
}
};
// Register the create event listener
this.app.vault.on('create', this.createListener);
}
async onunload() {
// Unregister the create event listener on plugin unload
this.app.vault.off('create', this.createListener);
console.log('My plugin has been unloaded!');
}
}