Skip to content

Commit

Permalink
break modules/post.ts into separate files
Browse files Browse the repository at this point in the history
  • Loading branch information
nemanjam committed Jun 16, 2024
1 parent 058b7cc commit b99aae7
Show file tree
Hide file tree
Showing 24 changed files with 271 additions and 265 deletions.
2 changes: 1 addition & 1 deletion docs/my-old-code/open-graph/[...route].ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { OGImageRoute } from 'astro-og-canvas';

import { getAllPosts } from '@/modules/post';
import { getAllPosts } from '@/modules/post/common';
import { getAllProjects } from '@/modules/project';
import { ROUTES } from '@/constants/routes';

Expand Down
5 changes: 5 additions & 0 deletions docs/todo3.md
Original file line number Diff line number Diff line change
Expand Up @@ -310,4 +310,9 @@ fix image sizes for new PostCard
link and primary single color
put all components and variants in styleguide
daisy ui color-mix() and oklch just to calc hover colors, i dont need it, hardcode it
sorted archive like in astro-cactus, route param highlight and link
refactor rss and json feed
ProjectCard and test markdown
PostCardSmall
extract types from constants
```
6 changes: 3 additions & 3 deletions src/components/CategoriesAndTags.astro
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
---
import { Icon } from 'astro-icon/components';
import { getCategoryLinks, getCategoryProps, getTagLinks } from '@/modules/post';
import { getCategoryLinks, getCategoryProps } from '@/modules/post/category';
import { getTagLinks } from '@/modules/post/tag';
import Button from '@/components/Button.astro';
import FilterList from '@/components/FilterList.astro';
import Tag from '@/components/Tag.astro';
import { cn } from '@/utils/styles';
import type { FilterLink } from '@/modules/post';
import type { PostCollection } from '@/types/post';
import type { FilterLink, PostCollection } from '@/types/post';
export interface Props extends astroHTML.JSX.AnchorHTMLAttributes {
posts: PostCollection[];
Expand Down
2 changes: 1 addition & 1 deletion src/components/CategoryCard.astro
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
import { Icon } from 'astro-icon/components';
import { getCategoryProps } from '@/modules/post';
import { getCategoryProps } from '@/modules/post/category';
import { cn } from '@/utils/styles';
export interface Props extends astroHTML.JSX.AnchorHTMLAttributes {
Expand Down
2 changes: 1 addition & 1 deletion src/components/FilterList.astro
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
import { cn } from '@/utils/styles';
import type { FilterLink } from '@/modules/post';
import type { FilterLink } from '@/types/post';
export interface Props extends astroHTML.JSX.AnchorHTMLAttributes {
itemLinks: FilterLink[];
Expand Down
2 changes: 1 addition & 1 deletion src/components/PostCard.astro
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { Icon } from 'astro-icon/components';
import { Image } from 'astro:assets';
import { getCategoryProps } from '@/modules/post';
import { getCategoryProps } from '@/modules/post/category';
import Link from '@/components/Link.astro';
import { ROUTES } from '@/constants/routes';
import { formatDate, formatDateIso } from '@/utils/datetime';
Expand Down
2 changes: 1 addition & 1 deletion src/components/PostMeta.astro
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import Link from '@/components/Link.astro';
import { ROUTES } from '@/constants/routes';
import { formatDate, formatDateIso } from '@/utils/datetime';
import { cn } from '@/utils/styles';
import { getCategoryProps } from '@/modules/post';
import { getCategoryProps } from '@/modules/post/category';
interface Props {
publishDate: Date;
Expand Down
2 changes: 1 addition & 1 deletion src/modules/feed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { createMarkdownProcessor } from '@astrojs/markdown-remark';

import { Feed } from 'feed';

import { getAllPosts } from '@/modules/post';
import { getAllPosts } from '@/modules/post/common';
import { ROUTES } from '@/constants/routes';
import { CONFIG } from '@/config';

Expand Down
241 changes: 0 additions & 241 deletions src/modules/post.ts

This file was deleted.

53 changes: 53 additions & 0 deletions src/modules/post/category.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { CATEGORIES } from '@/constants/collections';
import { ROUTES } from '@/constants/routes';

import type { CategoryType } from '@/constants/collections';
import type { Filter, FilterLink, PostCollection } from '@/types/post';

export const getAllCategories = (posts: PostCollection[]): string[] =>
posts.map((post) => post.data.category).filter(Boolean) as string[];

export const getUniqueCategories = (posts: PostCollection[]): string[] => {
const uniqueCategories = [...new Set([...getAllCategories(posts)])];
return uniqueCategories;
};

export const getSortedUniqueCategoriesWithCount = (posts: PostCollection[]): Filter[] => {
const categories = getAllCategories(posts);
if (!(categories.length > 0)) return [];

const uniqueCategories = getUniqueCategories(posts);

const categoriesWithCount = uniqueCategories.map((category) => {
const count = categories.filter((item) => item === category).length;
return { text: category, count };
});

const sortedCategoriesWithCount = categoriesWithCount.slice().sort((a, b) => b.count - a.count);
return sortedCategoriesWithCount;
};

export const getCategoryLinks = (posts: PostCollection[], pathname?: string): FilterLink[] => {
const filterItems = getSortedUniqueCategoriesWithCount(posts);

const itemLinks = filterItems.map((item) => {
const { text, count } = item;

const href = `${ROUTES.CATEGORIES}${text}`;
const textWithCount = `${text} ${count}`;

const isActive = href === pathname;

const link = { href, text, count, textWithCount, isActive };

return link;
});

return itemLinks;
};

const defaultCategory = CATEGORIES[0];

/** set default to prevent breaking build */
export const getCategoryProps = (categoryName: string): CategoryType =>
CATEGORIES.find((item) => item.name === categoryName) ?? defaultCategory;
22 changes: 22 additions & 0 deletions src/modules/post/common.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { getAllEntries } from '@/modules/common';
import { COLLECTIONS } from '@/constants/collections';

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

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

export const getPostsWithReadingTimeFromPosts = async (
posts: PostCollection[]
): Promise<Post[]> => {
const readingTimePromises = posts.map(async (post) => {
const { remarkPluginFrontmatter } = await post.render();
const { readingTime } = remarkPluginFrontmatter;
return { readingTime };
});
const readingTimes = await Promise.all(readingTimePromises);

// other frontmatter props are in post.data...
// readingTimes is in post.readingTimes
const postsWithReadingTime = posts.map((post, index) => ({ ...post, ...readingTimes[index] }));
return postsWithReadingTime;
};
Loading

0 comments on commit b99aae7

Please sign in to comment.