From ee90afb2dc44168a343e627a3c9cea1f369f8da8 Mon Sep 17 00:00:00 2001 From: Yassine Doghri Date: Sun, 21 Aug 2022 11:37:44 +0000 Subject: [PATCH] fix(example): add isCurrentPath function comparing current url to localized path --- .../src/components/molecules/Navigation.astro | 13 ++++++++-- example/src/pages/en/about.astro | 24 ------------------- example/src/pages/en/index.astro | 19 --------------- src/utils.ts | 1 + 4 files changed, 12 insertions(+), 45 deletions(-) delete mode 100644 example/src/pages/en/about.astro delete mode 100644 example/src/pages/en/index.astro diff --git a/example/src/components/molecules/Navigation.astro b/example/src/components/molecules/Navigation.astro index 0e3843b..8f11411 100644 --- a/example/src/components/molecules/Navigation.astro +++ b/example/src/components/molecules/Navigation.astro @@ -3,7 +3,16 @@ import i18next, { t } from "i18next"; import { localizePath } from "astro-i18next"; const { ...attributes } = Astro.props; -const { pathname } = Astro.url; + +// TODO: add as a utility function for astro-i18next. (out of scope?) +const isCurrentPath = (path: string) => { + const { pathname } = Astro.url; + + const currentNormalizedPath = pathname.replace(/\/$/, ""); + path = path.replace(/\/$/, ""); + + return currentNormalizedPath === path; +}; const navigationItems = { "/": t("navigation.home"), @@ -15,7 +24,7 @@ const navigationItems = { {Object.keys(navigationItems).map((key) => { let className = "py-2 px-4 uppercase text-sm underline-offset-2 tracking-wider decoration-4 font-semibold"; - if (localizePath(key, i18next.language) === pathname) { + if (isCurrentPath(localizePath(key, i18next.language))) { className += " underline decoration-cyan-500"; } else { className += diff --git a/example/src/pages/en/about.astro b/example/src/pages/en/about.astro deleted file mode 100644 index 831ad79..0000000 --- a/example/src/pages/en/about.astro +++ /dev/null @@ -1,24 +0,0 @@ ---- -import { t, changeLanguage } from "i18next"; -import { Trans } from "astro-i18next/components"; -import BaseTemplate from "templates:BaseTemplate"; -import Link from "atoms:Link"; - -changeLanguage("en"); ---- - - -

- - Created with love ❤️, cheese 🧀 and baguettes 🥖 during the Astro hackathon - of April / May 2022 by @yassinedoghri - . - -

-
diff --git a/example/src/pages/en/index.astro b/example/src/pages/en/index.astro deleted file mode 100644 index 3db57c7..0000000 --- a/example/src/pages/en/index.astro +++ /dev/null @@ -1,19 +0,0 @@ ---- -import Link from "atoms:Link"; -import { t, changeLanguage } from "i18next"; -import { Trans } from "astro-i18next/components"; -import BaseTemplate from "templates:BaseTemplate"; - -changeLanguage("en"); ---- - - -

- - An astro integration - of i18next - and utility components to help you translate your astro websites! - -

-
diff --git a/src/utils.ts b/src/utils.ts index a510d06..f4769d1 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -8,6 +8,7 @@ import typescript from "@proload/plugin-typescript"; * Adapted from astro's tailwind integration: * https://github.com/withastro/astro/tree/main/packages/integrations/tailwind */ +/* istanbul ignore next */ export const getUserConfig = async ( root: URL, configPath?: string