Skip to content

Commit

Permalink
fix the rename and hidding problem
Browse files Browse the repository at this point in the history
  • Loading branch information
xpgo committed Feb 3, 2021
1 parent 5b382ee commit d9752cd
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 38 deletions.
37 changes: 9 additions & 28 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,11 @@ The mechanism is simple: attaching a note file to a folder. But there is a quest
| **Folder Note Path** | parent/myFolder/\_about\_.md | parent/myFolder/myFolder.md | parent/myFolder.md |
| **Configuration** | - **Note File method:** Index File<br />- **Index File Name:** \_about\_ (or other name you like) | **Note File method:** Folder Name Inside | **Note File method:** Folder Name Outside |
| **Pros** | - The note file belongs to the folder. <br />- The note filename keeps the same if you rename or move a folder. | - The note file belongs to the folder. <br />- The note file has the same name as folder, the note title looks better. | - The note file has the same name as folder, the note title looks better.<br />- Wiki-style of linking, easy to insert link like [\[myFolder]] |
| **Cons** | - The note filename and title may looks weird.<br />- Have to use additional file name for linking. | - You have to manually change the note and folder name, if you change one of them.<br /> | - The note file does not belong to the folder. You have to move the note file manually if a folder is moved. <br />- The note filename will be changed if you change the folder name. |

- The **default** configuration is the **Index-File** method with note filename of `_about_.md`. When create description note for a folder, a note file named `_about_.md` will be created in the clicked folder. However, the file `_about_.md` is hidden by the CSS rules of the plugin. You can let it shown by configure the **Hide Note** option.

- If you prefer the **Outside-Folder** or **Inside-Folder** method, please change the settings.
| **Cons** | - The note filename and title may looks weird.<br />- Have to use additional file name for linking. | - Linking outside of the folder will be [[myFolder/myFolder]].<br />- The note filename will be changed if you change the folder name. | - The note file does not belong to the folder. You have to move the note file manually if a folder is moved. <br />- The note filename will be changed if you change the folder name. |

- The **default** configuration is the **Inside-File** method.
- The **Index-File** method uses a note filename of `_about_.md` (is is configurable). When create description note for a folder, a note file named `_about_.md` will be created in the clicked folder. However, the file `_about_.md` is hidden by the CSS rules of the plugin. You can let it shown by configure the **Hide Note** option.
- If you prefer the **Outside-Folder** or **Index-File** method, please change the settings.
- **NOTICE**: For those who use the plugin of old versions, please change the **Note File Method** in the settings panel for your choice, and reopen Obsidian to take effect.

## Configuration
Expand Down Expand Up @@ -60,34 +59,16 @@ 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.5.1

- Fix the hiding issue for Outside-Folder method
- Add automatically rename for Inside-Folder method

### 0.5.0

- Add options for three different folder note file method
- Add options for auto rename

### 0.4.x

- auto rename folder when the note file name changes. (0.4.1)
- move note filename with {{FOLDER_NAME}} to out of folder for better organization. (0.4.0)

### 0.3.x

- add keyword {{FOLDER_BRIEF_LIVE}} for initial content to generate folder overview in real time. (0.3.3)
- Insert folder overview by command: ctrl+p, Insert Folder Overview (0.3.2)
- Reorganized source code and fixed a mini bug (0.3.2)
- Fix the command key on Mac (0.3.1)
- Automatically generate card-view of folder overview (Experimental).
- Add keyword {{FOLDER_BRIEF}} for generating the folder overview.

### 0.2.x

- Fix folder and note name check for hiding. (0.2.5)
- Add settings option to hide or unhide folder note file. (0.2.3)
- Fix: failed to create note file when create a new folder. (0.2.2)
- Change: change the default note name to \_about\_ because of folder rename problem. (0.2.2)
- Add: settings tab (0.2.1)
- Note name and contents can be configured. (0.2.1)

## Plans for future

