|
1 | 1 | import { Feed } from "feed"; |
| 2 | +import { remark } from "remark"; |
| 3 | +import html from "remark-html"; |
| 4 | +import remarkGfm from "remark-gfm"; |
| 5 | +import { visit } from "unist-util-visit"; |
2 | 6 | import { posts } from "@/utils/posts"; |
3 | 7 |
|
4 | 8 | export const dynamic = "force-static"; |
5 | 9 |
|
6 | | -export async function GET() { |
7 | | - const allPosts = posts.getPosts(); |
8 | | - const latestPost = allPosts[0]; |
9 | | - const siteUrl = process.env.NEXT_PUBLIC_BASE_URL || "https://jacobmoy.com"; |
10 | | - const author = { |
11 | | - name: "Jacob Moy", |
12 | | - email: "me@jacobmoy.com", |
13 | | - link: siteUrl, |
| 10 | +function remarkAbsoluteImages(options: { siteUrl: string }) { |
| 11 | + return (tree: any) => { |
| 12 | + visit(tree, "image", (node: any) => { |
| 13 | + if (node.url.startsWith("/")) { |
| 14 | + node.url = `${options.siteUrl}${node.url}`; |
| 15 | + } |
| 16 | + }); |
14 | 17 | }; |
| 18 | +} |
15 | 19 |
|
16 | | - const feed = new Feed({ |
17 | | - title: "Jacob Moy | Blog", |
18 | | - description: "Jacob Moy's personal blog about software development, technology, and life.", |
19 | | - id: siteUrl, |
20 | | - link: siteUrl, |
21 | | - language: "en", |
22 | | - image: `${siteUrl}/favicon.ico`, |
23 | | - favicon: `${siteUrl}/favicon.ico`, |
24 | | - copyright: `All rights reserved ${new Date().getFullYear()}, Jacob Moy`, |
25 | | - updated: latestPost ? new Date(latestPost.date) : new Date(), |
26 | | - generator: "Feed for Node.js", |
27 | | - feedLinks: { |
28 | | - rss2: `${siteUrl}/blog/rss`, |
29 | | - }, |
30 | | - author: author, |
31 | | - }); |
| 20 | +export async function GET() { |
| 21 | + try { |
| 22 | + const allPosts = posts.getPosts(); |
| 23 | + const latestPost = allPosts[0]; |
| 24 | + const siteUrl = process.env.NEXT_PUBLIC_BASE_URL || "https://jacobmoy.com"; |
| 25 | + const author = { |
| 26 | + name: "Jacob Moy", |
| 27 | + email: "me@jacobmoy.com", |
| 28 | + link: siteUrl, |
| 29 | + }; |
32 | 30 |
|
33 | | - allPosts.forEach((post) => { |
34 | | - const url = `${siteUrl}/blog/posts/${post.slug}`; |
35 | | - feed.addItem({ |
36 | | - title: post.title, |
37 | | - id: url, |
38 | | - link: url, |
39 | | - description: post.description, |
40 | | - content: post.content, |
41 | | - author: [author], |
42 | | - contributor: [author], |
43 | | - date: new Date(post.date), |
44 | | - image: post.image ? `${siteUrl}${post.image}` : undefined, |
| 31 | + const feed = new Feed({ |
| 32 | + title: "Jacob Moy | Blog", |
| 33 | + description: "Jacob Moy's personal blog about software development, technology, and life.", |
| 34 | + id: siteUrl, |
| 35 | + link: siteUrl, |
| 36 | + language: "en", |
| 37 | + image: `${siteUrl}/favicon.ico`, |
| 38 | + favicon: `${siteUrl}/favicon.ico`, |
| 39 | + copyright: `All rights reserved ${new Date().getFullYear()}, Jacob Moy`, |
| 40 | + updated: latestPost ? new Date(latestPost.date) : new Date(), |
| 41 | + generator: "Feed for Node.js", |
| 42 | + feedLinks: { |
| 43 | + rss2: `${siteUrl}/blog/rss`, |
| 44 | + }, |
| 45 | + author: author, |
45 | 46 | }); |
46 | | - }); |
47 | 47 |
|
48 | | - return new Response(feed.rss2(), { |
49 | | - headers: { |
50 | | - "Content-Type": "application/xml", |
51 | | - "Cache-Control": "s-maxage=3600, stale-while-revalidate", |
52 | | - }, |
53 | | - }); |
| 48 | + for (const post of allPosts) { |
| 49 | + const url = `${siteUrl}/blog/posts/${post.slug}`; |
| 50 | + const processedContent = await remark() |
| 51 | + .use(remarkGfm) |
| 52 | + .use(remarkAbsoluteImages, { siteUrl }) |
| 53 | + .use(html) |
| 54 | + .process(post.content); |
| 55 | + const contentHtml = processedContent.toString(); |
| 56 | + |
| 57 | + feed.addItem({ |
| 58 | + title: post.title, |
| 59 | + id: url, |
| 60 | + link: url, |
| 61 | + description: post.description, |
| 62 | + content: contentHtml, |
| 63 | + author: [author], |
| 64 | + contributor: [author], |
| 65 | + date: new Date(post.date), |
| 66 | + image: post.image ? `${siteUrl}${post.image}` : undefined, |
| 67 | + }); |
| 68 | + } |
| 69 | + |
| 70 | + return new Response(feed.rss2(), { |
| 71 | + headers: { |
| 72 | + "Content-Type": "application/xml", |
| 73 | + "Cache-Control": "s-maxage=3600, stale-while-revalidate", |
| 74 | + }, |
| 75 | + }); |
| 76 | + } catch (e: any) { |
| 77 | + console.error(e); |
| 78 | + return new Response(`Error generating RSS feed: ${e.message}\n${e.stack}`, { |
| 79 | + status: 500, |
| 80 | + }); |
| 81 | + } |
54 | 82 | } |
0 commit comments