-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.js
75 lines (58 loc) · 2.68 KB
/
main.js
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
'use strict';
var obsidian = require('obsidian');
class NotesAsFolders extends obsidian.Plugin {
onload() {
this.addCommand(
{id: "make-folder-note", name: "Make this note a folder note", checkCallback: this.noteToFolder.bind(this)}
);
this.registerEvent(this.app.vault.on("rename", this.onRenameFile.bind(this)));
}
noteToFolder(check) {
const note = this.app.workspace.getActiveFile();
if ( !note || note.extension !== "md" || isFolderNote(note.path)) return false;
if (check) return true;
return (async () => {
const destination = dirname(note.path) + "/" + note.basename;
const existing = this.app.vault.getAbstractFileByPath(destination);
if ( !existing ) {
await this.app.vault.createFolder(destination);
} else {
return new obsidian.Notice(`A file or folder named ${note.basename} is in the way; can't create folder`);
}
// Move the note into the folder
await this.safeRename(note, destination + "/" + note.name);
})()
}
async safeRename(file, newName) {
return await this.app.fileManager.renameFile(file, newName);
}
async onRenameFile(file, oldName) {
// We only care about notes that were already folder notes
if (file.extension !== "md" || !isFolderNote(oldName)) return;
const
oldDir = folderBasename(oldName),
oldPath = dirname(oldName),
newPath = dirname(file.path),
oldFolder = this.app.vault.getAbstractFileByPath(oldPath)
;
// If a folder note was renamed inside its folder, rename the folder to match
if (oldPath === newPath) {
const destination = dirname(oldPath) + "/" + file.basename;
await this.safeRename(oldFolder, destination);
}
// If a folder note was moved out of its old folder to a new folder,
else if (oldFolder && file.basename === oldDir) {
const destination = newPath + "/" + oldDir;
// Move the folder alongside the note,
await this.safeRename(oldFolder, destination);
// Then move the note back into the folder
await this.safeRename(file, destination + "/" + file.name);
return;
}
}
}
function folderBasename(path) { return path.split("/").slice(-2, -1).pop(); }
function basename(path) { return path.split("/").pop(); }
function dirname(path) { return path.split("/").slice(0, -1).join("/"); }
function isFolderNote(path) { return basename(path) === folderBasename(path) + ".md"; }
module.exports = NotesAsFolders;