From c3f119d332dea3fc33494a44e077f00f6ad05eec Mon Sep 17 00:00:00 2001 From: Joe Carstairs <65492573+Sycamost@users.noreply.github.com> Date: Sun, 13 Aug 2023 09:07:24 +0100 Subject: Adds breadcrumbs --- src/components/Breadcrumbs.astro | 36 +++++++++++++++++++++++++++++++++ src/components/Navbar.astro | 2 ++ src/components/SwitchLanguageLink.astro | 2 +- 3 files changed, 39 insertions(+), 1 deletion(-) create mode 100644 src/components/Breadcrumbs.astro (limited to 'src/components') 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); +--- + + diff --git a/src/components/Navbar.astro b/src/components/Navbar.astro index 8174efe..80829d3 100644 --- a/src/components/Navbar.astro +++ b/src/components/Navbar.astro @@ -1,5 +1,6 @@ --- import type Locale from '../types/Locale'; +import Breadcrumbs from './Breadcrumbs.astro'; import SwitchLanguageLink from './SwitchLanguageLink.astro'; interface Props { @@ -11,4 +12,5 @@ const { locale } = Astro.props; diff --git a/src/components/SwitchLanguageLink.astro b/src/components/SwitchLanguageLink.astro index 77dfa18..e8dace3 100644 --- a/src/components/SwitchLanguageLink.astro +++ b/src/components/SwitchLanguageLink.astro @@ -18,7 +18,7 @@ const otherLocalePath = currentPath.replace(currentLocaleInPath, `${otherLocale} const t = translate(currentLocale); --- - + { t(tPage, { key: 'language-link-text', -- cgit v1.2.3