From a77cd5d5c9e5f88fcd6260eb9c8272af6e48b0c8 Mon Sep 17 00:00:00 2001 From: GauthierWebDev Date: Fri, 18 Apr 2025 17:29:58 +0200 Subject: [PATCH 01/10] feat: Add sitemap generation feature --- app/fastify-entry.ts | 3 ++ app/public/sitemap.xml | 1 + app/services/Sitemap.ts | 77 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 81 insertions(+) create mode 100644 app/public/sitemap.xml create mode 100644 app/services/Sitemap.ts diff --git a/app/fastify-entry.ts b/app/fastify-entry.ts index 2127835..dcbac11 100644 --- a/app/fastify-entry.ts +++ b/app/fastify-entry.ts @@ -3,6 +3,7 @@ import type { Theme } from "@/contexts/ThemeContext"; import { createHandler } from "@universal-middleware/fastify"; import { telefuncHandler } from "./server/telefunc-handler"; import { vikeHandler } from "./server/vike-handler"; +import { sitemap } from "./services/Sitemap"; import fastifyCookie from "@fastify/cookie"; import { fileURLToPath } from "node:url"; import { dirname } from "node:path"; @@ -34,6 +35,8 @@ declare global { async function startServer() { const app = Fastify(); + sitemap.generateSitemap(); + app.register(fastifyCookie, { secret: "todo", hook: "onRequest", diff --git a/app/public/sitemap.xml b/app/public/sitemap.xml new file mode 100644 index 0000000..edf58cf --- /dev/null +++ b/app/public/sitemap.xml @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/app/services/Sitemap.ts b/app/services/Sitemap.ts new file mode 100644 index 0000000..ad2e69e --- /dev/null +++ b/app/services/Sitemap.ts @@ -0,0 +1,77 @@ +import { navigation } from "@/lib/navigation"; +import path from "path"; +import fs from "fs"; + +const __dirname = path.resolve(); + +const getBaseUrl = () => { + if (process.env.NODE_ENV === "production") { + return "https://memento-dev.fr"; + } + + return `http://localhost:${process.env.PORT || 3000}`; +}; + +type SitemapElement = { + location: string; + lastmod: string; + priority: string; +}; + +class Sitemap { + private readonly sitemapPath = path.join(__dirname, "public", "sitemap.xml"); + private readonly lastModified = new Date().toISOString(); + private readonly baseUrl = getBaseUrl(); + + private urls: SitemapElement[] = []; + private sitemap: string = ""; + + private static instance: Sitemap; + + private constructor() {} + + public static getInstance(): Sitemap { + if (!Sitemap.instance) { + Sitemap.instance = new Sitemap(); + } + return Sitemap.instance; + } + + private resetMemory(): void { + this.sitemap = ""; + this.urls = []; + } + + private prependSitemap(): void { + this.sitemap = ``; + this.sitemap += ``; + } + + private appendSitemap(): void { + this.sitemap += ``; + } + + private buildSitemap(): void { + this.prependSitemap(); + this.appendSitemap(); + } + + private saveSitemap(): void { + fs.writeFileSync(this.sitemapPath, this.sitemap, "utf8"); + } + + private loadUrls(): void {} + + public generateSitemap(): void { + console.log("Generating sitemap..."); + + this.resetMemory(); + this.loadUrls(); + this.buildSitemap(); + this.saveSitemap(); + + console.log("Sitemap generated successfully."); + } +} + +export const sitemap = Sitemap.getInstance(); -- 2.45.2 From 3a84a78c435b1851f909df36a44c66ee7c424ce8 Mon Sep 17 00:00:00 2001 From: GauthierWebDev Date: Fri, 18 Apr 2025 17:43:25 +0200 Subject: [PATCH 02/10] feat: Add new script for generating sitemap --- app/package.json | 1 + app/services/Sitemap.ts | 40 +++++++++++++++++++++++++++++++++++++++- app/sitemap.ts | 3 +++ 3 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 app/sitemap.ts diff --git a/app/package.json b/app/package.json index a6e5eba..58e2aca 100644 --- a/app/package.json +++ b/app/package.json @@ -1,6 +1,7 @@ { "scripts": { "dev": "tsx ./fastify-entry.ts", + "dev:sitemap": "tsx --watch ./sitemap.ts", "build": "vike build", "preview": "cross-env NODE_ENV=production tsx ./fastify-entry.ts", "lint": "eslint .", diff --git a/app/services/Sitemap.ts b/app/services/Sitemap.ts index ad2e69e..908424e 100644 --- a/app/services/Sitemap.ts +++ b/app/services/Sitemap.ts @@ -19,6 +19,8 @@ type SitemapElement = { }; class Sitemap { + private readonly pagesPath = path.join(__dirname, "pages"); + private readonly dataPath = path.join(__dirname, "data"); private readonly sitemapPath = path.join(__dirname, "public", "sitemap.xml"); private readonly lastModified = new Date().toISOString(); private readonly baseUrl = getBaseUrl(); @@ -51,8 +53,11 @@ class Sitemap { this.sitemap += ``; } + private addSitemapElement(url: SitemapElement): void {} + private buildSitemap(): void { this.prependSitemap(); + this.urls.forEach(this.addSitemapElement.bind(this)); this.appendSitemap(); } @@ -60,7 +65,40 @@ class Sitemap { fs.writeFileSync(this.sitemapPath, this.sitemap, "utf8"); } - private loadUrls(): void {} + private loadPriority(href: string): string { + const isRootUrl = ["/", ""].includes(href); + const isMainUrl = ["/docs", "/certifications", "/politique-de-confidentialite", "/mentions-legales"].includes(href); + + if (isRootUrl) return "1.0"; + if (isMainUrl) return "0.9"; + return "0.8"; + } + + private loadLastModified(href: string): string {} + + private loadFile(href: string) {} + + private loadUrls(): void { + this.urls = navigation.flatMap((item) => { + return item.links + .map((link) => { + const file = this.loadFile(link.href); + if (!file) { + console.warn(`File not found for URL: ${link.href}`); + return null; + } + + return { + location: `${this.baseUrl}${link.href}`, + lastmod: this.loadLastModified(link.href), + priority: this.loadPriority(link.href), + }; + }) + .filter((url) => url !== null); + }); + + console.log("Loaded URLs:", this.urls); + } public generateSitemap(): void { console.log("Generating sitemap..."); diff --git a/app/sitemap.ts b/app/sitemap.ts new file mode 100644 index 0000000..e31308f --- /dev/null +++ b/app/sitemap.ts @@ -0,0 +1,3 @@ +import { sitemap } from "./services/Sitemap"; + +sitemap.generateSitemap(); -- 2.45.2 From 4a4d867ab9af7fc157d39acf9185006d5618e17e Mon Sep 17 00:00:00 2001 From: GauthierWebDev Date: Fri, 18 Apr 2025 17:50:32 +0200 Subject: [PATCH 03/10] feat: Update sitemap with additional URLs and sitemap logic --- app/public/sitemap.xml | 2 +- app/services/Sitemap.ts | 52 +++++++++++++++++++++++++---------------- 2 files changed, 33 insertions(+), 21 deletions(-) diff --git a/app/public/sitemap.xml b/app/public/sitemap.xml index edf58cf..085453d 100644 --- a/app/public/sitemap.xml +++ b/app/public/sitemap.xml @@ -1 +1 @@ - \ No newline at end of file +http://localhost:5500/2025-04-18T15:49:26.620Z1.0http://localhost:5500/certifications2025-04-18T15:49:26.620Z0.9http://localhost:5500/docs2025-04-18T15:49:26.620Z0.9http://localhost:5500/docs/communaute/influenceurs2025-04-18T15:49:26.620Z0.7http://localhost:5500/docs/communaute/partages2025-04-18T15:49:26.620Z0.7http://localhost:5500/mentions-legales2025-04-18T15:49:26.620Z0.9http://localhost:5500/politique-de-confidentialite2025-04-18T15:49:26.620Z0.9http://localhost:5500/certifications/dwwm2025-04-18T15:49:26.620Z0.8http://localhost:5500/certifications/dwwm/at12025-04-18T15:49:26.620Z0.7http://localhost:5500/certifications/dwwm/at22025-04-18T15:49:26.620Z0.7http://localhost:5500/docs/react2025-04-18T15:49:26.620Z0.8http://localhost:5500/docs/merise2025-04-18T15:49:26.620Z0.8 \ No newline at end of file diff --git a/app/services/Sitemap.ts b/app/services/Sitemap.ts index 908424e..e749d6b 100644 --- a/app/services/Sitemap.ts +++ b/app/services/Sitemap.ts @@ -53,7 +53,13 @@ class Sitemap { this.sitemap += ``; } - private addSitemapElement(url: SitemapElement): void {} + private addSitemapElement(url: SitemapElement): void { + this.sitemap += ``; + this.sitemap += `${url.location}`; + this.sitemap += `${url.lastmod || this.lastModified}`; + this.sitemap += `${url.priority}`; + this.sitemap += ``; + } private buildSitemap(): void { this.prependSitemap(); @@ -67,34 +73,40 @@ class Sitemap { private loadPriority(href: string): string { const isRootUrl = ["/", ""].includes(href); - const isMainUrl = ["/docs", "/certifications", "/politique-de-confidentialite", "/mentions-legales"].includes(href); if (isRootUrl) return "1.0"; - if (isMainUrl) return "0.9"; - return "0.8"; + const countOfSlashes = (href.match(/\//g) || []).length; + return (1 - countOfSlashes * 0.1).toFixed(1); } private loadLastModified(href: string): string {} private loadFile(href: string) {} - private loadUrls(): void { - this.urls = navigation.flatMap((item) => { - return item.links - .map((link) => { - const file = this.loadFile(link.href); - if (!file) { - console.warn(`File not found for URL: ${link.href}`); - return null; - } + private loadSection(sectionLinks: (typeof navigation)[number]["links"]) { + return sectionLinks.map((link) => { + const href = link.href; + const priority = this.loadPriority(href); + const lastmod = this.loadLastModified(href); + const location = `${this.baseUrl}${href}`; - return { - location: `${this.baseUrl}${link.href}`, - lastmod: this.loadLastModified(link.href), - priority: this.loadPriority(link.href), - }; - }) - .filter((url) => url !== null); + return { + location, + lastmod, + priority, + }; + }); + } + + private loadUrls(): void { + this.urls = navigation.flatMap((section) => { + return Array.from( + new Set( + this.loadSection(section.links) + .filter((url) => url !== null) + .sort((a, b) => a.location.localeCompare(b.location)), + ), + ); }); console.log("Loaded URLs:", this.urls); -- 2.45.2 From 42089a1b7b68ac8546e557ef19d69ffc1debf260 Mon Sep 17 00:00:00 2001 From: GauthierWebDev Date: Fri, 18 Apr 2025 17:51:45 +0200 Subject: [PATCH 04/10] refactor: Update variable usage in Sitemap.ts --- app/public/sitemap.xml | 2 +- app/services/Sitemap.ts | 7 +++---- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/app/public/sitemap.xml b/app/public/sitemap.xml index 085453d..9295e87 100644 --- a/app/public/sitemap.xml +++ b/app/public/sitemap.xml @@ -1 +1 @@ -http://localhost:5500/2025-04-18T15:49:26.620Z1.0http://localhost:5500/certifications2025-04-18T15:49:26.620Z0.9http://localhost:5500/docs2025-04-18T15:49:26.620Z0.9http://localhost:5500/docs/communaute/influenceurs2025-04-18T15:49:26.620Z0.7http://localhost:5500/docs/communaute/partages2025-04-18T15:49:26.620Z0.7http://localhost:5500/mentions-legales2025-04-18T15:49:26.620Z0.9http://localhost:5500/politique-de-confidentialite2025-04-18T15:49:26.620Z0.9http://localhost:5500/certifications/dwwm2025-04-18T15:49:26.620Z0.8http://localhost:5500/certifications/dwwm/at12025-04-18T15:49:26.620Z0.7http://localhost:5500/certifications/dwwm/at22025-04-18T15:49:26.620Z0.7http://localhost:5500/docs/react2025-04-18T15:49:26.620Z0.8http://localhost:5500/docs/merise2025-04-18T15:49:26.620Z0.8 \ No newline at end of file +http://localhost:5500/2025-04-18T15:51:43.576Z1.0http://localhost:5500/certifications2025-04-18T15:51:43.576Z0.9http://localhost:5500/docs2025-04-18T15:51:43.576Z0.9http://localhost:5500/docs/communaute/influenceurs2025-04-18T15:51:43.576Z0.7http://localhost:5500/docs/communaute/partages2025-04-18T15:51:43.576Z0.7http://localhost:5500/mentions-legales2025-04-18T15:51:43.576Z0.9http://localhost:5500/politique-de-confidentialite2025-04-18T15:51:43.576Z0.9http://localhost:5500/certifications/dwwm2025-04-18T15:51:43.576Z0.8http://localhost:5500/certifications/dwwm/at12025-04-18T15:51:43.576Z0.7http://localhost:5500/certifications/dwwm/at22025-04-18T15:51:43.576Z0.7http://localhost:5500/docs/react2025-04-18T15:51:43.576Z0.8http://localhost:5500/docs/merise2025-04-18T15:51:43.576Z0.8 \ No newline at end of file diff --git a/app/services/Sitemap.ts b/app/services/Sitemap.ts index e749d6b..ff2a3dc 100644 --- a/app/services/Sitemap.ts +++ b/app/services/Sitemap.ts @@ -85,10 +85,9 @@ class Sitemap { private loadSection(sectionLinks: (typeof navigation)[number]["links"]) { return sectionLinks.map((link) => { - const href = link.href; - const priority = this.loadPriority(href); - const lastmod = this.loadLastModified(href); - const location = `${this.baseUrl}${href}`; + const priority = this.loadPriority(link.href); + const lastmod = this.loadLastModified(link.href); + const location = `${this.baseUrl}${link.href}`; return { location, -- 2.45.2 From 9aeaec0821bfcdf7ace73cbf4020f6955412ee2d Mon Sep 17 00:00:00 2001 From: GauthierWebDev Date: Fri, 18 Apr 2025 18:17:47 +0200 Subject: [PATCH 05/10] refactor: Improve loadSubitems method in Sitemap class --- .gitignore | 1 + app/public/sitemap.xml | 1 - app/services/Sitemap.ts | 56 ++++++++++++++++++++++++++++++----------- 3 files changed, 43 insertions(+), 15 deletions(-) delete mode 100644 app/public/sitemap.xml diff --git a/.gitignore b/.gitignore index d8fc90b..68d7568 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,5 @@ app/.pnpm-store app/node_modules app/dist +app/public/sitemap.xml **/*~lock* \ No newline at end of file diff --git a/app/public/sitemap.xml b/app/public/sitemap.xml deleted file mode 100644 index 9295e87..0000000 --- a/app/public/sitemap.xml +++ /dev/null @@ -1 +0,0 @@ -http://localhost:5500/2025-04-18T15:51:43.576Z1.0http://localhost:5500/certifications2025-04-18T15:51:43.576Z0.9http://localhost:5500/docs2025-04-18T15:51:43.576Z0.9http://localhost:5500/docs/communaute/influenceurs2025-04-18T15:51:43.576Z0.7http://localhost:5500/docs/communaute/partages2025-04-18T15:51:43.576Z0.7http://localhost:5500/mentions-legales2025-04-18T15:51:43.576Z0.9http://localhost:5500/politique-de-confidentialite2025-04-18T15:51:43.576Z0.9http://localhost:5500/certifications/dwwm2025-04-18T15:51:43.576Z0.8http://localhost:5500/certifications/dwwm/at12025-04-18T15:51:43.576Z0.7http://localhost:5500/certifications/dwwm/at22025-04-18T15:51:43.576Z0.7http://localhost:5500/docs/react2025-04-18T15:51:43.576Z0.8http://localhost:5500/docs/merise2025-04-18T15:51:43.576Z0.8 \ No newline at end of file diff --git a/app/services/Sitemap.ts b/app/services/Sitemap.ts index ff2a3dc..c7e8918 100644 --- a/app/services/Sitemap.ts +++ b/app/services/Sitemap.ts @@ -79,33 +79,61 @@ class Sitemap { return (1 - countOfSlashes * 0.1).toFixed(1); } - private loadLastModified(href: string): string {} + private loadLastModified(href: string): string { + return this.lastModified; + } - private loadFile(href: string) {} + private getFileServerLocation(href: string) { + const jsxHref = ["/politique-de-confidentialite", "/mentions-legales"]; + const isJsxFile = jsxHref.includes(href); + + if (isJsxFile) { + return path.join(this.pagesPath, href.replace("/", ""), "+Page.tsx"); + } + + return path.join(this.pagesPath, href.replace("/", ""), "page.md"); + } + + private loadSubitems(subitems: (typeof navigation)[number]["links"][number]["subitems"]): void { + subitems.forEach((subitem) => { + const fileLocation = this.getFileServerLocation(subitem.href); + console.log("File location:", fileLocation); + + const priority = this.loadPriority(subitem.href); + const lastmod = this.loadLastModified(subitem.href); + const location = `${this.baseUrl}${subitem.href}`; + + this.urls.push({ + location, + lastmod, + priority, + }); + }); + } + + private loadSection(section: (typeof navigation)[number]): void { + section.links.forEach((link) => { + if (link.subitems.length > 0) { + return this.loadSubitems(link.subitems); + } - private loadSection(sectionLinks: (typeof navigation)[number]["links"]) { - return sectionLinks.map((link) => { const priority = this.loadPriority(link.href); const lastmod = this.loadLastModified(link.href); const location = `${this.baseUrl}${link.href}`; - return { + this.urls.push({ location, lastmod, priority, - }; + }); }); } private loadUrls(): void { - this.urls = navigation.flatMap((section) => { - return Array.from( - new Set( - this.loadSection(section.links) - .filter((url) => url !== null) - .sort((a, b) => a.location.localeCompare(b.location)), - ), - ); + navigation.forEach(this.loadSection.bind(this)); + + this.urls = Array.from(new Set(this.urls)).sort((a, b) => { + return a.location.localeCompare(b.location); }); console.log("Loaded URLs:", this.urls); -- 2.45.2 From 4bb8b0e73d3cf596cf92de0cea297b7b9fdd4e25 Mon Sep 17 00:00:00 2001 From: GauthierWebDev Date: Fri, 18 Apr 2025 18:19:21 +0200 Subject: [PATCH 06/10] refactor: Update fileLocation logic in Sitemap class --- app/services/Sitemap.ts | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/app/services/Sitemap.ts b/app/services/Sitemap.ts index c7e8918..cc3c271 100644 --- a/app/services/Sitemap.ts +++ b/app/services/Sitemap.ts @@ -91,7 +91,7 @@ class Sitemap { return path.join(this.pagesPath, href.replace("/", ""), "+Page.tsx"); } - return path.join(this.pagesPath, href.replace("/", ""), "page.md"); + return path.join(this.dataPath, href.replace("/", ""), "page.md"); } private loadSubitems(subitems: (typeof navigation)[number]["links"][number]["subitems"]): void { @@ -117,6 +117,9 @@ class Sitemap { return this.loadSubitems(link.subitems); } + const fileLocation = this.getFileServerLocation(link.href); + console.log("File location:", fileLocation); + const priority = this.loadPriority(link.href); const lastmod = this.loadLastModified(link.href); const location = `${this.baseUrl}${link.href}`; @@ -135,8 +138,6 @@ class Sitemap { this.urls = Array.from(new Set(this.urls)).sort((a, b) => { return a.location.localeCompare(b.location); }); - - console.log("Loaded URLs:", this.urls); } public generateSitemap(): void { -- 2.45.2 From 115ab1b45be4ca60358f55d20a491629700dd96a Mon Sep 17 00:00:00 2001 From: GauthierWebDev Date: Fri, 18 Apr 2025 18:25:34 +0200 Subject: [PATCH 07/10] refactor(Sitemap): Update loadLastModified method to accept fs.Stats --- app/data/certifications/{index => }/page.md | 0 app/data/docs/{documentations => }/page.md | 0 app/data/{docs/index => }/page.md | 0 app/services/Sitemap.ts | 23 +++++++++++++++------ 4 files changed, 17 insertions(+), 6 deletions(-) rename app/data/certifications/{index => }/page.md (100%) rename app/data/docs/{documentations => }/page.md (100%) rename app/data/{docs/index => }/page.md (100%) diff --git a/app/data/certifications/index/page.md b/app/data/certifications/page.md similarity index 100% rename from app/data/certifications/index/page.md rename to app/data/certifications/page.md diff --git a/app/data/docs/documentations/page.md b/app/data/docs/page.md similarity index 100% rename from app/data/docs/documentations/page.md rename to app/data/docs/page.md diff --git a/app/data/docs/index/page.md b/app/data/page.md similarity index 100% rename from app/data/docs/index/page.md rename to app/data/page.md diff --git a/app/services/Sitemap.ts b/app/services/Sitemap.ts index cc3c271..f36a283 100644 --- a/app/services/Sitemap.ts +++ b/app/services/Sitemap.ts @@ -79,8 +79,8 @@ class Sitemap { return (1 - countOfSlashes * 0.1).toFixed(1); } - private loadLastModified(href: string): string { - return this.lastModified; + private loadLastModified(stat?: fs.Stats): string { + return stat ? stat.mtime.toISOString() : this.lastModified; } private getFileServerLocation(href: string) { @@ -97,10 +97,15 @@ class Sitemap { private loadSubitems(subitems: (typeof navigation)[number]["links"][number]["subitems"]): void { subitems.forEach((subitem) => { const fileLocation = this.getFileServerLocation(subitem.href); - console.log("File location:", fileLocation); + let fileDetails: fs.Stats | undefined; + try { + fileDetails = fs.statSync(fileLocation); + } catch (error) { + console.error(`Error loading file for ${subitem.href}:`, error); + } const priority = this.loadPriority(subitem.href); - const lastmod = this.loadLastModified(subitem.href); + const lastmod = this.loadLastModified(fileDetails); const location = `${this.baseUrl}${subitem.href}`; this.urls.push({ @@ -118,10 +123,16 @@ class Sitemap { } const fileLocation = this.getFileServerLocation(link.href); - console.log("File location:", fileLocation); + let fileDetails: fs.Stats | undefined; + + try { + fileDetails = fs.statSync(fileLocation); + } catch (error) { + console.error(`Error loading file for ${link.href}:`, error); + } const priority = this.loadPriority(link.href); - const lastmod = this.loadLastModified(link.href); + const lastmod = this.loadLastModified(fileDetails); const location = `${this.baseUrl}${link.href}`; this.urls.push({ -- 2.45.2 From e33700f13de8bb67c6fe0efe96bb0913ce5a1988 Mon Sep 17 00:00:00 2001 From: GauthierWebDev Date: Fri, 18 Apr 2025 18:34:54 +0200 Subject: [PATCH 08/10] refactor: Improve handling of root document retrieval --- app/pages/index/+data.ts | 7 ++----- app/services/DocsService.ts | 24 +++++++++++++++++------- 2 files changed, 19 insertions(+), 12 deletions(-) diff --git a/app/pages/index/+data.ts b/app/pages/index/+data.ts index 1cc024d..d985aa8 100644 --- a/app/pages/index/+data.ts +++ b/app/pages/index/+data.ts @@ -12,11 +12,8 @@ export type Data = Awaited>; export async function data(_pageContext: PageContext) { const config = useConfig(); - const doc = await docsService.getDoc("docs", "index"); - - if (!doc) { - throw render(404); - } + const doc = await docsService.getDoc("root"); + if (!doc) throw render(404); const readingTimeObject = readingTime(doc.content, 300, "fr"); diff --git a/app/services/DocsService.ts b/app/services/DocsService.ts index a54ecef..274424f 100644 --- a/app/services/DocsService.ts +++ b/app/services/DocsService.ts @@ -109,16 +109,19 @@ class DocsService { public async fetchDocs() { const docs = glob.sync(DocsService.DOCS_PATH + `/**/*.{${DocsService.DOCS_EXTS.join(",")}}`); + const data = await Promise.all( docs.map((doc) => { const content = fs.readFileSync(doc, "utf-8"); const extension = path.extname(doc).slice(1) as DocExtension; - const key = doc + let key = doc .replace(DocsService.DOCS_PATH, "") .replace(`page.${extension}`, "") .replace(`.${extension}`, "") .replace(/\/$/g, ""); + if (key === "") key = "/root"; + const ast = Markdoc.parse(content); const title = ast.attributes?.frontmatter?.match(/^title:\s*(.*?)\s*$/m)?.[1]; const description = ast.attributes?.frontmatter?.match(/^description:\s*(.*?)\s*$/m)?.[1]?.replaceAll('"', ""); @@ -149,23 +152,30 @@ class DocsService { }; } - public async getDoc(namespace: "docs" | "certifications", key: string) { + public async getDoc(namespace: "root"): Promise; + public async getDoc(namespace: "docs" | "certifications", key: string): Promise; + public async getDoc(namespace: "root" | "docs" | "certifications", key?: string): Promise { try { await this.fetchDocs(); - const doc = this.getFromCache(`/${namespace}/${key}`); + let doc: DocData | undefined; - if (!doc) { - throw new Error("Doc not found"); + if (namespace === "root") { + doc = this.getFromCache(`/${namespace}`); + } else { + doc = this.getFromCache(`/${namespace}/${key}`); } + if (!doc) throw new Error("Doc not found"); + return doc; } catch (error) { console.error("Error fetching docs:", error); - return null; + + return undefined; } } - public async getUrls(namespace: "docs" | "certifications") { + public async getUrls(namespace: "root" | "docs" | "certifications") { try { await this.fetchDocs(); const docs = Array.from(this.cache.keys()).filter((key) => key.startsWith(`/${namespace}`)); -- 2.45.2 From 668f73069218a4477ce84a72c67e879c089a22c4 Mon Sep 17 00:00:00 2001 From: GauthierWebDev Date: Fri, 18 Apr 2025 18:41:40 +0200 Subject: [PATCH 09/10] fix: Update findNavigationLink parameter to optional --- app/lib/navigation.ts | 2 +- app/pages/docs/+route.ts | 2 +- app/services/DocsService.ts | 4 +--- 3 files changed, 3 insertions(+), 5 deletions(-) diff --git a/app/lib/navigation.ts b/app/lib/navigation.ts index a74b5e2..a970b4c 100644 --- a/app/lib/navigation.ts +++ b/app/lib/navigation.ts @@ -133,7 +133,7 @@ export function doesLinkSubitemExist(link: NavigationLink, subitemHref: string): return link.subitems.some((subitem) => subitem.href === subitemHref); } -export function findNavigationLink(namespace: string, href: string): NavigationLink | undefined { +export function findNavigationLink(namespace: string, href?: string): NavigationLink | undefined { const currentUrl = `/${namespace}/${href}`.replace(/\/+/g, "/").replace(/\/$/, ""); const foundLink = navigation diff --git a/app/pages/docs/+route.ts b/app/pages/docs/+route.ts index 6d0b182..1552b94 100644 --- a/app/pages/docs/+route.ts +++ b/app/pages/docs/+route.ts @@ -4,7 +4,7 @@ const routeRegex = /^\/docs\/(.*)$/; export function route(pageContext: PageContext) { if (pageContext.urlPathname === "/docs") { - return { routeParams: { key: "documentations" } }; + return { routeParams: { key: "index" } }; } const match = pageContext.urlPathname.match(routeRegex); diff --git a/app/services/DocsService.ts b/app/services/DocsService.ts index 274424f..53a534f 100644 --- a/app/services/DocsService.ts +++ b/app/services/DocsService.ts @@ -152,14 +152,12 @@ class DocsService { }; } - public async getDoc(namespace: "root"): Promise; - public async getDoc(namespace: "docs" | "certifications", key: string): Promise; public async getDoc(namespace: "root" | "docs" | "certifications", key?: string): Promise { try { await this.fetchDocs(); let doc: DocData | undefined; - if (namespace === "root") { + if (namespace === "root" || key === "index") { doc = this.getFromCache(`/${namespace}`); } else { doc = this.getFromCache(`/${namespace}/${key}`); -- 2.45.2 From 0e59a8b2e09bf8266d17add4c8ace129154f8034 Mon Sep 17 00:00:00 2001 From: GauthierWebDev Date: Fri, 18 Apr 2025 18:43:46 +0200 Subject: [PATCH 10/10] style: Update null check for nextPage variable --- app/components/syntax/PrevNextLinks.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/components/syntax/PrevNextLinks.tsx b/app/components/syntax/PrevNextLinks.tsx index 5cabe56..77940dd 100644 --- a/app/components/syntax/PrevNextLinks.tsx +++ b/app/components/syntax/PrevNextLinks.tsx @@ -70,7 +70,7 @@ export function PrevNextLinks() { // In case the next page is the same as the current page (in subitems), // we need to skip it to get the correct next page. - if (nextPage.href === urlPathname) { + if (nextPage?.href === urlPathname) { nextPage = allLinks[linkIndex + 2] || null; } -- 2.45.2