-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrss.js
More file actions
168 lines (147 loc) · 6.29 KB
/
rss.js
File metadata and controls
168 lines (147 loc) · 6.29 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
// XML转义函数
function escapeXml(unsafe) {
if (!unsafe) return '';
return String(unsafe)
.replace(/&/g, '&')
.replace(/</g, '<')
.replace(/>/g, '>')
.replace(/"/g, '"')
.replace(/'/g, ''');
}
export function itemsToRss(items, channel, format = 'atom') {
switch (format) {
case 'atom':
return generateAtom(items, channel);
case 'json':
return generateJsonFeed(items, channel);
case 'rss':
return generateRss(items, channel);
}
}
function sanitizeCdata(text) {
if (!text) return '';
return text.replace(/]]>/g, ']]]]><![CDATA[>');
}
function generateRss(items, channel) {
const itemsXml = items.map(item => {
// title和link的文本内容需要转义,因为它们是XML文本节点
const title = item.title ? escapeXml(item.title) : 'Untitled';
const link = item.link || '#';
const description = item.description ? `<![CDATA[${sanitizeCdata(item.description)}]]>` : '';
const pubDate = item.pubDate ? new Date(item.pubDate).toUTCString() : new Date().toUTCString();
const guid = item.guid || item.link || '#';
const author = item.author ? `<author>${escapeXml(item.author)}</author>` : '';
// 只在有完整enclosure信息时才添加enclosure标签
const enclosure = item.enclosure?.url ?
`<enclosure url="${escapeXml(item.enclosure.url)}" length="${item.enclosure.length || 0}" type="${escapeXml(item.enclosure.type || 'application/octet-stream')}" />`
: '';
return `
<item>
<title>${title}</title>
<link>${link}</link>
${description ? `<description>${description}</description>` : ''}
<pubDate>${pubDate}</pubDate>
<guid isPermaLink="${item.guid_isPermaLink !== false && item.link}">${guid}</guid>
${author}
${enclosure}
</item>`;
}).join("\n");
const channelTitle = channel.title ? escapeXml(channel.title) : 'RSS Feed';
const channelLink = channel.link || '#';
const channelDesc = channel.description ? escapeXml(channel.description) : '';
const imageXml = channel.image?.url ? `
<image>
<url>${channel.image.url}</url>
<title>${channel.image.title ? escapeXml(channel.image.title) : escapeXml(channel.title || 'Image')}</title>
<link>${channel.image.link || channel.link || '#'}</link>
</image>` : '';
return `<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
<channel>
<title>${channelTitle}</title>
<link>${channelLink}</link>
<description>${channelDesc}</description>
<language>${channel.language || "zh-CN"}</language>
<lastBuildDate>${new Date().toUTCString()}</lastBuildDate>
${imageXml}
${itemsXml}
</channel>
</rss>`;
}
function generateAtom(items, channel) {
const entriesXml = items.map(item => {
const title = item.title ? escapeXml(item.title) : 'Untitled';
const link = item.link || '#';
const id = item.guid || item.link || `urn:uuid:${Date.now()}-${Math.random()}`;
const published = item.pubDate ? new Date(item.pubDate).toISOString() : new Date().toISOString();
const updated = item.updated ? new Date(item.updated).toISOString() : published;
const author = item.author ? `<author><name>${escapeXml(item.author)}</name></author>` : '';
const summary = item.description ? `<summary type="html"><![CDATA[${sanitizeCdata(item.description).substring(0, 200)}]]></summary>` : '';
const content = item.description ? `<content type="html"><![CDATA[${sanitizeCdata(item.description)}]]></content>` : '';
return `
<entry>
<title>${title}</title>
<link href="${link}" />
<id>${id}</id>
<published>${published}</published>
<updated>${updated}</updated>
${summary}
${content}
${author}
</entry>`;
}).join("");
const feedTitle = channel.title ? escapeXml(channel.title) : 'Atom Feed';
const feedLink = channel.link || '#';
const feedId = channel.id || channel.link || `urn:uuid:${Date.now()}`;
const feedSubtitle = channel.description ? escapeXml(channel.description) : '';
const feedUpdated = items.length > 0 && items[0].pubDate ? new Date(items[0].pubDate).toISOString() : new Date().toISOString();
const feedIcon = channel.image ? `<icon>${channel.image}</icon>` : '';
const feedLogo = channel.image ? `<logo>${channel.image}</logo>` : '';
return `<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
<title>${feedTitle}</title>
<link href="${feedLink}" />
<link href="${feedLink}" rel="self" />
<id>${feedId}</id>
<updated>${feedUpdated}</updated>
${feedSubtitle ? `<subtitle>${feedSubtitle}</subtitle>` : ''}
${feedLogo}
${feedIcon}${entriesXml}
</feed>`;
}
function generateJsonFeed(items, channel) {
const jsonFeed = {
version: "https://jsonfeed.org/version/1.1",
title: channel.title || "JSON Feed",
home_page_url: channel.link || "",
feed_url: channel.link,
description: channel.description || "",
items: items.map(item => {
const feedItem = {
id: item.guid || item.link,
url: item.link || "",
title: item.title || "Untitled",
content_html: item.description || "",
date_published: item.pubDate ? new Date(item.pubDate).toISOString() : new Date().toISOString()
};
if (item.author) {
feedItem.author = { name: item.author };
}
if (channel.image) {
feedItem.image = channel.image;
}
// 添加summary(从description生成,去除CDATA)
if (item.description) {
const plainText = item.description.replace(/<!\[CDATA\[(.*?)\]\]>/s, '$1').replace(/<[^>]*>/g, '');
feedItem.summary = plainText.substring(0, 200);
}
return feedItem;
})
};
// 只在有值时添加可选字段
if (channel.image) {
jsonFeed.icon = channel.image;
jsonFeed.favicon = channel.image;
}
return JSON.stringify(jsonFeed, null, 2);
}