Skip to content

Commit

Permalink
group posts by year works, now handle route params and PostCardSmall
Browse files Browse the repository at this point in the history
  • Loading branch information
nemanjam committed Jun 16, 2024
1 parent 142a0ab commit b9e7d57
Show file tree
Hide file tree
Showing 13 changed files with 106 additions and 31 deletions.
2 changes: 2 additions & 0 deletions docs/todo3.md
Original file line number Diff line number Diff line change
Expand Up @@ -317,4 +317,6 @@ PostCardSmall
extract types from constants
update yarn scripts for eslint, prettier, types
404 page design
PostCard updatedDate
post collection date md filename and publishDate?
```
4 changes: 2 additions & 2 deletions src/components/CategoriesAndTags.astro
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ import FilterList from '@/components/FilterList.astro';
import Tag from '@/components/Tag.astro';
import { cn } from '@/utils/styles';
import type { FilterLink, PostCollection } from '@/types/post';
import type { FilterLink, Post } from '@/types/post';
export interface Props extends astroHTML.JSX.AnchorHTMLAttributes {
posts: PostCollection[];
posts: Post[];
}
const { posts, class: className, ...props } = Astro.props;
Expand Down
7 changes: 5 additions & 2 deletions src/modules/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ export const getAllEntries = async <T extends CollectionKey>(
return sortedEntries;
};

/*-------------------------------- sort by publishDate ------------------------------*/
/*-------------------------------- sort by updatedDate or publishDate ------------------------------*/

export const getEntryLastDate = <T extends CollectionKey>(entry: CollectionEntry<T>): Date =>
entry.data.updatedDate ?? entry.data.publishDate;

export const sortEntriesByDateDesc = <T extends CollectionKey>(entries: CollectionEntry<T>[]) =>
entries.slice().sort((a, b) => b.data.publishDate.valueOf() - a.data.publishDate.valueOf());
entries.slice().sort((a, b) => getEntryLastDate(b).valueOf() - getEntryLastDate(a).valueOf());
5 changes: 5 additions & 0 deletions src/modules/post/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { COLLECTIONS } from '@/constants/collections';

import type { Post, PostCollection } from '@/types/post';

/** Sorted posts. */
export const getAllPosts = (): Promise<PostCollection[]> => getAllEntries(COLLECTIONS.POST);

export const getPostsWithReadingTimeFromPosts = async (
Expand All @@ -20,3 +21,7 @@ export const getPostsWithReadingTimeFromPosts = async (
const postsWithReadingTime = posts.map((post, index) => ({ ...post, ...readingTimes[index] }));
return postsWithReadingTime;
};

/** Prefer over getAllPosts() */
export const getAllPostsWithReadingTime = async (): Promise<Post[]> =>
getPostsWithReadingTimeFromPosts(await getAllPosts());
26 changes: 26 additions & 0 deletions src/modules/post/group-by-year.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { getEntryLastDate } from '@/modules/common';

import type { Post, PostsByYear } from '@/types/post';

export const groupPostsByYear = (sortedPosts: Post[]): PostsByYear => {
const postsByYear: PostsByYear = {
years: [],
posts: {},
};

const groupedPosts = sortedPosts.reduce((result, post) => {
const postDate = getEntryLastDate(post);
const year = postDate.getFullYear().toString();

if (!result.posts[year]) {
result.years.push(year);
result.posts[year] = [];
}

result.posts[year].push(post);

return result;
}, postsByYear);

return groupedPosts;
};
6 changes: 2 additions & 4 deletions src/pages/blog/[...page].astro
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
import { getAllPosts, getPostsWithReadingTimeFromPosts } from '@/modules/post/common';
import { getAllPostsWithReadingTime } from '@/modules/post/common';
import List from '@/layouts/List.astro';
import PostList from '@/components/PostList.astro';
import { CONFIG } from '@/config';
Expand All @@ -11,9 +11,7 @@ import type { GetStaticPathsOptions } from 'astro';
// [page].astro and getStaticPaths because of pagination
export async function getStaticPaths({ paginate }: GetStaticPathsOptions) {
const sortedPosts = await getAllPosts();
const posts: Post[] = await getPostsWithReadingTimeFromPosts(sortedPosts);
const posts: Post[] = await getAllPostsWithReadingTime();
const pagination = paginate(posts, { pageSize: CONFIG.PAGE_SIZE }); // must take entire config here, interesting
pagination.push({ params: { page: '1' }, props: pagination[0].props });
Expand Down
5 changes: 2 additions & 3 deletions src/pages/blog/[slug].astro
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
import { Image } from 'astro:assets';
import { getAllPosts, getPostsWithReadingTimeFromPosts } from '@/modules/post/common';
import { getAllPostsWithReadingTime } from '@/modules/post/common';
import { getRandomPosts } from '@/modules/post/random';
import { getHeadingsForTableOfContents } from '@/modules/post/table-of-contents';
import { default as PostLayout } from '@/layouts/Post.astro';
Expand All @@ -16,8 +16,7 @@ import type { Metadata } from '@/types/common';
import type { Post } from '@/types/post';
export async function getStaticPaths() {
const sortedPosts = await getAllPosts();
const posts: Post[] = await getPostsWithReadingTimeFromPosts(sortedPosts);
const posts: Post[] = await getAllPostsWithReadingTime();
const paths = posts.map((post) => {
const { slug } = post;
Expand Down
52 changes: 52 additions & 0 deletions src/pages/blog/categories-and-tags/[...page].astro
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
---
import { getAllPostsWithReadingTime } from '@/modules/post/common';
import { groupPostsByYear } from '@/modules/post/group-by-year';
import List from '@/layouts/List.astro';
import CategoriesAndTags from '@/components/CategoriesAndTags.astro';
import { CONFIG } from '@/config';
import { getPageMetadata } from '@/utils/metadata';
import { pickPaginationPropsFromPage } from '@/utils/pagination';
import type { Post } from '@/types/post';
import type { GetStaticPathsOptions } from 'astro';
export async function getStaticPaths({ paginate }: GetStaticPathsOptions) {
const posts: Post[] = await getAllPostsWithReadingTime();
// filter by tag and category here
// must have grouped here for pagination // no need
const pagination = paginate(posts, { pageSize: CONFIG.PAGE_SIZE });
pagination.push({ params: { page: '1' }, props: pagination[0].props });
return pagination;
}
const { page } = Astro.props;
const { data: posts } = page;
// for categories and tags
const allPosts = await getAllPostsWithReadingTime();
const postsByYear = groupPostsByYear(posts);
const paginationProps = pickPaginationPropsFromPage(page);
const metadata = getPageMetadata('lists/blog/categories-and-tags');
const layoutProps = { metadata, paginationProps };
---

<List {...layoutProps}>
<CategoriesAndTags posts={allPosts} />
{
postsByYear.years.map((year) => (
<>
<h2 class="title text-lg">{year}</h2>
<ul class="mb-8 mt-6 space-y-8">
{postsByYear.posts[year].map((post) => (
<li class="">{post.data.title}</li>
))}
</ul>
</>
))
}
</List>
14 changes: 0 additions & 14 deletions src/pages/blog/categories-and-tags/index.astro

This file was deleted.

5 changes: 2 additions & 3 deletions src/pages/blog/categories/[category]/[...page].astro
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
import { getUniqueCategories } from '@/modules/post/category';
import { getAllPosts, getPostsWithReadingTimeFromPosts } from '@/modules/post/common';
import { getAllPostsWithReadingTime } from '@/modules/post/common';
import List from '@/layouts/List.astro';
import Link from '@/components/Link.astro';
import PostList from '@/components/PostList.astro';
Expand All @@ -14,8 +14,7 @@ import type { Post } from '@/types/post';
import type { GetStaticPathsOptions } from 'astro';
export async function getStaticPaths({ paginate }: GetStaticPathsOptions) {
const sortedPosts = await getAllPosts();
const posts: Post[] = await getPostsWithReadingTimeFromPosts(sortedPosts);
const posts: Post[] = await getAllPostsWithReadingTime();
const uniqueCategories = getUniqueCategories(posts);
Expand Down
5 changes: 2 additions & 3 deletions src/pages/blog/tags/[tag]/[...page].astro
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
import { getAllPosts, getPostsWithReadingTimeFromPosts } from '@/modules/post/common';
import { getAllPostsWithReadingTime } from '@/modules/post/common';
import { getUniqueTags } from '@/modules/post/tag';
import List from '@/layouts/List.astro';
import Link from '@/components/Link.astro';
Expand All @@ -13,8 +13,7 @@ import type { Post } from '@/types/post';
import type { GetStaticPathsOptions } from 'astro';
export async function getStaticPaths({ paginate }: GetStaticPathsOptions) {
const sortedPosts = await getAllPosts();
const posts: Post[] = await getPostsWithReadingTimeFromPosts(sortedPosts);
const posts: Post[] = await getAllPostsWithReadingTime();
const uniqueTags = getUniqueTags(posts);
Expand Down
1 change: 1 addition & 0 deletions src/schemas/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export const projectSchema = ({ image }: SchemaContext) =>
title: z.string().default(TITLE),
description: z.string().default(DESCRIPTION),
publishDate: z.coerce.date(),
updatedDate: z.coerce.date().optional(),
heroImage: image().default(HERO_IMAGE),
heroAlt: z.string().default(HERO_ALT),
draft: z.boolean().default(DRAFT),
Expand Down
5 changes: 5 additions & 0 deletions src/types/post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ export type Post = PostCollection & {
readingTime: number;
};

export interface PostsByYear {
years: string[];
posts: Record<string, Post[]>;
}

export type FilterType = 'tag' | 'category';

/** For both tags and categories. */
Expand Down

0 comments on commit b9e7d57

Please sign in to comment.