summaryrefslogtreecommitdiff
path: root/src/components/Breadcrumbs.astro
diff options
context:
space:
mode:
authorJoe Carstairs <65492573+Sycamost@users.noreply.github.com>2023-08-13 09:07:24 +0100
committerJoe Carstairs <65492573+Sycamost@users.noreply.github.com>2023-08-13 09:07:24 +0100
commitc3f119d332dea3fc33494a44e077f00f6ad05eec (patch)
treef699eb6e4cc36d541dcae3ec3fff5485182546ab /src/components/Breadcrumbs.astro
parentc1c9f9a9f3004460787ca23d55ffdacec95c0aad (diff)
Adds breadcrumbs
Diffstat (limited to 'src/components/Breadcrumbs.astro')
-rw-r--r--src/components/Breadcrumbs.astro36
1 files changed, 36 insertions, 0 deletions
diff --git a/src/components/Breadcrumbs.astro b/src/components/Breadcrumbs.astro
new file mode 100644
index 0000000..d29acee
--- /dev/null
+++ b/src/components/Breadcrumbs.astro
@@ -0,0 +1,36 @@
+---
+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}`), '');
+console.log('Path without locale: %s', pathWithoutLocale);
+const slashIndices = findAllIndicesOf('/', pathWithoutLocale);
+console.log('Indices: (%s)', slashIndices.join(','));
+const parentRoutes = slashIndices.map((n) => pathWithoutLocale.slice(0, n + 1)).slice(0, -1);
+console.log('Parent routes: (%s)', parentRoutes.join(','));
+
+const t = translate(locale);
+---
+
+<ol class="breadcrumbs">
+ {
+ /* parentRoutes.length > 1 && */ parentRoutes.map((route) => {
+ return (
+ <li>
+ <a class="link" href={`/${locale}${route}`}>
+ {t(tBreadcrumbs, { key: route })}
+ </a>
+ </li>
+ );
+ })
+ }
+</ol>