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

Detecting Templates Recursively in a Folder #72

Open
Ding-Fan opened this issue Apr 18, 2023 · 1 comment
Open

Detecting Templates Recursively in a Folder #72

Ding-Fan opened this issue Apr 18, 2023 · 1 comment
Labels
enhancement New feature or request good first issue Good for newcomers

Comments

@Ding-Fan
Copy link

At this function

chatgpt-md/main.ts

Lines 919 to 933 in 1cd9234

getFilesInChatFolder(): TFile[] {
const folder = this.app.vault.getAbstractFileByPath(
this.settings.chatTemplateFolder
) as TFolder;
if (folder != null) {
return folder.children as TFile[];
} else {
new Notice(
`Error getting folder: ${this.settings.chatTemplateFolder}`
);
throw new Error(
`Error getting folder: ${this.settings.chatTemplateFolder}`
);
}
}

It could recurrsively call it self according to folder or file.

https://marcus.se.net/obsidian-plugin-docs/vault#is-it-a-file-or-folder

const folderOrFile = this.app.vault.getAbstractFileByPath("folderOrFile");  
  
if (folderOrFile instanceof TFile) {  
console.log("It's a file!");  
// add to TFile[]
} else if (folderOrFile instanceof TFolder) {  
console.log("It's a folder!");  
// do recurrsion
}

Example from GPT

const isTemplate = (filePath: string) => {
  // check if filePath is a valid template
  ...
};

// Create a utility function for recursive search
const detectTemplatesInFolder = async (folderPath: string) => {
  const files = await this.app.vault.getFiles();
  
  let templateFiles = [];
  
  for (let i=0; i<files.length; i++) {
    const file = files[i];
    
    if (!file.path.startsWith(folderPath)) { // ignore unrelated files/folders 
      continue;
    }
    
    if (file instanceof TFile && isTemplate(file.path)) { // found a template file
      templateFiles.push(file);
      
    } else if (file instanceof TFolder) { // found a sub-folder, do recursion
      const subfolderTemplates = await detectTemplatesInFolder(file.path);
      
      if(subfolderTemplates.length > 0 ) {
        templateFiles.push(...subfolderTemplates);
      }
    }
  }
  
 return templateFiles;
};
@DenizOkcu DenizOkcu added enhancement New feature or request good first issue Good for newcomers labels Jan 10, 2025
@DenizOkcu
Copy link
Collaborator

This sounds like a great opportunity to turn it into a PR which we can discuss :-)

Let me know if you need help with PRs

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

2 participants