/** * A set of common path utilities commonly used through the Astro core and integration * projects. These do things like ensure a forward slash prepends paths. */ export function appendExtension(path: string, extension: string) { return path + '.' + extension; } export function appendForwardSlash(path: string) { return path.endsWith('/') ? path : path + '/'; } export function prependForwardSlash(path: string) { return path[0] === '/' ? path : '/' + path; } export function collapseDuplicateSlashes(path: string) { return path.replace(/(? { if (i === 0) { return removeTrailingForwardSlash(path); } else if (i === paths.length - 1) { return removeLeadingForwardSlash(path); } else { return trimSlashes(path); } }) .join('/'); } export function removeFileExtension(path: string) { let idx = path.lastIndexOf('.'); return idx === -1 ? path : path.slice(0, idx); } export function removeQueryString(path: string) { const index = path.lastIndexOf('?'); return index > 0 ? path.substring(0, index) : path; } export function isRemotePath(src: string) { return /^(http|ftp|https|ws):?\/\//.test(src) || src.startsWith('data:'); } export function slash(path: string) { return path.replace(/\\/g, '/'); }