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}`));