Skip to content

getExtension

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

Import:

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

Signature:

function getExtension(params: GetExtensionParams): string

Parameters:

ParameterTypeDescription
paramsGetExtensionParamsThe parameters for the retrieval.

Returns: string — The extension of the pathOrFile, or an empty string if it has none.


Links to this page: