-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathfeed.xml.get.ts
More file actions
213 lines (178 loc) · 6.19 KB
/
feed.xml.get.ts
File metadata and controls
213 lines (178 loc) · 6.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
import type { ApiFunctionsContext, PageStaticData } from '@redocly/config';
import { readSharedData } from '@redocly/realm/dist/server/utils/shared-data.js';
const BLOG_SLUG = '/blog/';
const RSS_ITEMS_LIMIT = 50;
const ALL_POSTS_SHARED_DATA_ID = 'blog-posts';
type BlogCategory = { label: string };
type BlogAuthor = { name?: string };
type BlogPost = {
slug: string;
title: string;
description?: string;
date: string;
author?: BlogAuthor;
categories: BlogCategory[];
};
function escapeXml(unsafe: string): string {
return unsafe
.replace(/&/g, '&')
.replace(/</g, '<')
.replace(/>/g, '>')
.replace(/"/g, '"')
.replace(/'/g, ''');
}
function escapeXmlForCategories(unsafe: string): string {
return unsafe
.replace(/&(?!(amp|lt|gt|quot|apos);)/g, '&')
.replace(/</g, '<');
}
function formatRssDate(timestamp: string): string {
return new Date(timestamp).toUTCString();
}
function buildRssItem(post: BlogPost, origin: string): string {
const link = `${origin}${post.slug}`;
const pubDate = formatRssDate(post.date);
const categoriesArray = Array.isArray(post.categories) ? post.categories : [];
const categoryLabels = categoriesArray
.map((cat) => (typeof cat?.label === 'string' ? cat.label : null))
.filter((label): label is string => Boolean(label));
const categoriesXml = categoryLabels
.map(label => `<category>${escapeXmlForCategories(label)}</category>`)
.join('');
const author = escapeXml(post.author?.name || 'Redocly Team');
const description = `<p>${(post.description || '').replace(/]]>/g, ']]>')}</p>`;
return `
<item>
<title>${escapeXml(post.title)}</title>
<link>${escapeXml(link)}</link>
<guid isPermaLink="true">${escapeXml(link)}</guid>
<pubDate>${pubDate}</pubDate>
${categoriesXml}
<description><![CDATA[${description}]]></description>
<author>${author}</author>
</item>
`;
}
function mapCategory(category: any): BlogCategory | null {
if (!category || typeof category !== 'object') {
return null;
}
const mainLabel = typeof category.category?.label === 'string' ? category.category.label : '';
const subLabel = typeof category.subcategory?.label === 'string' ? category.subcategory.label : '';
if (mainLabel && subLabel) {
return { label: `${mainLabel} > ${subLabel}` };
}
if (mainLabel) {
return { label: mainLabel };
}
return null;
}
function mapToBlogPost(post: any): BlogPost | null {
if (!post) {
return null;
}
const postDate = post.publishedDate || post.date;
const slug = typeof post.slug === 'string' ? post.slug : '';
const title = typeof post.title === 'string' ? post.title : '';
if (!slug || !title || !postDate) {
return null;
}
const categoriesList = Array.isArray(post.categories) ? post.categories : [];
const categories: BlogCategory[] = categoriesList
.map(mapCategory)
.filter((cat): cat is BlogCategory => cat !== null);
let authorName: string | undefined;
if (typeof post.author === 'object' && typeof post.author?.name === 'string') {
authorName = post.author.name;
} else if (typeof post.author === 'string') {
authorName = post.author;
}
const author: BlogAuthor = { name: authorName || 'Redocly Team' };
return {
slug,
title,
description: typeof post.description === 'string' ? post.description : '',
date: postDate,
author,
categories,
};
}
async function readBlogPosts(outdir?: string): Promise<BlogPost[]> {
if (!outdir) {
console.warn('[Blog RSS] Missing outdir. Cannot read shared blog posts data.');
return [];
}
try {
const sharedData = await readSharedData(ALL_POSTS_SHARED_DATA_ID, outdir);
if (!sharedData) {
console.warn('[Blog RSS] No shared data found for blog posts.');
return [];
}
const postsSource = Array.isArray((sharedData as any).posts)
? (sharedData as any).posts
: Array.isArray(sharedData)
? sharedData
: [];
if (!Array.isArray(postsSource)) {
console.warn('[Blog RSS] Shared data does not contain a posts array.');
return [];
}
return postsSource
.map(mapToBlogPost)
.filter((post): post is BlogPost => Boolean(post));
} catch (error) {
console.error('[Blog RSS] Failed to read shared blog posts data:', error);
return [];
}
}
export default async function blogRssHandler(
request: Request,
context: ApiFunctionsContext,
staticData: PageStaticData
) {
try {
const outdir =
typeof staticData?.props?.outdir === 'string' ? staticData.props.outdir : undefined;
const posts = await readBlogPosts(outdir);
const sortedPosts = posts
.filter((post) => Boolean(post.date))
.sort((a, b) => new Date(b.date).getTime() - new Date(a.date).getTime())
.slice(0, RSS_ITEMS_LIMIT);
const url = new URL(request.url);
const origin = `${url.protocol}//${url.host}`;
const rssItems = sortedPosts.map((post) => buildRssItem(post, origin)).join('');
const rssXml = `<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
<channel>
<title>Redocly Blog</title>
<link>${escapeXml(origin + BLOG_SLUG)}</link>
<description>Latest posts from the Redocly blog.</description>
<language>en-us</language>
<lastBuildDate>${formatRssDate(new Date().toISOString())}</lastBuildDate>
<atom:link href="${escapeXml(request.url)}" rel="self" type="application/rss+xml"/>
${rssItems}
</channel>
</rss>`;
return new Response(rssXml, {
status: 200,
headers: {
'Content-Type': 'application/rss+xml; charset=utf-8',
'Cache-Control': 'no-store, no-cache, must-revalidate',
Pragma: 'no-cache',
Expires: '0',
},
});
} catch (error) {
const errorMessage = error instanceof Error ? error.message : String(error);
const errorStack = error instanceof Error ? error.stack : 'No stack trace';
console.error('[Blog RSS] Error generating RSS feed:', {
message: errorMessage,
stack: errorStack,
});
return context.status(500).json({
error: 'Internal server error',
message: 'Failed to generate blog RSS feed',
details: errorMessage,
});
}
}