Closed Hardeepex closed 6 months ago
31f04144a4
)[!TIP] I'll email you at hardeep.ex@gmail.com when I complete this pull request!
Here are the GitHub Actions logs prior to making any changes:
320cb25
Checking src/utils/blog.ts for syntax errors... ✅ src/utils/blog.ts has no syntax errors!
1/1 ✓Checking src/utils/blog.ts for syntax errors... ✅ src/utils/blog.ts has no syntax errors!
Sandbox passed on the latest main
, so sandbox checks will be enabled for this issue.
I found the following snippets in your repository. I will now analyze these snippets and come up with a plan.
src/utils/blog.ts
✓ https://github.com/Hardeepex/astroheadless/commit/8415d85e6b1d8cbb5992d5f37d1a78f98b0c81ec Edit
Modify src/utils/blog.ts with contents:
• Replace the existing `load` function with a new implementation that fetches blog posts from the WordPress REST API.
• Use the provided endpoint URL "https://juicytalent.com/wp-json/wp/v2/posts" to fetch the posts.
• Parse the JSON response and map each post to the existing Post type structure, ensuring that fields such as `id`, `slug`, `permalink`, `publishDate`, `updateDate`, `title`, `excerpt`, `image`, `category`, `tags`, `author`, `draft`, and `readingTime` are appropriately populated from the WordPress post data.
• Since the WordPress API provides dates in ISO format, convert them to JavaScript Date objects for the `publishDate` and `updateDate` fields.
• The `image` field should be populated with the URL of the featured media if available.
• The `draft` field should be determined based on the `status` field from the WordPress post data, where `status` of "publish" corresponds to `draft: false`.
• Remove the sorting and filtering logic as the WordPress API already returns published posts in reverse chronological order.
--- +++ @@ -91,14 +91,25 @@ }; const load = async function (): Promise> { - 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); - - return results; + 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 posts; }; let _posts: Array ; @@ -125,6 +136,13 @@ 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 ): Promise > => {
src/utils/blog.ts
✓ Edit
Check src/utils/blog.ts with contents:
Ran GitHub Actions for 8415d85e6b1d8cbb5992d5f37d1a78f98b0c81ec:
src/pages/rss.xml.ts
✓ https://github.com/Hardeepex/astroheadless/commit/64f81757ecab3d4dce75df2403d7b40926d189c5 Edit
Modify src/pages/rss.xml.ts with contents:
• Modify the `GET` function to use the updated `fetchPosts` function which now fetches posts from the WordPress REST API.
• Ensure that the `items` array in the `getRssString` function call is populated with the correct fields from the WordPress post data.
• The `link` field should use the `link` property from the WordPress post data.
• The `title` field should use the `title.rendered` property from the WordPress post data.
• The `description` field should use the `excerpt.rendered` property from the WordPress post data.
• The `pubDate` field should use the `date` property from the WordPress post data, converted to a JavaScript Date object.
• Ensure that the `getPermalink` function is either modified or replaced to handle the permalink structure provided by the WordPress API.
--- +++ @@ -20,10 +20,10 @@ 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,
src/pages/rss.xml.ts
✓ Edit
Check src/pages/rss.xml.ts with contents:
Ran GitHub Actions for 64f81757ecab3d4dce75df2403d7b40926d189c5:
I have finished reviewing the code for completeness. I did not find errors for sweep/i_want_to_use_headless_wordpress_in_this
.
💡 To recreate the pull request edit the issue title or description. To tweak the pull request, leave a comment on the pull request. Join Our Discord
This is an automated message generated by Sweep AI.
I want to use headless wordpress in this template
My Endpoint: https://juicytalent.com/wp-json/wp/v2/posts
{ "id": 1469, "date": "2023-12-30T06:51:13", "date_gmt": "2023-12-30T06:51:13", "guid": { "rendered": "https://juicytalent.com/diablo-3s-final-season-demonstrates-everything-diablo-4-gets-wrong/" }, "modified": "2023-12-30T06:51:13", "modified_gmt": "2023-12-30T06:51:13", "slug": "diablo-3s-final-season-demonstrates-everything-diablo-4-gets-wrong", "status": "publish", "type": "post", "link": "https://juicytalent.com/diablo-3s-final-season-demonstrates-everything-diablo-4-gets-wrong/", "title": { "rendered": "Diablo 3’s final season demonstrates everything Diablo 4 gets wrong" }, "content": { "rendered": "\n
It sportsman earnestly ye preserved an oner. Had judgment out opinions property the supplied.
\n\n\n\nName wereg me.
\n\n\n\nBOffere You day real less till dear read. Consid
\n", "protected": false }, "excerpt": { "rendered": "It sportsman earnestly ye preserved an on. Moment lely […]
\n", "protected": false }, "author": 1, "featured_media": 1357, "comment_status": "open", "ping_status": "open", "sticky": false, "template": "", "format": "standard", "meta": { "_uag_custom_page_level_css": "", "ub_ctt_via": "", "footnotes": "" },This is the official Astro Guide
Headless WordPress & Astro WordPress is a content management system that includes its own frontend, but can also be used as a headless CMS to provide content to your Astro project.
Integrating with AstroSection titled Integrating with Astro WordPress comes with a built-in WordPress REST API to connect your WordPress data to Astro. You can optionally install WPGraphQL on your site to use GraphQL.
PrerequisitesSection titled Prerequisites To get started, you will need to have the following:
An Astro project - If you don’t have an Astro project yet, our Installation guide will get you up and running in no time. A WordPress site - Your site’s REST API is [YOUR_SITE]/wp-json/wp/v2/ and is available by default with any WordPress site. It is also possible to set up WordPress on a local environment. Setting up CredentialsSection titled Setting up Credentials Your WordPress REST API is available to external requests for data fetching without authentication by default. This does not allow users to modify your data or site settings and allows you to use your data in your Astro project without any credentials.
You may choose to require authentication if necessary.
Fetching DataSection titled Fetching Data Fetch your WordPress data through your site’s unique REST API URL and the route for your content. (For a blog, this will commonly be posts.) Then, you can render your data properties using Astro’s set:html={} directive.
For example, to display a list of post titles and their content:
src/pages/index.astro const res = await fetch("https://[YOUR-SITE]/wp-json/wp/v2/posts"); const posts = await res.json();
Astro + WordPress 🚀
{ posts.map((post) => ( )) } The WordPress REST API includes global parameters such as _fields and _embed. A large quantity of data is available to you via this API, so you may wish to only fetch certain fields. You can restrict your response by adding the _fields parameter to the API URL, for example: [YOUR-SITE]/wp/v2/posts?_fields=author,id,excerpt,title,link The API can also return content related to your post, such as a link to the parent post, or to comments on the post. You can add the _embed parameter to the API URL (e.g. [YOUR-SITE]/wp/v2/posts?_embed) to indicate to the server that the response should include these embedded resources. Building a blog with WordPress and AstroSection titled Building a blog with WordPress and Astro This example fetches data from the public WordPress API of https://norian.studio/dinosaurs/. This WordPress site stores information about individual dinosaurs under the dinos route, just as a blog would store individual blog posts under the posts route. This example shows how to reproduce this site structure in Astro: an index page that lists dinosaurs with links to dynamically-generated individual dinosaur pages. Note To use Custom Post Types (CPT) in your WordPress API (not just post and page), you will have to configure them in your WordPress dashboard or add REST API Support For Custom Content Types in WordPress. This example fetches data from a WordPress site whose content types have already been configured and exposed to the REST API. Displaying a list of WordPress postsSection titled Displaying a list of WordPress posts The page src/pages/index.astro lists each dinosaur, with a description and link to its own page. Directorysrc/ Directorypages/ index.astro Directorydinos/ [slug].astro astro.config.mjs package.json Fetching via the API returns an object that includes the properties: title.rendered - Contains the HTML rendering of the title of the post. content.rendered - Contains the HTML rendering of the content of the post. slug - Contains the slug of the post. (This provides the link to the dynamically-generated individual dinosaur pages.) /src/pages/index.astro import Layout from "../layouts/Layout.astro"; let res = await fetch("https://norian.studio/wp-json/wp/v2/dinos"); let posts = await res.json();List of Dinosaurs
{ posts.map((post) => (Checklist
- [X] Modify `src/utils/blog.ts` ✓ https://github.com/Hardeepex/astroheadless/commit/8415d85e6b1d8cbb5992d5f37d1a78f98b0c81ec [Edit](https://github.com/Hardeepex/astroheadless/edit/sweep/i_want_to_use_headless_wordpress_in_this/src/utils/blog.ts#L93-L102) - [X] Running GitHub Actions for `src/utils/blog.ts` ✓ [Edit](https://github.com/Hardeepex/astroheadless/edit/sweep/i_want_to_use_headless_wordpress_in_this/src/utils/blog.ts#L93-L102) - [X] Modify `src/pages/rss.xml.ts` ✓ https://github.com/Hardeepex/astroheadless/commit/64f81757ecab3d4dce75df2403d7b40926d189c5 [Edit](https://github.com/Hardeepex/astroheadless/edit/sweep/i_want_to_use_headless_wordpress_in_this/src/pages/rss.xml.ts#L15-L30) - [X] Running GitHub Actions for `src/pages/rss.xml.ts` ✓ [Edit](https://github.com/Hardeepex/astroheadless/edit/sweep/i_want_to_use_headless_wordpress_in_this/src/pages/rss.xml.ts#L15-L30)