diff --git a/src/components/get-posts.ts b/src/components/get-posts.ts index cd1b916..36227be 100644 --- a/src/components/get-posts.ts +++ b/src/components/get-posts.ts @@ -1,6 +1,8 @@ import { getCollection, type CollectionEntry } from "astro:content"; -const getTime = (post: CollectionEntry<"docs">): number => { +type Post = CollectionEntry<"docs">; + +const getTime = (post: Post): number => { if (post.data.createdAt) { return post.data.createdAt.getTime(); } @@ -8,7 +10,7 @@ const getTime = (post: CollectionEntry<"docs">): number => { return 0; }; -export const getPublishablePosts = async () => { +export const getPublishablePosts = async (): Promise => { const posts = await getCollection( "docs", (post) => @@ -32,10 +34,12 @@ export const getPublishablePosts = async () => { const sections = ["React", "Dark mode", "CSS", "TypeScript", "Yarn", "Others"]; -export const getPublishablePostsBySection = async () => { +export const getPublishablePostsBySection = async (): Promise< + Record +> => { const posts = await getPublishablePosts(); - // console.log(posts); - const groups: Record[]> = Object.fromEntries( + + const groups: Record = Object.fromEntries( sections.map((section) => [section, []]), ); for (const post of posts) { @@ -49,6 +53,21 @@ export const getPublishablePostsBySection = async () => { return groups; }; +export const getPublishablePostsByTags = async (): Promise< + Record +> => { + const posts = await getPublishablePosts(); + const tagGroups: Record = {}; + for (const post of posts) { + for (const tag of post.data.tags || []) { + tagGroups[tag] ??= []; + tagGroups[tag].push(post); + } + } + + return tagGroups; +}; + export const getDraftPosts = async () => { const posts = await getCollection("docs", (post) => post.data.wip); @@ -73,9 +92,7 @@ export const getDraftPosts = async () => { return posts.sort((postA, postB) => getTime(postB) - getTime(postA)); }; -export const getCreatedDate = ( - post: Pick, "data">, -): string => { +export const getCreatedDate = (post: Pick): string => { if (!post.data.createdAt) { return ""; } diff --git a/src/content/docs/posts/tags.mdx b/src/content/docs/posts/tags.mdx new file mode 100644 index 0000000..38fddf8 --- /dev/null +++ b/src/content/docs/posts/tags.mdx @@ -0,0 +1,18 @@ +--- +title: Full list of posts per tags +template: splash +--- + +import LinkToPost from "@components/LinkToPost.astro"; +import { getPublishablePostsByTags } from "@components/get-posts"; + +{ +Object.entries(await getPublishablePostsByTags()).map(([tag, posts]) => { +return (<>

#{tag}

+ +
    +{posts.map(post =>
  • {post.data.title} | {post.data.tags.map(tag => `#${tag}`).join(' ')}
  • )} +
+) +}) +}