UNPKG

@sitecore-jss/sitecore-jss

Version:

This module is provided as a part of Sitecore JavaScript Rendering SDK. It contains the core JSS APIs (layout service) and utilities.

44 lines (43 loc) 1.57 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.SITE_PREFIX = void 0; exports.getSiteRewrite = getSiteRewrite; exports.getSiteRewriteData = getSiteRewriteData; exports.normalizeSiteRewrite = normalizeSiteRewrite; exports.SITE_PREFIX = '_site_'; /** * Get a site rewrite path for given pathname * @param {string} pathname the pathname * @param {SiteRewriteData} data the site data to include in the rewrite * @returns {string} the rewrite path */ function getSiteRewrite(pathname, data) { const path = pathname.startsWith('/') ? pathname : '/' + pathname; return `/${exports.SITE_PREFIX}${data.siteName}${path}`; } /** * Get site data from the rewrite path * @param {string} pathname the pathname * @param {string} defaultSiteName the default site name * @returns {SiteRewriteData} the site data from the rewrite */ function getSiteRewriteData(pathname, defaultSiteName) { const data = { siteName: defaultSiteName, }; const path = pathname.endsWith('/') ? pathname : pathname + '/'; const result = path.match(`${exports.SITE_PREFIX}(.*?)\\/`); if (result && result[1] !== '') { data.siteName = result[1]; } return data; } /** * Normalize a site rewrite path (remove site data) * @param {string} pathname the pathname * @returns {string} the pathname with site data removed */ function normalizeSiteRewrite(pathname) { const result = pathname.match(`${exports.SITE_PREFIX}.*?(?:\\/|$)`); return result === null ? pathname : pathname.replace(result[0], ''); }