SystemSculpt / obsidian-systemsculpt-ai

Enhance your Obsidian App experience with AI-powered tools for note-taking, task management, and much, MUCH more.
MIT License
99 stars 13 forks source link

context window folder import doesn't work on windows #73

Open momofox123 opened 1 month ago

momofox123 commented 1 month ago

Hi just want to let you know that folder import doesn't work on windows at the moment. when you select a context file by selecting a folder on windows. none of the files get imported.

I made this version of the addselecteditem code works with folders.

addSelectedItems() { console.log("FileSearcher: Adding selected items");

// Convert selectedItems Set to an Array
let selectedPaths = Array.from(this.selectedItems);
console.log("Selected paths in this.selectedItems:", selectedPaths);

// Array to hold markdown files from folders
let markdownFiles = [];

// Recursive function to get all markdown files in a folder and its subfolders
const getAllMarkdownFilesInFolder = (folder) => {
    let mdFiles = [];
    let folderContents = folder.children;

    if (!Array.isArray(folderContents)) {
        console.error(`Error: folder.children is not an array for folder: ${folder.path}`);
        return [];
    }

    folderContents.forEach(item => {
        if (item.children) {
            // If the item has 'children', it's a folder
            console.log(`Entering subfolder: ${item.path}`);
            // Recursively get markdown files in subfolder
            mdFiles = mdFiles.concat(getAllMarkdownFilesInFolder(item));
        } else if (item.extension === 'md') {
            // If the item has an extension and it's 'md', it's a markdown file
            console.log(`Found markdown file: ${item.path}`);
            mdFiles.push(item);
        }
    });

    return mdFiles;
};

// Process each selected path
selectedPaths.forEach(path => {
    console.log("Processing selected path:", path);

    // Get the file or folder by path using Obsidian's API
    let item = this.app.vault.getAbstractFileByPath(path);

    if (!item) {
        console.error(`Item not found for path: ${path}`);
        return;
    }

    if (item.children) {
        // If the item has 'children', it's a folder
        console.log(`Item is a folder: ${item.path}`);

        let mdFiles = getAllMarkdownFilesInFolder(item);

        console.log(`Markdown files found in folder (${item.path}) and subfolders:`, mdFiles);

        markdownFiles = markdownFiles.concat(mdFiles);
    } else if (item.extension === 'md') {
        // If the item has an extension and it's 'md', it's a markdown file
        console.log(`Item is a markdown file: ${item.path}`);
        markdownFiles.push(item);
    } else {
        console.log(`Item is not a folder or markdown file: ${item.path}`);
    }
});

// Remove duplicates in case of overlapping selections
markdownFiles = Array.from(new Set(markdownFiles.map(item => item.path)))
    .map(path => markdownFiles.find(item => item.path === path));

// Use markdownFiles as the final list of selected items
console.log("Final list of selected markdown items:", markdownFiles);

if (this.onChooseItems) {
    console.log("Triggering onChooseItems callback.");
    this.onChooseItems(markdownFiles);
} else {
    console.warn("onChooseItems callback is not defined.");
}

this.close();

}