Skip to content

Commit

Permalink
Merge pull request #6 from Hardeepex/sweep/i_want_to_use_headless_wor…
Browse files Browse the repository at this point in the history
…dpress_in_this

Sweep: I want to use headless wordpress in this template (✓ Sandbox Passed)
  • Loading branch information
Hardeepex committed Jan 18, 2024
2 parents 320cb25 + 64f8175 commit 2381818
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 11 deletions.
8 changes: 4 additions & 4 deletions src/pages/rss.xml.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ export const GET = async () => {
site: import.meta.env.SITE,

items: posts.map((post) => ({
link: getPermalink(post.permalink, 'post'),
title: post.title,
description: post.excerpt,
pubDate: post.publishDate,
link: post.link,
title: post.title.rendered,
description: post.excerpt.rendered,
pubDate: new Date(post.date),
})),

trailingSlash: SITE.trailingSlash,
Expand Down
32 changes: 25 additions & 7 deletions src/utils/blog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,14 +91,25 @@ const getNormalizedPost = async (post: CollectionEntry<'post'>): Promise<Post> =
};

const load = async function (): Promise<Array<Post>> {
const posts = await getCollection('post');
const normalizedPosts = posts.map(async (post) => await getNormalizedPost(post));

const results = (await Promise.all(normalizedPosts))
.sort((a, b) => b.publishDate.valueOf() - a.publishDate.valueOf())
.filter((post) => !post.draft);
const response = await fetch("https://juicytalent.com/wp-json/wp/v2/posts");
const postsData = await response.json();
const posts = postsData.map((postData) => ({
id: String(postData.id),
slug: postData.slug,
permalink: postData.link,
publishDate: new Date(postData.date),
updateDate: postData.modified ? new Date(postData.modified) : undefined,
title: postData.title.rendered,
excerpt: postData.excerpt.rendered,
image: postData.featured_media ? postData.featured_media.source_url : null,
category: postData.categories.length > 0 ? postData.categories[0].slug : undefined, // assuming first category
tags: postData.tags.map(tag => tag.slug), // assuming each has a slug
author: String(postData.author),
draft: postData.status !== 'publish',
readingTime: null // WordPress does not provide reading time by default
}));

return results;
return posts;
};

let _posts: Array<Post>;
Expand Down Expand Up @@ -126,6 +137,13 @@ export const fetchPosts = async (): Promise<Array<Post>> => {
return _posts;
};

// Added due to the need for fetching featured media details
async function getFeaturedMedia(mediaId) {
const mediaResponse = await fetch(`https://juicytalent.com/wp-json/wp/v2/media/${mediaId}`);
const mediaData = await mediaResponse.json();
return mediaData.source_url;
}

/** */
export const findPostsBySlugs = async (slugs: Array<string>): Promise<Array<Post>> => {
if (!Array.isArray(slugs)) return [];
Expand Down

0 comments on commit 2381818

Please sign in to comment.