- Add more template option for generating the initial content.
Expand Down
85 changes: 77 additions & 8 deletions main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ interface FolderNotePluginSettings {

const DEFAULT_SETTINGS: FolderNotePluginSettings = {
folderNoteHide: true,
folderNoteType: 'index',
folderNoteType: 'inside',
folderNoteName: '_about_',
folderNoteAutoRename: true,
folderNoteStrInit: '# {{FOLDER_NAME}} Overview\n {{FOLDER_BRIEF}} \n'
Expand All @@ -39,13 +39,17 @@ export default class FolderNotePlugin extends Plugin {
settings: FolderNotePluginSettings;
noteFileMethod: NoteFileMethod;
keyFOLDER_NAME: string;
filesToRename: string[];
filesToRenameSet: boolean;

async onload() {
console.log('Loading Folder Note plugin');

// class vars
this.noteFileMethod = NoteFileMethod.Index;
this.keyFOLDER_NAME = '{{FOLDER_NAME}}';
this.filesToRename = [];
this.filesToRenameSet = false;

// load settings
await this.loadSettings();
Expand All @@ -66,7 +70,6 @@ export default class FolderNotePlugin extends Plugin {

// hide sth
// this.hideRootNoteFiles();
// this.registerEvent(this.app.workspace.on("layout-ready", this.hideRootNoteFiles));

// for settings
this.addSettingTab(new FolderNoteSettingTab(this.app, this));
Expand Down Expand Up @@ -177,13 +180,15 @@ export default class FolderNotePlugin extends Plugin {
}

// get note folder
// make sure it is a note file
getNoteFolderPath(notePath: string) {
var folderPath = '';
if (this.noteFileMethod == NoteFileMethod.Index) {
folderPath = notePath.substring(0, notePath.lastIndexOf('/'));
}
else if (this.noteFileMethod == NoteFileMethod.Inside) {
folderPath = notePath.substring(0, notePath.lastIndexOf('/'));

}
else if (this.noteFileMethod == NoteFileMethod.Outside) {
// just remove .md
Expand Down Expand Up @@ -214,8 +219,15 @@ export default class FolderNotePlugin extends Plugin {
isFN = true;
}
}
else {
var folderPath = this.getNoteFolderPath(notePath);
else if (this.noteFileMethod == NoteFileMethod.Inside) {
var noteBaseName = notePath.split('/').pop();
noteBaseName = noteBaseName.substring(0, noteBaseName.length-3);
if (notePath.endsWith(noteBaseName + '/' + noteBaseName + '.md')) {
isFN = true;
}
}
else if (this.noteFileMethod == NoteFileMethod.Outside) {
var folderPath = notePath.substring(0, notePath.length-3);
isFN = await this.app.vault.adapter.exists(folderPath);
}
return isFN;
Expand Down Expand Up @@ -246,9 +258,9 @@ export default class FolderNotePlugin extends Plugin {
await this.app.vault.adapter.write(folderNotePath, noteStrInit);
showFolderNote = true;
}
else if (folderElem.hasClass('has-folder-note')) {
folderElem.removeClass('has-folder-note');
}
// else if (folderElem.hasClass('has-folder-note')) {
// folderElem.removeClass('has-folder-note');
// }
}

// show the note
Expand Down Expand Up @@ -420,7 +432,7 @@ export default class FolderNotePlugin extends Plugin {
// keep notefile name to be the folder name
async handleFileRename(newPath: any, oldPath: any) {
if (!this.settings.folderNoteAutoRename) return;
if (this.noteFileMethod != NoteFileMethod.Index) {
if (this.noteFileMethod == NoteFileMethod.Outside) {
if (!oldPath.endsWith('.md')) {
// changing folder name
// console.log('changing folder!!!')
Expand All @@ -445,6 +457,63 @@ export default class FolderNotePlugin extends Plugin {
}
}
}
else if (this.noteFileMethod == NoteFileMethod.Inside) {
if (!oldPath.endsWith('.md')) {
// changing folder name
var oldNotePaths = this.getFolderNotePath(oldPath);
var newNotePaths = this.getFolderNotePath(newPath.path);
var oldNotePathNew = newPath.path + '/' + oldNotePaths[1] + '.md';
let noteExists = await this.app.vault.adapter.exists(oldNotePathNew);
if (noteExists) {
if (newNotePaths[0] != oldNotePathNew) {
// put it to rename
this.filesToRename.push(oldNotePathNew);
this.filesToRename.push(newNotePaths[0]);
}
}
}
else if (this.filesToRename.length == 0) {
// changeing note name
let isFN = await this.isFolderNote(oldPath);
if (isFN) {
var oldFolderPath = this.getNoteFolderPath(oldPath);
// find the new path
var noteDir = newPath.path;
noteDir = noteDir.substring(0, noteDir.lastIndexOf('/'));
noteDir = noteDir.substring(0, noteDir.lastIndexOf('/'));
var noteBase = newPath.path.split('/').pop();
noteBase = noteBase.substring(0, noteBase.length-3);
var newFolderPath = '';
if (noteDir.length > 0) {
newFolderPath = noteDir + '/' + noteBase;
}
else {
newFolderPath = noteBase;
}
// put it to rename
if (oldFolderPath != newFolderPath) {
this.filesToRename.push(oldFolderPath);
this.filesToRename.push(newFolderPath);
}
}
}
// only do once a time
if (!this.filesToRenameSet && this.filesToRename.length > 0) {
this.filesToRenameSet = true;
setTimeout(() => {
console.log('rename is running after 1 s.');
if (this.filesToRename.length) {
var oldFolderPath = this.filesToRename[0];
var newFolderPath = this.filesToRename[1];
// console.log('Mod Old Path:', oldFolderPath);
// console.log('Mod New Path:', newFolderPath);
this.app.vault.adapter.rename(oldFolderPath, newFolderPath);
this.filesToRename = [];
this.filesToRenameSet = false;
}
}, 1000);
}
}
}

// form ccard code block
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.5.0",
"version": "0.5.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 @@ -12,5 +12,6 @@
"0.3.3": "0.9.7",
"0.4.0": "0.9.7",
"0.4.1": "0.9.7",
"0.5.0": "0.9.7"
"0.5.0": "0.9.7",
"0.5.1": "0.9.7"
}

0 comments on commit d9752cd

Please sign in to comment.