Skip to content

getBasename

Returns 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.

Import:

import { getBasename } from 'obsidian-dev-utils/obsidian/file-system';

Signature:

function getBasename(params: GetBasenameParams): string

Parameters:

ParameterTypeDescription
paramsGetBasenameParamsThe parameters for the retrieval.

Returns: string — The base name of the pathOrFile.


Links to this page: