From 9b74a5fa621a8704145beb66881ca53faee1a429 Mon Sep 17 00:00:00 2001 From: Kamran Ahmed Date: Mon, 2 Jan 2023 19:52:04 +0400 Subject: [PATCH] Guides listing page --- src/components/GuideListItem.astro | 40 ++++++++++++++++++++ src/lib/guide.ts | 59 ++++++++++++++++++++++++++++++ src/lib/roadmap.ts | 11 +++--- src/lib/topic.ts | 2 +- src/pages/guides.astro | 23 ++++++++++++ 5 files changed, 129 insertions(+), 6 deletions(-) create mode 100644 src/components/GuideListItem.astro create mode 100644 src/lib/guide.ts create mode 100644 src/pages/guides.astro diff --git a/src/components/GuideListItem.astro b/src/components/GuideListItem.astro new file mode 100644 index 000000000..a29c0b0a2 --- /dev/null +++ b/src/components/GuideListItem.astro @@ -0,0 +1,40 @@ +--- +import type { GuideFileType } from "../lib/guide"; + +export interface Props { + guide: GuideFileType; +} + +const { guide } = Astro.props; +const { frontmatter, id } = guide; +--- + + + + {frontmatter.title} + + { + frontmatter.isNew && ( + + New + + + ) + } + + + + » + diff --git a/src/lib/guide.ts b/src/lib/guide.ts new file mode 100644 index 000000000..57cc6a5ac --- /dev/null +++ b/src/lib/guide.ts @@ -0,0 +1,59 @@ +import type { MarkdownFileType } from "./file"; + +export interface GuideFrontmatter { + title: string; + description: string; + author: { + name: string; + url: string; + imageUrl: string; + }; + seo: { + title: string; + description: string; + }; + isNew: boolean; + type: "visual" | "textual"; + date: string; + sitemap: { + priority: number; + changefreq: "daily" | "weekly" | "monthly" | "yealry"; + }; + tags: string[]; +} + +export type GuideFileType = MarkdownFileType & { + id: string; +}; + +/** + * Generates id from the given guide file + * @param filePath Markdown file path + * + * @returns unique guide identifier + */ +function guidePathToId(filePath: string): string { + return filePath.replace("/src/guides/", "").replace(".md", ""); +} + +/** + * Gets all the guides sorted by the publishing date + * @returns Promisifed guide files + */ +export async function getAllGuides(): Promise { + const guides = await import.meta.glob("/src/guides/*.md", { + eager: true, + }); + + const guideFiles = Object.values(guides); + const enrichedGuides = guideFiles.map((guideFile) => ({ + ...guideFile, + id: guidePathToId(guideFile.file), + })); + + return enrichedGuides.sort( + (a, b) => + new Date(b.frontmatter.date).valueOf() - + new Date(a.frontmatter.date).valueOf() + ); +} diff --git a/src/lib/roadmap.ts b/src/lib/roadmap.ts index e574a9908..24a7da090 100644 --- a/src/lib/roadmap.ts +++ b/src/lib/roadmap.ts @@ -1,8 +1,5 @@ -import type { MarkdownFileType } from "./File"; +import type { MarkdownFileType } from "./file"; -export type RoadmapFileType = MarkdownFileType & { - id: string; -}; export interface RoadmapFrontmatter { jsonUrl: string; @@ -32,6 +29,10 @@ export interface RoadmapFrontmatter { tags: string[]; } +export type RoadmapFileType = MarkdownFileType & { + id: string; +}; + function roadmapPathToId(filePath: string):string { const fileName = filePath.split("/").pop() || ""; @@ -44,7 +45,7 @@ function roadmapPathToId(filePath: string):string { * @returns string[] Array of roadmap IDs */ export async function getRoadmapIds() { - const roadmapFiles = await import.meta.glob("/src/roadmaps/*/*.md", { + const roadmapFiles = await import.meta.glob("/src/roadmaps/*/*.md", { eager: true, }); diff --git a/src/lib/topic.ts b/src/lib/topic.ts index 82848cf60..85b33b89b 100644 --- a/src/lib/topic.ts +++ b/src/lib/topic.ts @@ -1,4 +1,4 @@ -import type { MarkdownFileType } from './File'; +import type { MarkdownFileType } from './file'; import type { RoadmapFrontmatter } from './roadmap'; // Generates URL from the topic file path e.g. diff --git a/src/pages/guides.astro b/src/pages/guides.astro new file mode 100644 index 000000000..abd3799c1 --- /dev/null +++ b/src/pages/guides.astro @@ -0,0 +1,23 @@ +--- +import GuideListItem from "../components/GuideListItem.astro"; +import SimplePageHeader from "../components/SimplePageHeader.astro"; +import BaseLayout from "../layouts/BaseLayout.astro"; +import { getAllGuides } from "../lib/guide"; + +const guides = await getAllGuides(); +--- + + + + +
+
+
+ {guides.map((guide) => )} +
+
+
+