config.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.resolveSiteDataByRoute = void 0;
  4. const inBrowser = typeof window !== 'undefined';
  5. function findMatchRoot(route, roots) {
  6. // first match to the routes with the most deep level.
  7. roots.sort((a, b) => {
  8. const levelDelta = b.split('/').length - a.split('/').length;
  9. if (levelDelta !== 0) {
  10. return levelDelta;
  11. }
  12. else {
  13. return b.length - a.length;
  14. }
  15. });
  16. for (const r of roots) {
  17. if (route.startsWith(r))
  18. return r;
  19. }
  20. return undefined;
  21. }
  22. function resolveLocales(locales, route) {
  23. const localeRoot = findMatchRoot(route, Object.keys(locales));
  24. return localeRoot ? locales[localeRoot] : undefined;
  25. }
  26. // this merges the locales data to the main data by the route
  27. function resolveSiteDataByRoute(siteData, route) {
  28. route = cleanRoute(siteData, route);
  29. const localeData = resolveLocales(siteData.locales || {}, route) || {};
  30. const localeThemeConfig = resolveLocales((siteData.themeConfig && siteData.themeConfig.locales) || {}, route) || {};
  31. return {
  32. ...siteData,
  33. ...localeData,
  34. themeConfig: {
  35. ...siteData.themeConfig,
  36. ...localeThemeConfig,
  37. // clean the locales to reduce the bundle size
  38. locales: {}
  39. },
  40. locales: {}
  41. };
  42. }
  43. exports.resolveSiteDataByRoute = resolveSiteDataByRoute;
  44. /**
  45. * Clean up the route by removing the `base` path if it's set in config.
  46. */
  47. function cleanRoute(siteData, route) {
  48. if (!inBrowser) {
  49. return route;
  50. }
  51. const base = siteData.base;
  52. const baseWithoutSuffix = base.endsWith('/') ? base.slice(0, -1) : base;
  53. return route.slice(baseWithoutSuffix.length);
  54. }
  55. //# sourceMappingURL=config.js.map