@knightly/vitepress
Version:
Vite & Vue powered static site generator
70 lines • 2.63 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.resolveSiteDataByRoute = exports.createLangDictionary = exports.inBrowser = exports.EXTERNAL_URL_RE = void 0;
exports.EXTERNAL_URL_RE = /^https?:/i;
exports.inBrowser = typeof window !== 'undefined';
function findMatchRoot(route, roots) {
// first match to the routes with the most deep level.
roots.sort((a, b) => {
const levelDelta = b.split('/').length - a.split('/').length;
if (levelDelta !== 0) {
return levelDelta;
}
else {
return b.length - a.length;
}
});
for (const r of roots) {
if (route.startsWith(r))
return r;
}
}
function resolveLocales(locales, route) {
const localeRoot = findMatchRoot(route, Object.keys(locales));
return localeRoot ? locales[localeRoot] : undefined;
}
function createLangDictionary(siteData) {
const { locales } = siteData.themeConfig || {};
const siteLocales = siteData.locales;
return locales && siteLocales
? Object.keys(locales).reduce((langs, path) => {
langs[path] = {
label: locales[path].label,
lang: siteLocales[path].lang
};
return langs;
}, {})
: {};
}
exports.createLangDictionary = createLangDictionary;
// this merges the locales data to the main data by the route
function resolveSiteDataByRoute(siteData, route) {
route = cleanRoute(siteData, route);
const localeData = resolveLocales(siteData.locales || {}, route);
const localeThemeConfig = resolveLocales(siteData.themeConfig.locales || {}, route);
// avoid object rest spread since this is going to run in the browser
// and spread is going to result in polyfill code
return Object.assign({}, siteData, localeData, {
themeConfig: Object.assign({}, siteData.themeConfig, localeThemeConfig, {
// clean the locales to reduce the bundle size
locales: {}
}),
lang: (localeData || siteData).lang,
// clean the locales to reduce the bundle size
locales: {},
langs: createLangDictionary(siteData)
});
}
exports.resolveSiteDataByRoute = resolveSiteDataByRoute;
/**
* Clean up the route by removing the `base` path if it's set in config.
*/
function cleanRoute(siteData, route) {
if (!exports.inBrowser) {
return route;
}
const base = siteData.base;
const baseWithoutSuffix = base.endsWith('/') ? base.slice(0, -1) : base;
return route.slice(baseWithoutSuffix.length);
}
//# sourceMappingURL=shared.js.map