oen_liquid-template/utils.ts

26 lines
949 B
TypeScript

import { App, normalizePath, TAbstractFile, TFile, TFolder, Vault } from "obsidian";
export function getTFilesFromFolder(app: App, folderName: string, subfoldersToExclude?: string[]): Array<TFile> {
folderName = normalizePath(folderName);
let folder = app.vault.getAbstractFileByPath(folderName);
if (!folder) throw new Error(`${folderName} folder doesn't exist`);
if (!(folder instanceof TFolder)) throw new Error(`${folderName} is a file, not a folder`);
const foldersToExclude = subfoldersToExclude
? subfoldersToExclude.map(subfolder => [folderName,subfolder].join('/'))
: [];
let files: Array<TFile> = [];
Vault.recurseChildren(folder, (file: TAbstractFile) => {
if (foldersToExclude.some(toExclude => file.path.startsWith(toExclude))) return;
if (!(file instanceof TFile)) return;
files.push(file);
});
files.sort((a, b) => {
return a.basename.localeCompare(b.basename);
});
return files;
}