FlorianWoelki / obsidian-iconize

Simply add icons to anything you want in Obsidian.
https://florianwoelki.github.io/obsidian-iconize/
MIT License
852 stars 58 forks source link

Remove deleted files/renamed? #240

Open Mara-Li opened 1 year ago

Mara-Li commented 1 year ago

Is your feature request related to a problem? Please describe. The data.json become more and more big when in a big vault, as file are moved, renamed. The settings doesn't autoclean so it can lead to surprising icon/error when recreating file.

Describe the solution you'd like A listener to renamed/deleted/moved file, to remove old path.

Describe alternatives you've considered A node script that autoclean the data.

Additional context My js script: (feat french & english :'D)

//import data.json file from .obsidian/plugins/obsidian-icon-folder/data.json
const data = require("../../.obsidian/plugins/obsidian-icon-folder/data.json");
//get all files in the folder from root
const fs = require("fs");
const path = require("path");

//create a list of all file path in the folder from root (recursive)

const scriptDir = path.dirname(__filename);
const parentDir = path.join(scriptDir, "..", ".."); // Le ".." signifie le répertoire parent

// Fonction récursive pour obtenir la liste des fichiers
function getFilesRecursively(dir) {
    const files = fs.readdirSync(dir);
    let fileList = [];

    files.forEach((file) => {
        const filePath = path.join(dir, file);
        const stats = fs.statSync(filePath);

        if (stats.isDirectory() && !filePath.match(/\.(obsidian|trash|smart-connections)/g)) {
            // Si le chemin est un répertoire, appelez la fonction récursivement
            fileList = fileList.concat(getFilesRecursively(filePath));
            //also ajouter le dossier
            fileList.push(filePath);
        } //if file is not a directory remove extension and add to list
        else if (stats.isFile()) {
            fileList.push(filePath.replace(/\.[^\/.]+$/g, ""));
        }
        //remove root path
        fileList = fileList.map((file) => file.replace(path.normalize(`${parentDir}/`), ""));
    });

    return [...new Set(fileList)];
}

// Utilisez la fonction pour obtenir la liste des fichiers
let fileList = getFilesRecursively(parentDir);
fileList = fileList.map((file) => path.normalize(file));
console.log(fileList);

/*** ---- JSON OBJECT ------- */
/** form of
 * {
 *  "path/toFile": "iconName",
 *  "path/toFile2": {
 *      "iconName": iconName,
        "inheritanceIcon": "FasCode"
 *   },
 * }
 */

function removeNonExistPath(data, fileList) {
    const newData = {};
    const settings = data.settings;
    delete data.settings;
    newData.settings = settings;
    for (const [key, value] of Object.entries(data)) {
        if (fileList.includes(path.normalize(key))) {
            newData[key] = value;
        }
    }
    return newData;
}

const newData = removeNonExistPath(data, fileList);
console.log(newData);
/*** ---- JSON OBJECT ------- */
fs.writeFileSync(
    path.join(scriptDir, "..", "..", ".obsidian", "plugins", "obsidian-icon-folder", "data.json"),
    JSON.stringify(newData, null, 2)
);
FlorianWoelki commented 1 year ago

Could you elaborate on this a little more? I am not sure if I can reproduce this locally because for me, moved, renamed, or deleted files/folders are updated accordingly in the json data.

Mara-Li commented 1 year ago

I recorded a video with deleting a file for example of my issue:

https://github.com/FlorianWoelki/obsidian-iconize/assets/30244939/8e634406-c06c-4154-9346-0db7e4a8ff51

FlorianWoelki commented 1 year ago

Hmm. I am not able to reproduce this. Would it be possible to debug this yourself locally? Normally, this function should be called.

Mara-Li commented 1 year ago

Not related, but got this in the logs when loading/reloading the plugin: image Fixed by using ? :

export const getExtraPath = (iconPackName: string): string | undefined => {
  const path = Object.values(iconPacks).find((iconPack) => iconPack.name === iconPackName)?.path;
  return path?.length === 0 ? undefined : path;
};

Also get this when deleting:

image

And this on creating:

image

For the deleting/renaming, it's pretty strange. I can repro on some file and not others :/. Maybe adding a clean-up during Plugin load can help ? As file can be removed outside Obsidian it can help a lot.

Also, get that:

util.ts:99 Uncaught (in promise) TypeError: Cannot read properties of null (reading 'substring')
    at removeIconFromIconPack (util.ts:99:37)
    at IconFolderPlugin.removeFolderIcon (main.ts:451:7)
    at IconFolderPlugin.eval (main.ts:110:12)
    at Generator.next (<anonymous>)
    at fulfilled (tslib.es6.js:118:58)

Okay found!

The last one appear for inheritance where iconName is null.

I will make a PR with my change/proposition :)

Mara-Li commented 1 year ago

See #262 for more information & cleanUp function