|
| 1 | +import { blogApi } from '../../../lib/blog.api'; |
| 2 | + |
| 3 | +function escapeHtml(text: string): string { |
| 4 | + return text |
| 5 | + .replace(/&/g, '&') |
| 6 | + .replace(/</g, '<') |
| 7 | + .replace(/>/g, '>') |
| 8 | + .replace(/"/g, '"') |
| 9 | + .replace(/'/g, '''); |
| 10 | +} |
| 11 | + |
| 12 | +function getExcerpt(content: string, description?: string): string { |
| 13 | + if (description && description.trim().length > 0) { |
| 14 | + return description; |
| 15 | + } |
| 16 | + const paragraphs = content |
| 17 | + .split(/\n\n+/) |
| 18 | + .map((p) => p.trim()) |
| 19 | + .filter((p) => p.length > 0); |
| 20 | + return paragraphs.slice(0, 2).join('\n\n'); |
| 21 | +} |
| 22 | + |
| 23 | +export async function GET() { |
| 24 | + const posts = await blogApi.getBlogs((p) => !!p.published); |
| 25 | + const items = posts |
| 26 | + .map((post) => { |
| 27 | + const link = `https://nx.dev/blog/${post.slug}`; |
| 28 | + const excerpt = getExcerpt(post.content, post.description); |
| 29 | + const authors = |
| 30 | + post.authors && post.authors.length > 0 |
| 31 | + ? post.authors |
| 32 | + : [{ name: 'Nx Team', image: '', twitter: '', github: '' }]; |
| 33 | + const authorElements = authors |
| 34 | + .map( |
| 35 | + (author) => |
| 36 | + `\n <author>\n <name>${escapeHtml( |
| 37 | + author.name |
| 38 | + )}</name>\n </author>` |
| 39 | + ) |
| 40 | + .join(''); |
| 41 | + return `\n <entry>\n <title>${escapeHtml( |
| 42 | + post.title |
| 43 | + )}</title>\n <link href="${link}"/>\n <id>${link}</id>\n <updated>${new Date( |
| 44 | + post.date |
| 45 | + ).toISOString()}</updated>${authorElements}\n <summary><![CDATA[${excerpt}]]></summary>\n </entry>`; |
| 46 | + }) |
| 47 | + .join(''); |
| 48 | + |
| 49 | + const atom = `<?xml version="1.0" encoding="utf-8"?>\n<feed xmlns="http://www.w3.org/2005/Atom">\n <title>Nx Blog</title>\n <link href="https://nx.dev/blog"/>\n <link rel="self" href="https://nx.dev/blog/atom.xml"/>\n <id>https://nx.dev/blog</id>\n <updated>${new Date().toISOString()}</updated>\n <author>\n <name>Nx Team</name>\n <email>devrel@nrwl.io</email>\n </author>${items}\n</feed>`; |
| 50 | + |
| 51 | + return new Response(atom, { |
| 52 | + headers: { |
| 53 | + 'Content-Type': 'application/atom+xml', |
| 54 | + }, |
| 55 | + }); |
| 56 | +} |
0 commit comments