Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added prefix option for folder note filename. #67

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 45 additions & 38 deletions main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ enum FolderNoteType {
}

interface WaypointSettings {
waypointFlag: string
folderNotesPrefix: string
waypointFlag: string,
stopScanAtFolderNotes: boolean,
showFolderNotes: boolean,
showNonMarkdownFiles: boolean,
Expand All @@ -17,6 +18,7 @@ interface WaypointSettings {
}

const DEFAULT_SETTINGS: WaypointSettings = {
folderNotesPrefix: "_",
waypointFlag: "%% Waypoint %%",
stopScanAtFolderNotes: false,
showFolderNotes: false,
Expand Down Expand Up @@ -57,8 +59,8 @@ export default class Waypoint extends Plugin {
const parentFolder = this.getParentFolder(oldPath);
if (parentFolder !== null) {
this.foldersWithChanges.add(parentFolder);
this.scheduleUpdate();
}
this.scheduleUpdate();
}));
this.registerEvent(this.app.vault.on("modify", this.detectWaypointFlag));
});
Expand Down Expand Up @@ -102,10 +104,11 @@ export default class Waypoint extends Plugin {

isFolderNote(file: TFile): boolean {
if (this.settings.folderNoteType === FolderNoteType.InsideFolder) {
return file.basename == file.parent.name;
return file.basename == this.settings.folderNotesPrefix + file.parent.name;
} else if (this.settings.folderNoteType === FolderNoteType.OutsideFolder) {
if (file.parent) {
return this.app.vault.getAbstractFileByPath(this.getCleanParentPath(file) + file.basename) instanceof TFolder;
const folderName = file.basename.substring(this.settings.folderNotesPrefix.length)
return this.app.vault.getAbstractFileByPath(this.getCleanParentPath(file) + folderName) instanceof TFolder;
}
return false;
}
Expand Down Expand Up @@ -148,7 +151,7 @@ export default class Waypoint extends Plugin {
if (this.settings.folderNoteType === FolderNoteType.InsideFolder) {
fileTree = await this.getFileTreeRepresentation(file.parent, file.parent, 0, true);
} else if (this.settings.folderNoteType === FolderNoteType.OutsideFolder) {
const folder = this.app.vault.getAbstractFileByPath(this.getCleanParentPath(file) + file.basename);
const folder = this.app.vault.getAbstractFileByPath(this.getCleanParentPath(file) + file.basename.substring(this.settings.folderNotesPrefix.length));
if (folder instanceof TFolder) {
fileTree = await this.getFileTreeRepresentation(file.parent, folder, 0, true);
}
Expand Down Expand Up @@ -186,63 +189,57 @@ export default class Waypoint extends Plugin {
*/
async getFileTreeRepresentation(rootNode: TFolder, node: TAbstractFile, indentLevel: number, topLevel = false): Promise<string>|null {
const bullet = " ".repeat(indentLevel) + "-";
let text = ""

if (node instanceof TFile) {
console.log(node)
// Print the file name
if (node.extension == "md") {
if (this.settings.useWikiLinks) {
return `${bullet} [[${node.basename}]]`;
} else {
return `${bullet} [${node.basename}](${this.getEncodedUri(rootNode, node)})`;
}
text = this.settings.useWikiLinks
? `${bullet} [[${node.path}|${node.basename}]]`
: `${bullet} [${node.basename}](${this.getEncodedUri(rootNode, node)})`;
} else if (this.settings.showNonMarkdownFiles) {
if (this.settings.useWikiLinks) {
return `${bullet} [[${node.name}]]`;
} else {
return `${bullet} [${node.name}](${this.getEncodedUri(rootNode, node)})`;
}
text = this.settings.useWikiLinks
? `${bullet} [[${node.path}|${node.name}]]`
: `${bullet} [${node.name}](${this.getEncodedUri(rootNode, node)})`;
}
return null;
return text;

} else if (node instanceof TFolder) {
let text = "";
if (!topLevel || this.settings.showEnclosingNote) {
// Print the folder name
text = `${bullet} **${node.name}**`;
let folderNote;
if (this.settings.folderNoteType === FolderNoteType.InsideFolder) {
folderNote = this.app.vault.getAbstractFileByPath(node.path + "/" + node.name + ".md");
} else if (this.settings.folderNoteType === FolderNoteType.OutsideFolder) {
if (node.parent) {
folderNote = this.app.vault.getAbstractFileByPath(node.parent.path + "/" + node.name + ".md");
}
}
let folderNotePath = "/" + this.settings.folderNotesPrefix + node.name + ".md"
folderNotePath = this.settings.folderNoteType === FolderNoteType.InsideFolder
? node.path + folderNotePath
: this.settings.folderNoteType === FolderNoteType.OutsideFolder && node.parent
? node.parent.path + folderNotePath
: folderNotePath;
const folderNote = this.app.vault.getAbstractFileByPath(folderNotePath);
if (folderNote instanceof TFile) {
if (this.settings.useWikiLinks) {
text = `${bullet} **[[${folderNote.basename}]]**`;
} else {
text = `${bullet} **[${folderNote.basename}](${this.getEncodedUri(rootNode, folderNote)})**`;
}
text = this.settings.useWikiLinks
? `${bullet} **[[${folderNotePath}|${folderNote.parent.name}]]**`
: `${bullet} **[${folderNote.basename}](${this.getEncodedUri(rootNode, folderNote)})**`;
if (!topLevel) {
if (this.settings.stopScanAtFolderNotes) {
return text;
} else {
const content = await this.app.vault.cachedRead(folderNote);
if (content.includes(Waypoint.BEGIN_WAYPOINT) || content.includes(this.settings.waypointFlag)) {
return text;
// return text;
}
}
}
}
}
if (node.children && node.children.length > 0) {
let children = node.children;
if (children && children.length > 0) {
// Print the files and nested folders within the folder
let children = node.children;
children = children.sort((a, b) => {
return a.name.localeCompare(b.name, undefined, {numeric: true, sensitivity: 'base'});
});
if (!this.settings.showFolderNotes) {
if (this.settings.folderNoteType === FolderNoteType.InsideFolder) {
children = children.filter(child => this.settings.showFolderNotes || child.name !== node.name + ".md");
children = children.filter(child => this.settings.showFolderNotes || child.name !== this.settings.folderNotesPrefix + node.name + ".md");
} else if (this.settings.folderNoteType === FolderNoteType.OutsideFolder) {
const folderNames = new Set();
for (const element of children) {
Expand Down Expand Up @@ -326,10 +323,10 @@ export default class Waypoint extends Plugin {
while (folder) {
let folderNote;
if (this.settings.folderNoteType === FolderNoteType.InsideFolder) {
folderNote = this.app.vault.getAbstractFileByPath(folder.path + "/" + folder.name + ".md");
folderNote = this.app.vault.getAbstractFileByPath(folder.path + "/" + this.settings.folderNotesPrefix + folder.name + ".md");
} else if (this.settings.folderNoteType === FolderNoteType.OutsideFolder) {
if (folder.parent) {
folderNote = this.app.vault.getAbstractFileByPath(this.getCleanParentPath(folder) + folder.name + ".md");
folderNote = this.app.vault.getAbstractFileByPath(this.getCleanParentPath(folder) + this.settings.folderNotesPrefix + folder.name + ".md");
}
}
if (folderNote instanceof TFile) {
Expand Down Expand Up @@ -387,6 +384,16 @@ class WaypointSettingsTab extends PluginSettingTab {
const {containerEl} = this;
containerEl.empty();
containerEl.createEl('h2', {text: 'Waypoint Settings'});
new Setting(containerEl)
.setName("Folder Notes Prefix")
.setDesc("Allow prefix to be added to the folder notes filename.")
.addText(text => text
.setValue(this.plugin.settings.folderNotesPrefix)
.onChange(async (value) => {
this.plugin.settings.folderNotesPrefix = value;
await this.plugin.saveSettings();
})
);
new Setting(this.containerEl)
.setName("Folder Note Style")
.setDesc("Select the style of folder note used.")
Expand Down Expand Up @@ -475,4 +482,4 @@ class WaypointSettingsTab extends PluginSettingTab {
descriptionElement.createEl("a", { attr: { "href": "https://twitter.com/IdreesInc" }, text: "@IdreesInc" });
postscriptElement.appendChild(descriptionElement);
}
}
}
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.