Skip to content

Commit

Permalink
Merge pull request #347 from thkruz/related-blog-posts
Browse files Browse the repository at this point in the history
Add Related Blog Posts Component
  • Loading branch information
prototypa authored Jan 22, 2024
2 parents 155a602 + 13cbe42 commit 03887af
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 0 deletions.
28 changes: 28 additions & 0 deletions src/components/blog/RelatedPosts.astro
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
---
import { APP_BLOG } from "~/utils/config";
import { fetchPosts, getRelatedPosts } from "~/utils/blog";
import BlogHighlightedPosts from "../widgets/BlogHighlightedPosts.astro";
import type { Post } from "~/types";
import { getBlogPermalink } from "~/utils/permalinks";
export interface Props {
post: Post;
}
const { post } = Astro.props;
const fetchedPosts = await fetchPosts();
const relatedPosts = post.tags ? getRelatedPosts(fetchedPosts, post.slug, post.tags) : [];
---

{
APP_BLOG.isRelatedPostsEnabled ? (
<BlogHighlightedPosts
classes={{ container: "pt-0 lg:pt-0 md:pt-0" }}
title="Related Posts"
linkText="View All Posts"
linkUrl={getBlogPermalink()}
postIds={relatedPosts.map((post) => post.id)}
/>
) : null
}
2 changes: 2 additions & 0 deletions src/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ apps:
blog:
isEnabled: true
postsPerPage: 6
isRelatedPostsEnabled: true
relatedPostsCount: 4

post:
isEnabled: true
Expand Down
2 changes: 2 additions & 0 deletions src/pages/[...blog]/index.astro
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { getCanonical, getPermalink } from '~/utils/permalinks';
import { getStaticPathsBlogPost, blogPostRobots } from '~/utils/blog';
import { findImage } from '~/utils/images';
import type { MetaData } from '~/types';
import RelatedPosts from '~/components/blog/RelatedPosts.astro';
export const prerender = true;
Expand Down Expand Up @@ -45,4 +46,5 @@ const metadata = merge(
<Layout metadata={metadata}>
<SinglePost post={{ ...post, image: image }} url={url} />
<ToBlogLink />
<RelatedPosts post={post} />
</Layout>
33 changes: 33 additions & 0 deletions src/utils/blog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,18 @@ const getNormalizedPost = async (post: CollectionEntry<'post'>): Promise<Post> =
};
};

const getRandomizedPosts = (array: Post[], num: number) => {
const newArray: Post[] = [];

while (newArray.length < num && array.length > 0) {
const randomIndex = Math.floor(Math.random() * array.length);
newArray.push(array[randomIndex]);
array.splice(randomIndex, 1);
}

return newArray;
};

const load = async function (): Promise<Array<Post>> {
const posts = await getCollection('post');
const normalizedPosts = posts.map(async (post) => await getNormalizedPost(post));
Expand All @@ -105,6 +117,7 @@ let _posts: Array<Post>;

/** */
export const isBlogEnabled = APP_BLOG.isEnabled;
export const isRelatedPostsEnabled = APP_BLOG.isRelatedPostsEnabled;
export const isBlogListRouteEnabled = APP_BLOG.list.isEnabled;
export const isBlogPostRouteEnabled = APP_BLOG.post.isEnabled;
export const isBlogCategoryRouteEnabled = APP_BLOG.category.isEnabled;
Expand Down Expand Up @@ -225,3 +238,23 @@ export const getStaticPathsBlogTag = async ({ paginate }: { paginate: PaginateFu
)
);
};

/** */
export function getRelatedPosts(allPosts: Post[], currentSlug: string, currentTags: string[]) {
if (!isBlogEnabled || !isRelatedPostsEnabled) return [];

const relatedPosts = getRandomizedPosts(
allPosts.filter((post) => post.slug !== currentSlug && post.tags?.some((tag) => currentTags.includes(tag))),
APP_BLOG.relatedPostsCount
);

if (relatedPosts.length < APP_BLOG.relatedPostsCount) {
const morePosts = getRandomizedPosts(
allPosts.filter((post) => post.slug !== currentSlug && !post.tags?.some((tag) => currentTags.includes(tag))),
APP_BLOG.relatedPostsCount - relatedPosts.length
);
relatedPosts.push(...morePosts);
}

return relatedPosts;
}
2 changes: 2 additions & 0 deletions src/utils/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ export interface I18NConfig {
export interface AppBlogConfig {
isEnabled: boolean;
postsPerPage: number;
isRelatedPostsEnabled: boolean;
relatedPostsCount: number;
post: {
isEnabled: boolean;
permalink: string;
Expand Down

0 comments on commit 03887af

Please sign in to comment.