Skip to content

Commit

Permalink
fix: link logic
Browse files Browse the repository at this point in the history
  • Loading branch information
pReya committed Oct 17, 2023
1 parent dafe1f4 commit 3f61ce1
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 25 deletions.
2 changes: 1 addition & 1 deletion src/components/WideCardWrapper.astro
Original file line number Diff line number Diff line change
Expand Up @@ -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;
---
Expand Down
10 changes: 5 additions & 5 deletions src/layouts/AcademyContent.astro
Original file line number Diff line number Diff line change
Expand Up @@ -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<AcademyPageFrontmatter>(
"../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";
---

<BaseLayout frontmatter={frontmatter}>
Expand Down
5 changes: 2 additions & 3 deletions src/layouts/AcademyIndex.astro
Original file line number Diff line number Diff line change
Expand Up @@ -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);
---

<BaseLayout footerGrey={true} frontmatter={frontmatter}>
Expand Down
11 changes: 4 additions & 7 deletions src/layouts/BaseLayout.astro
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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<IMenuMarkdown>("../pages/**/_menu.mdx");
Expand All @@ -42,6 +39,7 @@ const isProduction = import.meta.env.MODE === "production";
<!doctype html>
<html lang={localeFromUrl} class="scroll-smooth h-full">
<head>
<base href={baseUrl} />
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="icon" type="image/png" href={"/favicon.png"} />
Expand All @@ -61,7 +59,6 @@ const isProduction = import.meta.env.MODE === "production";
/>
)
}
<base href={baseUrl} />
<title>{frontmatter?.title} - Tolocar Project</title>
</head>
<body class="w-full font-inter antialiased h-full tracking-[-0.02em]">
Expand Down
3 changes: 2 additions & 1 deletion src/layouts/RedirectToAcademyIndex.astro
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/pages/index.astro
Original file line number Diff line number Diff line change
Expand Up @@ -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()}`;
Expand Down
12 changes: 7 additions & 5 deletions src/util/CommonUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
13 changes: 11 additions & 2 deletions src/util/LanguageUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
export default LanguageUtils;

0 comments on commit 3f61ce1

Please sign in to comment.