-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
break modules/post.ts into separate files
- Loading branch information
Showing
24 changed files
with
271 additions
and
265 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |
Oops, something went wrong.