extension

This is a collection of functions for working with file extensions.

extname(path)

This behaves the same as the path.extname() in Node.js. It returns the extension of the given file name or path.

$ ori "extension/extname('foo.html')"
.html

extname ignores trailing slashes:

$ ori "extension/extname('https://example.com/data.json/')"
.json

If a file has multiple extensions, extname returns just the last one:

$ ori "extension/extname('index.ori.html')"
.html

match(key, ext)

If the key ends in the given extension, this returns the base name without the extension; otherwise this returns null.

$ ori "extension/match('index.md', '.html')"

$ ori "extension/match('index.html', '.html')"
index

Trailing slashes are ignored for this comparison.

match supports a more liberal interpretation of “extension” than extname does. A file name like index.ori.html ends in a multi-part extension. extname will return just the last .html part, but match can be used to match against the full multi-part .ori.html extension.

$ ori "extension/match('index.ori.html', '.ori.html')"
index

replace(key, ext1, ext2)

Return key with the extension ext1 (if it exists) replaced by extension ext2. If key doesn’t end in the extension, it is returned unchanged.

$ ori "extension/replace('index.md', '.md', '.html')"
index.html

Like match, replace supports multi-part extensions like .ori.html.

This is appropriate when handling a single file. If you are replacing extensions on a set of files, use the extension option of the map builtin.