Skip to content

Commit

Permalink
auto rename folder when the note file name changes
Browse files Browse the repository at this point in the history
  • Loading branch information
xpgo committed Feb 2, 2021
1 parent be980a5 commit 08af6e7
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 19 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ The keyword {{FOLDER_BRIEF_LIVE}} is a block of yaml codes which will be rendere

Remember to update the plugin, if you find some issues.

### 0.4.1

- auto rename folder when the note file name changes.

### 0.4.0

- move note filename with {{FOLDER_NAME}} to out of folder for better orgnization.
Expand Down
54 changes: 37 additions & 17 deletions main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ export default class FolderNotePlugin extends Plugin {
const editor = view.sourceMode.cmEditor;
const activeFile = this.app.workspace.getActiveFile();
// generate brief
var folderPath = this.getNoteFolderBriefPath(activeFile.path);
let folderPath = await this.getNoteFolderBriefPath(activeFile.path);
let briefCards = await this.makeFolderBriefCards(folderPath);
var folderBrief = briefCards.getHtmlCode();
editor.replaceSelection(folderBrief, "end");
Expand Down Expand Up @@ -180,9 +180,10 @@ export default class FolderNotePlugin extends Plugin {
}

// get the file breif path
getNoteFolderBriefPath(notePath: string) {
async getNoteFolderBriefPath(notePath: string) {
var folderPath = '';
if (this.isFolderNote(notePath)) {
let isFN = await this.isFolderNote(notePath);
if (isFN) {
folderPath = this.getNoteFolderPath(notePath);
}
else {
Expand All @@ -192,10 +193,17 @@ export default class FolderNotePlugin extends Plugin {
}

// check is folder note file?
isFolderNote(notePath: string) {
var folderPath = this.getNoteFolderPath(notePath);
var folderNotePaths = this.getFolderNotePath(folderPath);
return (folderNotePaths[0] == notePath);
async isFolderNote(notePath: string) {
var isFN = false;
var folderNoteName = this.settings.folderNoteName;
if (this.useFolderName) {
var folderPath = this.getNoteFolderPath(notePath);
isFN = await this.app.vault.adapter.exists(folderPath);
}
else if (notePath.endsWith('/' + folderNoteName + '.md')) {
isFN = true;
}
return isFN;
}

// check existence of folder note
Expand Down Expand Up @@ -430,15 +438,27 @@ export default class FolderNotePlugin extends Plugin {

// keep notefile name to be the folder name
async handleFileRename(newPath: any, oldPath: any) {
if (this.useFolderName && (!oldPath.endsWith('.md'))) {
// maybe this is a folder
// console.log('changing folder!!!')
let hasFolderNote = await this.hasFolderNote(oldPath);
if (hasFolderNote) {
var oldNotePaths = this.getFolderNotePath(oldPath);
var newNotePaths = this.getFolderNotePath(newPath.path);
if (oldNotePaths[1] != newNotePaths[1]) {
this.app.vault.adapter.rename(oldNotePaths[0], newNotePaths[0]);
if (this.useFolderName) {
if (!oldPath.endsWith('.md')) {
// maybe this is a folder
// console.log('changing folder!!!')
let hasFolderNote = await this.hasFolderNote(oldPath);
if (hasFolderNote) {
var oldNotePaths = this.getFolderNotePath(oldPath);
var newNotePaths = this.getFolderNotePath(newPath.path);
if (oldNotePaths[1] != newNotePaths[1]) {
this.app.vault.adapter.rename(oldNotePaths[0], newNotePaths[0]);
}
}
}
else {
let isFN = await this.isFolderNote(oldPath);
if (isFN) {
// console.log('oldPath: ', oldPath);
// console.log('newPath: ', newPath.path);
var oldFolderPath = this.getNoteFolderPath(oldPath);
var newFolderPath = this.getNoteFolderPath(newPath.path);
this.app.vault.adapter.rename(oldFolderPath, newFolderPath);
}
}
}
Expand Down Expand Up @@ -479,7 +499,7 @@ export default class FolderNotePlugin extends Plugin {
}
else {
const activeFile = this.app.workspace.getActiveFile();
folderPath = this.getNoteFolderBriefPath(activeFile.path);
folderPath = await this.getNoteFolderBriefPath(activeFile.path);
}

if (folderPath.length > 0) {
Expand Down
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"id": "folder-note-plugin",
"name": "Folder Note",
"version": "0.4.0",
"version": "0.4.1",
"minAppVersion": "0.9.12",
"description": "Click a folder node to show a note describing the folder.",
"author": "xpgo",
Expand Down
3 changes: 2 additions & 1 deletion versions.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@
"0.3.1": "0.9.7",
"0.3.2": "0.9.7",
"0.3.3": "0.9.7",
"0.4.0": "0.9.7"
"0.4.0": "0.9.7",
"0.4.1": "0.9.7"
}

0 comments on commit 08af6e7

Please sign in to comment.