|
| 1 | +import { NextResponse } from 'next/server'; |
| 2 | +import fs from 'fs'; |
| 3 | +import path from 'path'; |
| 4 | +import matter from 'gray-matter'; |
| 5 | + |
| 6 | +const postsDirectory = path.join(process.cwd(), 'content/posts'); |
| 7 | + |
| 8 | +function getAllPosts() { |
| 9 | + const slugs = fs.readdirSync(postsDirectory); |
| 10 | + const posts = slugs |
| 11 | + .map((slug) => { |
| 12 | + const fullPath = path.join(postsDirectory, slug); |
| 13 | + const fileContents = fs.readFileSync(fullPath, 'utf8'); |
| 14 | + const { data, content } = matter(fileContents); |
| 15 | + |
| 16 | + return { |
| 17 | + slug: slug.replace(/\.md$/, ''), |
| 18 | + frontmatter: data, |
| 19 | + content, |
| 20 | + fullPath, |
| 21 | + }; |
| 22 | + }) |
| 23 | + .filter((post) => post.frontmatter.showInBlog !== false) |
| 24 | + .sort((a, b) => new Date(b.frontmatter.date) - new Date(a.frontmatter.date)); |
| 25 | + |
| 26 | + return posts; |
| 27 | +} |
| 28 | + |
| 29 | +function generateAtomFeed(posts) { |
| 30 | + const siteUrl = 'https://open-elements.com'; |
| 31 | + const feed = `<?xml version="1.0" encoding="utf-8" standalone="yes"?> |
| 32 | +<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en"> |
| 33 | + <title>Open Elements</title> |
| 34 | + <link href="${siteUrl}/api/atom" rel="self"/> |
| 35 | + <link href="${siteUrl}"/> |
| 36 | + <updated>${new Date().toISOString()}</updated> |
| 37 | + <id>${siteUrl}/</id> |
| 38 | + <author> |
| 39 | + <name>Open Elements</name> |
| 40 | + </author> |
| 41 | + ${posts.map(post => ` |
| 42 | + <entry> |
| 43 | + <title>${post.frontmatter.title}</title> |
| 44 | + <link href="${siteUrl}/posts/${post.frontmatter.date ? post.frontmatter.date.substring(0, 4) : ''}/${post.frontmatter.date ? post.frontmatter.date.substring(5, 7) : ''}/${post.frontmatter.date ? post.frontmatter.date.substring(8, 10) : ''}/${post.slug}/"/> |
| 45 | + <id>${siteUrl}/posts/${post.frontmatter.date ? post.frontmatter.date.substring(0, 4) : ''}/${post.frontmatter.date ? post.frontmatter.date.substring(5, 7) : ''}/${post.frontmatter.date ? post.frontmatter.date.substring(8, 10) : ''}/${post.slug}/</id> |
| 46 | + <updated>${new Date(post.frontmatter.date || post.frontmatter.lastmod).toISOString()}</updated> |
| 47 | + <published>${new Date(post.frontmatter.date).toISOString()}</published> |
| 48 | + <author> |
| 49 | + <name>${post.frontmatter.author || 'Open Elements'}</name> |
| 50 | + </author> |
| 51 | + <summary type="html"><![CDATA[${post.frontmatter.excerpt || ''}]]></summary> |
| 52 | + <content type="html"><![CDATA[${post.frontmatter.preview_image ? `<img src="${siteUrl}${post.frontmatter.preview_image}" alt="${post.frontmatter.title}" /><br/>` : ''}${post.content}]]></content> |
| 53 | + </entry>`).join('')} |
| 54 | +</feed>`; |
| 55 | + |
| 56 | + return feed; |
| 57 | +} |
| 58 | + |
| 59 | +export async function GET() { |
| 60 | + try { |
| 61 | + const posts = getAllPosts().slice(0, 20); // Limit to 20 most recent posts |
| 62 | + const atomFeed = generateAtomFeed(posts); |
| 63 | + |
| 64 | + return new NextResponse(atomFeed, { |
| 65 | + status: 200, |
| 66 | + headers: { |
| 67 | + 'Content-Type': 'application/atom+xml', |
| 68 | + 'Cache-Control': 's-maxage=3600, stale-while-revalidate', |
| 69 | + }, |
| 70 | + }); |
| 71 | + } catch (error) { |
| 72 | + console.error('Error generating Atom feed:', error); |
| 73 | + return new NextResponse('Error generating Atom feed', { status: 500 }); |
| 74 | + } |
| 75 | +} |
0 commit comments