@redocly/theme
Version:
Shared UI components lib
119 lines • 4.5 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.combineUrls = combineUrls;
exports.withPathPrefix = withPathPrefix;
exports.withoutPathPrefix = withoutPathPrefix;
exports.withoutHash = withoutHash;
exports.getPathPrefix = getPathPrefix;
exports.normalizePathPrefix = normalizePathPrefix;
exports.addLeadingSlash = addLeadingSlash;
exports.removeTrailingSlash = removeTrailingSlash;
exports.removeLeadingSlash = removeLeadingSlash;
exports.isPathInFolder = isPathInFolder;
exports.slash = slash;
exports.addTrailingSlash = addTrailingSlash;
exports.getPathnameForLocale = getPathnameForLocale;
exports.getLocaleFromPathname = getLocaleFromPathname;
const common_1 = require("../constants/common");
function combineUrls(baseURL, ...relativeURLs) {
let res = baseURL;
for (let relativeURL of relativeURLs) {
res = relativeURL
? res.replace(/[\/\\]+$/, '') + '/' + relativeURL.replace(/^[\/\\]+/, '')
: res;
}
return res;
}
function withPathPrefix(url) {
return combineUrls(getPathPrefix(), url);
}
function withoutPathPrefix(pathname) {
const pathPrefix = getPathPrefix();
if (!pathPrefix) {
return pathname;
}
if (pathPrefix === pathname) {
return '/';
}
return pathname.startsWith(pathPrefix) ? pathname.slice(pathPrefix.length) : pathname;
}
function withoutHash(url) {
if (url == null)
return undefined;
return url.split('#')[0];
}
/**
*
* @returns url with leading and without trailing slash or empty string, e.g. '/prefix'
*/
function getPathPrefix() {
if (process.env.REDOCLY_PREFIX_PATHS) {
return normalizePathPrefix(process.env.REDOCLY_PREFIX_PATHS);
}
return '';
}
function normalizePathPrefix(prefix) {
const withoutTrailing = removeTrailingSlash(prefix);
return addLeadingSlash(withoutTrailing === '.' ? '' : withoutTrailing);
}
function addLeadingSlash(url) {
return url.startsWith('/') ? url : `/${url}`;
}
function removeTrailingSlash(url) {
return url.endsWith('/') && url !== '/' ? url.substring(0, url.length - 1) : url;
}
function removeLeadingSlash(url) {
return url.startsWith('/') ? url.substring(1) : url;
}
function isPathInFolder(child, parent) {
parent = removeTrailingSlash(removeLeadingSlash(slash(parent)));
child = removeTrailingSlash(removeLeadingSlash(slash(child)));
return child === parent || child.startsWith(parent + '/');
}
/**
* Convert Windows backslash paths to slash paths: foo\\bar ➔ foo/bar
*
* @param path
* @return slashed path
*/
function slash(path) {
const isExtendedLengthPath = /^\\\\\?\\/.test(path);
if (isExtendedLengthPath) {
return path;
}
return path.replace(/\\/g, `/`);
}
function addTrailingSlash(url) {
return url.endsWith('/') ? url : `${url}/`;
}
/**
* Adds locale to pathname, or replaces current locale in pathname with a new one
* @param originalPathname - Pathname without path prefix
* @param defaultLocale - Default locale code
* @param newLocale - New locale code to apply
* @param allLocales - Array of all available locales
*/
function getPathnameForLocale(originalPathname, defaultLocale, newLocale, allLocales) {
const currentLocale = getLocaleFromPathname(originalPathname, defaultLocale, allLocales);
if (currentLocale === newLocale) {
return originalPathname;
}
const pathnameWithoutLocale = currentLocale === defaultLocale
? originalPathname
: originalPathname.slice(currentLocale.length + 1);
const newLocalePrefix = newLocale === defaultLocale ? '' : '/' + newLocale;
return `${newLocalePrefix.toLowerCase()}${pathnameWithoutLocale}`;
}
/**
* Extracts the locale code from a pathname
* @param pathname - URL pathname to extract locale from without path prefix
* @param defaultLocale - Default locale code to return if no locale found in pathname
* @param allLocales - Array of all available locales to check against
* @returns The locale code from the pathname, or the default locale if none found
*/
function getLocaleFromPathname(pathname, defaultLocale = common_1.DEFAULT_LOCALE_PLACEHOLDER, allLocales = []) {
const maybeLocale = pathname === null || pathname === void 0 ? void 0 : pathname.split('/')[1];
const locale = allLocales.find((locale) => locale.code.toLowerCase() === maybeLocale);
return (locale === null || locale === void 0 ? void 0 : locale.code) || defaultLocale;
}
//# sourceMappingURL=urls.js.map