@withstudiocms/internal_helpers
Version:
Internal helper utilities for StudioCMS
67 lines (66 loc) • 1.88 kB
JavaScript
function pathWithBase(path) {
let newPath = path;
newPath = stripLeadingSlash(newPath);
return newPath ? `/${newPath}` : "/";
}
function fileWithBase(path) {
let newPath = path;
newPath = stripLeadingSlash(newPath);
return newPath ? `/${newPath}` : "/";
}
function ensureLeadingSlash(href) {
let newHref = href;
if (newHref[0] !== "/") newHref = `/${newHref}`;
return newHref;
}
function ensureTrailingSlash(href) {
let newHref = href;
if (newHref[newHref.length - 1] !== "/") newHref += "/";
return newHref;
}
function ensureLeadingAndTrailingSlashes(href) {
let newHref = href;
newHref = ensureLeadingSlash(newHref);
newHref = ensureTrailingSlash(newHref);
return newHref;
}
function stripLeadingSlash(href) {
let newHref = href;
if (newHref[0] === "/") newHref = newHref.slice(1);
return newHref;
}
function stripTrailingSlash(href) {
let newHref = href;
if (newHref[newHref.length - 1] === "/") newHref = newHref.slice(0, -1);
return newHref;
}
function stripLeadingAndTrailingSlashes(href) {
let newHref = href;
newHref = stripLeadingSlash(newHref);
newHref = stripTrailingSlash(newHref);
return newHref;
}
function stripHtmlExtension(path) {
const pathWithoutTrailingSlash = stripTrailingSlash(path);
return pathWithoutTrailingSlash.endsWith(".html") ? pathWithoutTrailingSlash.slice(0, -5) : pathWithoutTrailingSlash;
}
function ensureHtmlExtension(path) {
let newPath = path;
newPath = stripLeadingAndTrailingSlashes(newPath);
if (!newPath.endsWith(".html")) {
newPath = newPath ? `${newPath}.html` : "/index.html";
}
return ensureLeadingSlash(newPath);
}
export {
ensureHtmlExtension,
ensureLeadingAndTrailingSlashes,
ensureLeadingSlash,
ensureTrailingSlash,
fileWithBase,
pathWithBase,
stripHtmlExtension,
stripLeadingAndTrailingSlashes,
stripLeadingSlash,
stripTrailingSlash
};