summaryrefslogtreecommitdiff
path: root/src/components/Navbar/Breadcrumbs.astro
blob: f94987376f0f1e4d12976bdf5efd1672fae48d92 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
---
import findAllIndicesOf from '$lib/findAllIndicesOf';
import translate from '$i18n/translate';
import tBreadcrumbs from '$i18n/translations/components/breadcrumbs';
import type Locale from '$types/Locale';
import locales from '$i18n/locales';

const path = Astro.url.pathname.replace(/(?<!\/)$/, '/');
const localeInPath = new RegExp(`/(?<=^/)(${locales.join('|')})`);
const locale = path.match(localeInPath)?.[1] as Locale;
if (!locale) {
  return;
}
const pathWithoutLocale = path.replace(new RegExp(`^/${locale}`), '');
const slashIndices = findAllIndicesOf('/', pathWithoutLocale);
const parentRoutes = slashIndices.map((n) => pathWithoutLocale.slice(0, n + 1)).slice(0, -1);

const t = translate(locale);
---

<ol class="nav__breadcrumbs">
  {
    parentRoutes.map((route) => {
      return (
        <li>
          <a href={`/${locale}${route}`}>{t(tBreadcrumbs, { key: route })}</a>
        </li>
      );
    })
  }
</ol>