From 3f61ce1134d3e3fb42dc92840a88ef922ab0f196 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Moritz=20St=C3=BCckler?= Date: Tue, 17 Oct 2023 16:55:21 +0200 Subject: [PATCH] fix: link logic --- src/components/WideCardWrapper.astro | 2 +- src/layouts/AcademyContent.astro | 10 +++++----- src/layouts/AcademyIndex.astro | 5 ++--- src/layouts/BaseLayout.astro | 11 ++++------- src/layouts/RedirectToAcademyIndex.astro | 3 ++- src/pages/index.astro | 2 +- src/util/CommonUtils.ts | 12 +++++++----- src/util/LanguageUtils.ts | 13 +++++++++++-- 8 files changed, 33 insertions(+), 25 deletions(-) diff --git a/src/components/WideCardWrapper.astro b/src/components/WideCardWrapper.astro index f24892dd..9197a10e 100644 --- a/src/components/WideCardWrapper.astro +++ b/src/components/WideCardWrapper.astro @@ -3,7 +3,7 @@ import WideCardComponent from "./WideCard"; import CommonUtils from "@util/CommonUtils"; const { target, ...rest } = Astro.props; -const baseUrl = CommonUtils.getBaseUrl(true); +const baseUrl = CommonUtils.getBaseUrl(false); const prefixedTarget = target ? baseUrl + target : undefined; --- diff --git a/src/layouts/AcademyContent.astro b/src/layouts/AcademyContent.astro index 5d359ea9..3be1a026 100644 --- a/src/layouts/AcademyContent.astro +++ b/src/layouts/AcademyContent.astro @@ -11,24 +11,24 @@ import { getParentPage } from "@util/ContentTransformer"; import fs from "node:fs"; import Giscus from "@components/Giscus.astro"; import CommonUtils from "@util/CommonUtils"; +import LanguageUtils from "@util/LanguageUtils"; import FooterCards from "@components/FooterCards.astro"; const rawAcademyContent = await Astro.glob( "../pages/en/academy/*/*.mdx" ); -const baseUrl = CommonUtils.getBaseUrl(false); const { frontmatter, url, file, headings } = Astro.props; +const baseUrl = CommonUtils.getBaseUrl(true); +const localeFromUrl = LanguageUtils.getLocaleFromUrl(url, baseUrl); + + const statsMtime = fs.statSync(file).mtime; const editDate = new Date(statsMtime); const parentPage = getParentPage(rawAcademyContent, url); -const localeFromUrl = - Astro?.url?.pathname.replace(baseUrl, "").split("/").filter(Boolean)[0] || - "en"; - --- diff --git a/src/layouts/AcademyIndex.astro b/src/layouts/AcademyIndex.astro index 99947a7b..5ba5b88c 100644 --- a/src/layouts/AcademyIndex.astro +++ b/src/layouts/AcademyIndex.astro @@ -3,13 +3,12 @@ import FooterCards from "@components/FooterCards.astro"; import BaseLayout from "./BaseLayout.astro"; import { ContentSection } from "@components"; import CommonUtils from "@util/CommonUtils"; +import LanguageUtils from "@util/LanguageUtils"; const { frontmatter } = Astro.props; const baseUrl = CommonUtils.getBaseUrl(false); -const localeFromUrl = - Astro?.url?.pathname.replace(baseUrl, "").split("/").filter(Boolean)[0] || - "en"; +const localeFromUrl = LanguageUtils.getLocaleFromUrl(Astro.url, baseUrl); --- diff --git a/src/layouts/BaseLayout.astro b/src/layouts/BaseLayout.astro index 3ce03e42..aa5765e1 100644 --- a/src/layouts/BaseLayout.astro +++ b/src/layouts/BaseLayout.astro @@ -2,6 +2,7 @@ import type { MarkdownLayoutProps } from "astro"; import { Navigation, Footer } from "@components"; import CommonUtils from "@util/CommonUtils"; +import LanguageUtils from "@util/LanguageUtils"; import type { IMenuMarkdown } from "@interfaces/IMenu"; import "../../base.css"; @@ -13,13 +14,9 @@ type Props = MarkdownLayoutProps<{ const { frontmatter } = Astro.props; const path = Astro.url.pathname; -const baseUrl = CommonUtils.getBaseUrl(false); +const baseUrl = CommonUtils.getBaseUrl(true); -const localeFromUrl = Astro?.url?.pathname - .replace(baseUrl, "") - .split("/") - .filter(Boolean)[0] - .toLowerCase(); +const localeFromUrl = LanguageUtils.getLocaleFromUrl(Astro.url, baseUrl); const rawMenus = await Astro.glob("../pages/**/_menu.mdx"); @@ -42,6 +39,7 @@ const isProduction = import.meta.env.MODE === "production"; + @@ -61,7 +59,6 @@ const isProduction = import.meta.env.MODE === "production"; /> ) } - {frontmatter?.title} - Tolocar Project diff --git a/src/layouts/RedirectToAcademyIndex.astro b/src/layouts/RedirectToAcademyIndex.astro index 7be60a34..52e1dfa2 100644 --- a/src/layouts/RedirectToAcademyIndex.astro +++ b/src/layouts/RedirectToAcademyIndex.astro @@ -1,8 +1,9 @@ --- +import CommonUtils from "@util/CommonUtils"; import GithubSlugger from "github-slugger"; const { frontmatter } = Astro.props; -const baseUrl = import.meta.env.BASE_URL; +const baseUrl = CommonUtils.getBaseUrl(true); const slugger = new GithubSlugger(); const targetSlug = slugger.slug(frontmatter.title); const targetUrl = baseUrl + "academy/#" + targetSlug; diff --git a/src/pages/index.astro b/src/pages/index.astro index 6945793b..3c2d7c25 100644 --- a/src/pages/index.astro +++ b/src/pages/index.astro @@ -2,7 +2,7 @@ import LanguageUtils from "@util/LanguageUtils"; import CommonUtils from "@util/CommonUtils"; // Redirect user to previously set language preference (or get it from the browser) - const baseUrl = CommonUtils.getBaseUrl(true); + const baseUrl = CommonUtils.getBaseUrl(false); const preferredLanguage = LanguageUtils.getOrGuessInitialLanguageFromBrowser(); window.location.pathname = `${baseUrl}/${preferredLanguage.toLowerCase()}`; diff --git a/src/util/CommonUtils.ts b/src/util/CommonUtils.ts index 144f80e3..0fc2dd97 100644 --- a/src/util/CommonUtils.ts +++ b/src/util/CommonUtils.ts @@ -2,13 +2,15 @@ const CommonUtils = { /** * Return the base URL of the current page with or without trailing slash depending on the given parameter */ - getBaseUrl: (removeTrailingSlash?: boolean): string => { + getBaseUrl: (trailingSlash: boolean = false): string => { const baseUrl = import.meta.env.BASE_URL; - if (removeTrailingSlash) { - return baseUrl.endsWith("/") ? baseUrl.slice(0, -1) : baseUrl; - } + const endsWithSlash = baseUrl.endsWith("/"); + // Add slash + if (!endsWithSlash && trailingSlash) return baseUrl + "/"; + // Remove slash + else if (endsWithSlash && !trailingSlash) return baseUrl.slice(0, -1); return baseUrl; - } + }, }; export default CommonUtils; diff --git a/src/util/LanguageUtils.ts b/src/util/LanguageUtils.ts index 4cd5852b..52b992da 100644 --- a/src/util/LanguageUtils.ts +++ b/src/util/LanguageUtils.ts @@ -24,7 +24,16 @@ export const LanguageUtils = { }, setLanguage: function (newLanguage: string) { localStorage.setItem(STORAGE_KEY, newLanguage); - } + }, + getLocaleFromUrl: function (url: URL, baseUrl: string) { + return ( + url?.pathname + .replace(baseUrl, "") + .split("/") + .filter(Boolean)[0] + .toLowerCase() || "en" + ); + }, }; -export default LanguageUtils; \ No newline at end of file +export default LanguageUtils;