diff --git a/src/components/LinkToPost.astro b/src/components/LinkToPost.astro index 2a941d1..d834d93 100644 --- a/src/components/LinkToPost.astro +++ b/src/components/LinkToPost.astro @@ -2,14 +2,15 @@ import { getEntry } from "astro:content"; import { LinkCard } from "@astrojs/starlight/components"; -import { getCreatedDate } from "@components/get-posts"; +import { getCreatedDate, getTagsHtml } from "@components/get-posts"; export interface Props { slug: string; includeDate?: boolean; + includeTags?: boolean; } -const { slug, includeDate } = Astro.props; +const { slug, includeDate, includeTags } = Astro.props; const entry = await getEntry( "docs", @@ -19,10 +20,22 @@ if (entry == null) { throw new Error(`slug ${slug} isn’t valid`); } +// TODO: include in the name the series' name (only for light/dark for now) + let description: string | undefined; if (includeDate) { description = getCreatedDate(entry); } +if (includeTags) { + const tags = getTagsHtml(entry); + if (tags) { + if (!description) { + description = tags; + } else { + description += `
${tags}`; + } + } +} if (entry.data.description) { if (!description) { diff --git a/src/components/get-posts.ts b/src/components/get-posts.ts index a6602dc..2206225 100644 --- a/src/components/get-posts.ts +++ b/src/components/get-posts.ts @@ -32,24 +32,22 @@ export const getPublishablePosts = async (): Promise => { return posts.sort((postA, postB) => getTime(postB) - getTime(postA)); }; -const sections = ["React", "Dark mode", "CSS", "TypeScript", "Yarn", "Others"]; - -export const getPublishablePostsBySection = async (): Promise< - Record +export const getPublishablePostsByYear = async (): Promise< + Record > => { const posts = await getPublishablePosts(); - const groups: Record = Object.fromEntries( - sections.map((section) => [section, []]), - ); + const groups: Record = {}; for (const post of posts) { - const group = post.id.split("/")[1]; - if (!group) { - throw new Error(`Post "${post.id}" has no section`); + if (!post.data.createdAt) { + throw new Error(`Post "${post.id}" has no createdAt`); } - groups[group]!.push(post); + const year = post.data.createdAt.getUTCFullYear(); + groups[year] ??= []; + groups[year].push(post); } + // in JS, number-keys in objects are auto sorted as-per the spec (from low to high) return groups; }; @@ -118,3 +116,13 @@ export const getCreatedDate = (post: Pick): string => { return full; }; + +export const getTagsHtml = (post: Pick): string | null => { + if (!post.data.tags?.length) { + return null; + } + // TODO: turn those into links, but links to where? + return post.data.tags + .map((tag) => `#${tag}`) + .join(" "); +}; diff --git a/src/components/starlight/PageTitle.astro b/src/components/starlight/PageTitle.astro index 9745ce1..b1e1aec 100644 --- a/src/components/starlight/PageTitle.astro +++ b/src/components/starlight/PageTitle.astro @@ -3,7 +3,7 @@ import { render } from "astro:content"; import Default from "@astrojs/starlight/components/PageTitle.astro"; -import { getCreatedDate } from "@components/get-posts"; +import { getCreatedDate, getTagsHtml } from "@components/get-posts"; const { remarkPluginFrontmatter } = await render( // @ts-expect-error – this works, but for some reasons, it doesn’t seem to be compatible at the type level @@ -11,6 +11,7 @@ const { remarkPluginFrontmatter } = await render( ); const createdDate = getCreatedDate(Astro.locals.starlightRoute.entry); +const tags = getTagsHtml(Astro.locals.starlightRoute.entry); --- @@ -18,9 +19,12 @@ const createdDate = getCreatedDate(Astro.locals.starlightRoute.entry); { // Only display the reading time & last updated when it is a doc Astro.locals.starlightRoute.entry.data.template === "doc" ? ( -

- {remarkPluginFrontmatter.readingTime.displayedText} - {createdDate ? ` • ${createdDate}` : ""} -

+ <> + {tags ?
: null} +

+ {remarkPluginFrontmatter.readingTime.displayedText} + {createdDate ? ` • ${createdDate}` : ""} +

+ ) : null } diff --git a/src/content/docs/posts/drafts.mdx b/src/content/docs/posts/drafts.mdx index bb9d063..622ba33 100644 --- a/src/content/docs/posts/drafts.mdx +++ b/src/content/docs/posts/drafts.mdx @@ -8,4 +8,4 @@ import { getDraftPosts } from "@components/get-posts"; List of all posts that are in the process, but not ready to be open to public: -{(await getDraftPosts()).slice(0, 10).map((post) => )} +{(await getDraftPosts()).slice(0, 10).map((post) => )} diff --git a/src/content/docs/posts/full.mdx b/src/content/docs/posts/full.mdx index 0da283d..249952c 100644 --- a/src/content/docs/posts/full.mdx +++ b/src/content/docs/posts/full.mdx @@ -4,11 +4,11 @@ template: splash --- import LinkToPost from "@components/LinkToPost.astro"; -import { getPublishablePostsBySection } from "@components/get-posts"; +import { getPublishablePostsByYear } from "@components/get-posts"; { -Object.entries(await getPublishablePostsBySection()).map(([section, posts]) => { -return (<>

{section}

-{posts.map(post => )}) +Object.entries(await getPublishablePostsByYear()).reverse().map(([year, posts]) => { +return (<>

{year}

+{posts.map(post => )}) }) } diff --git a/src/content/docs/posts/index.mdx b/src/content/docs/posts/index.mdx index d4f0b27..be5f815 100644 --- a/src/content/docs/posts/index.mdx +++ b/src/content/docs/posts/index.mdx @@ -8,7 +8,7 @@ import { getPublishablePosts } from "@components/get-posts"; ### Latest posts -{(await getPublishablePosts()).slice(0, 10).map((post) => )} +{(await getPublishablePosts()).slice(0, 10).map((post) => )} ### See more diff --git a/src/content/docs/posts/tags.mdx b/src/content/docs/posts/tags.mdx index a3f1489..f952fcb 100644 --- a/src/content/docs/posts/tags.mdx +++ b/src/content/docs/posts/tags.mdx @@ -6,9 +6,10 @@ template: splash import LinkToPost from "@components/LinkToPost.astro"; import { getPostsByTags } from "@components/get-posts"; +← View all { Object.entries(await getPostsByTags()).map(([tag, posts]) => { -return (<>

#{tag} ({posts.length})

+return (<>

#{tag}

    {posts.map(post =>
  • {post.data.title} | {post.data.tags.map(tag => `#${tag}`).join(' ')}
  • )} @@ -16,3 +17,18 @@ return (<>

    #{tag} ({posts.length})

    ) }) } + +