Skip to content

obsidian/file-system

This module provides utility functions for working with TAbstractFile, TFile, and TFolder instances in Obsidian.

InterfaceDescription
DoesExistParamsParameters for doesExist.
GetAbstractFileOrNullParamsParameters for getAbstractFileOrNull.
GetAbstractFileParamsParameters for getAbstractFile.
GetBasenameParamsParameters for getBasename.
GetExtensionParamsParameters for getExtension.
GetFileOrNullParamsParameters for getFileOrNull.
GetFileParamsParameters for getFile.
GetFolderOrNullParamsParameters for getFolderOrNull.
GetFolderParamsParameters for getFolder.
GetMarkdownFilesParamsParameters for getMarkdownFiles.
IsTreatedAsAttachmentParamsParameters for isTreatedAsAttachment.
FunctionDescription
asArrayOfFilesConverts an array of abstract files to an array of files.
asArrayOfFoldersConverts an array of abstract files to an array of folders.
asFileConverts an abstract file to a file.
asFileOrNullConverts an abstract file to a file or null.
asFolderConverts an abstract file to a folder.
asFolderOrNullConverts an abstract file to a folder or null.
checkExtensionChecks if the given path or file has the specified extension.
doesExistChecks if the given path exists.
getAbstractFileRetrieves the TAbstractFile object for the given path or abstract file.
getAbstractFileOrNullRetrieves an abstract file or null based on the provided path or abstract file.
getBasenameReturns the base name of the given pathOrFile: a file’s name without its extension, a folder’s full name. Unlike basename from path.ts, which is a pure string function, this resolves the argument against the vault first. That is what lets a folder whose name contains a dot answer with its whole name instead of a bogus extension split, and it is why a caller handling both kinds no longer has to branch on isFile itself — a folder has no basename of its own. A path that resolves to nothing has no answer to look up, so GetBasenameParams.whenMissingTreatAs decides what happens. It defaults to MissingPathTreatment.Throw rather than to a guess: alpha.bravo may name a dotted folder or a .bravo file, and nothing in the vault says which, so a silently wrong answer is worse than an error. Pass MissingPathTreatment.File for the checkExtension reading of a bare string, or MissingPathTreatment.Folder for the other one.
getExtensionReturns the extension of the given pathOrFile: a file’s extension without the leading dot, and an empty string for a folder. Unlike extname from path.ts, which is a pure string function, this resolves the argument against the vault first, so a folder whose name contains a dot answers with an empty string rather than a bogus extension. A folder has no extension of its own, which is why a caller handling both kinds would otherwise branch on isFile itself. A path that resolves to nothing has no answer to look up, so GetExtensionParams.whenMissingTreatAs decides what happens. It defaults to MissingPathTreatment.Throw rather than to a guess: alpha.bravo may name a dotted folder or a .bravo file, and nothing in the vault says which, so a silently wrong answer is worse than an error. Pass MissingPathTreatment.File for the checkExtension reading of a bare string, or MissingPathTreatment.Folder for the other one.
getFileRetrieves a file based on the provided path or file.
getFileOrNullRetrieves a file or null based on the provided path or file. If the provided argument is already a file, it is returned as is. Otherwise, the function uses the app’s vault to retrieve the file by its path.
getFileSystemTypeGets the type of a file system object.
getFolderRetrieves a folder based on the provided app and pathOrFolder.
getFolderOrNullRetrieves a folder or null based on the provided path or folder.
getMarkdownFilesRetrieves an array of files representing the markdown files within a specified folder or path.
getOrCreateFileRetrieves the file for the given path or creates a new one if it does not exist.
getOrCreateFolderRetrieves the folder for the given path or creates a new one if it does not exist.
getPathReturns the path of the given pathOrFile.
isAbstractFileChecks if the given file is an instance of abstract file.
isBaseFileChecks if the given file is a base file.
isCanvasFileChecks if the given file is a canvas file.
isFileChecks if the given file is an instance of file.
isFolderChecks if the given file is a folder.
isMarkdownFileChecks if the given file is a Markdown file.
isNoteChecks if the given file is a note.
isTreatedAsAttachmentChecks if the given file should be treated as an attachment because of its extension. Some plugins store their data in files that are markdown on disk but are not prose — Excalidraw, for example, saves each drawing as sketch.excalidraw.md. Obsidian reports such a file’s extension as md, so a caller that wants to tell a drawing apart from a note has to match the full multi-part extension (.excalidraw.md) against the file name. Which extensions count is the caller’s configuration, not this library’s — only the matching rule is shared.
trimMarkdownExtensionTrims the markdown extension from the file path if the file is a markdown file. If the file is not a markdown file, the original file path is returned.
TypeDescription
PathOrAbstractFileA path or an abstract file.
PathOrFileA path or a file.
PathOrFolderA path or a folder.
EnumDescription
FileSystemTypeA type of file system object.
MissingPathTreatmentHow getBasename and getExtension should answer for a path that resolves to nothing in the vault. Nothing in the vault says whether an unresolved alpha.bravo names a dotted folder or a .bravo file, so the caller decides.
VariableDescription
BASE_FILE_EXTENSIONA file extension for base files.
CANVAS_FILE_EXTENSIONA file extension for canvas files.
MARKDOWN_FILE_EXTENSIONA file extension for markdown files.