-
Notifications
You must be signed in to change notification settings - Fork 89
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(blog): add related posts (#976)
* feat(blog): add related posts * refactor(blog): props loader related posts * feat(blog): jsdoc and refactors * chore(deno): eslint
- Loading branch information
1 parent
c93ac15
commit c1754b7
Showing
4 changed files
with
126 additions
and
6 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
/** | ||
* @title BlogRelatedPosts | ||
* @description Retrieves a list of blog related posts. | ||
* | ||
* @param props - The props for the blog related post list. | ||
* @param req - The request object. | ||
* @param ctx - The application context. | ||
* @returns A promise that resolves to an array of blog related posts. | ||
*/ | ||
import { RequestURLParam } from "../../website/functions/requestToParam.ts"; | ||
import { AppContext } from "../mod.ts"; | ||
import { BlogPost, SortBy } from "../types.ts"; | ||
import handlePosts, { slicePosts } from "../utils/handlePosts.ts"; | ||
import { getRecordsByPath } from "../utils/records.ts"; | ||
|
||
const COLLECTION_PATH = "collections/blog/posts"; | ||
const ACCESSOR = "post"; | ||
|
||
export interface Props { | ||
/** | ||
* @title Items per page | ||
* @description Number of posts per page to display. | ||
*/ | ||
count?: number; | ||
/** | ||
* @title Page query parameter | ||
* @description The current page number. Defaults to 1. | ||
*/ | ||
page?: number; | ||
/** | ||
* @title Category Slug | ||
* @description Filter by a specific category slug. | ||
*/ | ||
slug?: RequestURLParam | string[]; | ||
/** | ||
* @title Page sorting parameter | ||
* @description The sorting option. Default is "date_desc" | ||
*/ | ||
sortBy?: SortBy; | ||
/** | ||
* @description Overrides the query term at url | ||
*/ | ||
query?: string; | ||
/** | ||
* @title Exclude Post Slug | ||
* @description Excludes a post slug from the list | ||
*/ | ||
excludePostSlug?: RequestURLParam | string; | ||
} | ||
|
||
/** | ||
* @title BlogRelatedPosts | ||
* @description Retrieves a list of blog related posts. | ||
* | ||
* @param props - The props for the blog related post list. | ||
* @param req - The request object. | ||
* @param ctx - The application context. | ||
* @returns A promise that resolves to an array of blog related posts. | ||
*/ | ||
|
||
export type BlogRelatedPosts = BlogPost[] | null; | ||
|
||
export default async function BlogRelatedPosts( | ||
{ page, count, slug, sortBy, query, excludePostSlug }: Props, | ||
req: Request, | ||
ctx: AppContext, | ||
): Promise<BlogRelatedPosts> { | ||
const url = new URL(req.url); | ||
const postsPerPage = Number(count ?? url.searchParams.get("count") ?? 12); | ||
const pageNumber = Number(page ?? url.searchParams.get("page") ?? 1); | ||
const pageSort = sortBy ?? (url.searchParams.get("sortBy") as SortBy) ?? | ||
"date_desc"; | ||
const term = query ?? url.searchParams.get("q") ?? undefined; | ||
|
||
const posts = await getRecordsByPath<BlogPost>( | ||
ctx, | ||
COLLECTION_PATH, | ||
ACCESSOR, | ||
); | ||
|
||
const handledPosts = handlePosts( | ||
posts, | ||
pageSort, | ||
slug, | ||
term, | ||
excludePostSlug, | ||
); | ||
|
||
if (!handledPosts) { | ||
return null; | ||
} | ||
|
||
const slicedPosts = slicePosts(handledPosts, pageNumber, postsPerPage); | ||
|
||
return slicedPosts.length > 0 ? slicedPosts : null; | ||
} |
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