From f7d3ca6cac2245ee7b9e31df0c24d7b5629993bd Mon Sep 17 00:00:00 2001 From: Patrik Tkacik Date: Fri, 4 Apr 2025 23:34:58 +0200 Subject: [PATCH] Add feature to include subfolder files in parent folder index --- package-lock.json | 4 ++-- src/types/MarkdownTextRenderer.ts | 40 +++++++++++++++++++++---------- 2 files changed, 30 insertions(+), 14 deletions(-) diff --git a/package-lock.json b/package-lock.json index c73e8d9..071bca6 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "obsidian-sample-plugin", - "version": "1.0.28", + "version": "1.0.29", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "obsidian-sample-plugin", - "version": "1.0.28", + "version": "1.0.29", "license": "MIT", "devDependencies": { "@types/node": "16.11.6", diff --git a/src/types/MarkdownTextRenderer.ts b/src/types/MarkdownTextRenderer.ts index 8d9b647..d9f37e9 100644 --- a/src/types/MarkdownTextRenderer.ts +++ b/src/types/MarkdownTextRenderer.ts @@ -18,17 +18,17 @@ export class MarkdownTextRenderer { public buildMarkdownText(filesInFolder: TAbstractFile[]): string { const fileTree = this.buildFileTree(filesInFolder); - return this.buildStructureMarkdownText(fileTree, 0); + return this.buildStructureMarkdownText(fileTree, 0, true); // Pass true for isTopLevel } - private buildStructureMarkdownText(fileTree: FileTree, indentLevel: number): string { + private buildStructureMarkdownText(fileTree: FileTree, indentLevel: number, isTopLevel: boolean = false): string { let markdownText = "" for (const file of fileTree) { if(isExcludedPath(file.path)){ continue } - if (file instanceof TFolder && this.plugin.settings.recursiveIndexFiles) { + if (file instanceof TFolder) { // Create a deep copy of the children array let children = file.children; const indexFile = this.checkIfFolderHasIndexFile(file.children) @@ -39,11 +39,29 @@ export class MarkdownTextRenderer { } else { markdownText += this.buildMarkdownLinkString(file.name, null, indentLevel, true) } - if (this.plugin.settings.recursionLimit === -1 || indentLevel < this.plugin.settings.recursionLimit) { - markdownText += this.buildStructureMarkdownText(this.buildFileTree(children), indentLevel + 1) + + // For top-level rendering, we always want to show subfolder contents + // For recursive rendering, we only show if recursiveIndexFiles is enabled + if (isTopLevel || this.plugin.settings.recursiveIndexFiles) { + // Check recursion limit + if (this.plugin.settings.recursionLimit === -1 || indentLevel < this.plugin.settings.recursionLimit) { + // Process non-index files in this folder + const sortedChildren = this.buildFileTree(children); + + for (const child of sortedChildren) { + if (child instanceof TFile) { + if (!isIndexFile(child.path)) { + markdownText += this.buildContentMarkdownText(child, indentLevel + 1) + } + } else if (child instanceof TFolder) { + // For subfolders, pass false for isTopLevel + markdownText += this.buildStructureMarkdownText([child], indentLevel + 1, false) + } + } + } } } - if (file instanceof TFile) { + else if (file instanceof TFile) { if (isIndexFile(file.path)) { continue; } @@ -188,11 +206,9 @@ export class MarkdownTextRenderer { private buildFileTree(filesInFolder: TAbstractFile[]): FileTree { const fileTree: FileTree = []; for (const file of filesInFolder) { - if (file instanceof TFolder && this.plugin.settings.recursiveIndexFiles) { - fileTree.push(file) - } - if (file instanceof TFile) { - fileTree.push(file) + // Only add files and folders, not other abstract file types + if (file instanceof TFile || file instanceof TFolder) { + fileTree.push(file); } } if (this.plugin.settings.sortIndexFiles === SortBy.Alphabetically) { @@ -239,4 +255,4 @@ export class MarkdownTextRenderer { } return indentText } -} +} \ No newline at end of file