@rspress/plugin-rss 0.0.0-nightly-20240402160431
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +5 -0
- package/dist/index.cjs +317 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.ts +136 -0
- package/dist/index.mjs +272 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +67 -0
- package/static/global-components/FeedsAnnotations.tsx +24 -0
package/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2023-present Bytedance, Inc. and its affiliates.
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
package/dist/index.cjs
ADDED
@@ -0,0 +1,317 @@
|
|
1
|
+
"use strict";
|
2
|
+
var __create = Object.create;
|
3
|
+
var __defProp = Object.defineProperty;
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
8
|
+
var __export = (target, all) => {
|
9
|
+
for (var name in all)
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
11
|
+
};
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
14
|
+
for (let key of __getOwnPropNames(from))
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
17
|
+
}
|
18
|
+
return to;
|
19
|
+
};
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
26
|
+
mod
|
27
|
+
));
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
29
|
+
|
30
|
+
// src/index.ts
|
31
|
+
var src_exports = {};
|
32
|
+
__export(src_exports, {
|
33
|
+
PluginComponents: () => PluginComponents,
|
34
|
+
PluginName: () => PluginName,
|
35
|
+
createFeed: () => createFeed,
|
36
|
+
generateFeedItem: () => generateFeedItem,
|
37
|
+
getDefaultFeedOption: () => getDefaultFeedOption,
|
38
|
+
getFeedFileType: () => getFeedFileType,
|
39
|
+
getOutputInfo: () => getOutputInfo,
|
40
|
+
pluginRss: () => pluginRss,
|
41
|
+
testPage: () => testPage
|
42
|
+
});
|
43
|
+
module.exports = __toCommonJS(src_exports);
|
44
|
+
|
45
|
+
// src/plugin-rss.ts
|
46
|
+
var import_node_path = __toESM(require("path"));
|
47
|
+
var import_node_url3 = require("url");
|
48
|
+
var import_feed = require("feed");
|
49
|
+
|
50
|
+
// src/exports.ts
|
51
|
+
var PluginName = "@rspress/plugin-rss";
|
52
|
+
var PluginComponents = {
|
53
|
+
FeedsAnnotations: "@rspress/plugin-rss/FeedsAnnotations"
|
54
|
+
};
|
55
|
+
|
56
|
+
// src/feed.ts
|
57
|
+
var import_node_url = require("url");
|
58
|
+
|
59
|
+
// src/internals/lang.ts
|
60
|
+
function notNullish(n) {
|
61
|
+
return n !== void 0 && n !== null;
|
62
|
+
}
|
63
|
+
function concatArray(...arrList) {
|
64
|
+
return arrList.reduce(
|
65
|
+
(arr, item) => arr.concat((Array.isArray(item) ? item : [item]).filter(notNullish)),
|
66
|
+
[]
|
67
|
+
);
|
68
|
+
}
|
69
|
+
function selectNonNullishProperty(...list) {
|
70
|
+
for (const item of list) {
|
71
|
+
if (item === "")
|
72
|
+
return "";
|
73
|
+
if (item === 0)
|
74
|
+
return "0";
|
75
|
+
if (typeof item === "number")
|
76
|
+
return `${item}`;
|
77
|
+
if (typeof item === "string")
|
78
|
+
return item;
|
79
|
+
}
|
80
|
+
}
|
81
|
+
function toDate(s) {
|
82
|
+
const d = new Date(s);
|
83
|
+
return Number.isNaN(d.getDate()) ? null : d;
|
84
|
+
}
|
85
|
+
|
86
|
+
// src/internals/node.ts
|
87
|
+
var import_promises = require("fs/promises");
|
88
|
+
var NodePath = __toESM(require("path"));
|
89
|
+
async function writeFile(path, content) {
|
90
|
+
const dir = NodePath.dirname(path);
|
91
|
+
await (0, import_promises.mkdir)(dir, { mode: 493, recursive: true });
|
92
|
+
return (0, import_promises.writeFile)(path, content);
|
93
|
+
}
|
94
|
+
|
95
|
+
// src/feed.ts
|
96
|
+
function generateFeedItem(page, siteUrl) {
|
97
|
+
const { frontmatter: fm } = page;
|
98
|
+
return {
|
99
|
+
id: selectNonNullishProperty(fm.slug, fm.id, page.id) || "",
|
100
|
+
title: selectNonNullishProperty(fm.title, page.title) || "",
|
101
|
+
author: toAuthors(fm.author),
|
102
|
+
link: (0, import_node_url.resolve)(
|
103
|
+
siteUrl,
|
104
|
+
selectNonNullishProperty(fm.permalink, page.routePath) || ""
|
105
|
+
),
|
106
|
+
description: selectNonNullishProperty(fm.description) || "",
|
107
|
+
content: selectNonNullishProperty(fm.summary, page.content) || "",
|
108
|
+
date: toDate(fm.date || fm.published_at),
|
109
|
+
category: concatArray(fm.categories, fm.category).map(
|
110
|
+
(cat) => ({ name: cat })
|
111
|
+
)
|
112
|
+
};
|
113
|
+
}
|
114
|
+
function createFeed(options, config) {
|
115
|
+
const { output, item, id, title, ..._options } = options;
|
116
|
+
return {
|
117
|
+
id,
|
118
|
+
copyright: config.themeConfig?.footer?.message || "",
|
119
|
+
description: config.description || "",
|
120
|
+
link: output.url,
|
121
|
+
..._options,
|
122
|
+
title: title || config.title || ""
|
123
|
+
};
|
124
|
+
}
|
125
|
+
function toAuthors(author) {
|
126
|
+
const authors = (Array.isArray(author) ? author : [author]).filter(Boolean).map((author2) => ({
|
127
|
+
// email is mandatory for RSS 2.0.
|
128
|
+
...typeof author2 === "string" ? { name: author2 } : author2
|
129
|
+
}));
|
130
|
+
return authors.length ? authors : void 0;
|
131
|
+
}
|
132
|
+
|
133
|
+
// src/options.ts
|
134
|
+
var import_node_url2 = require("url");
|
135
|
+
function testPage(test, page, base = "/") {
|
136
|
+
if (Array.isArray(test)) {
|
137
|
+
return test.some((item) => testPage(item, page, base));
|
138
|
+
}
|
139
|
+
if (typeof test === "function") {
|
140
|
+
return test(page, base);
|
141
|
+
}
|
142
|
+
const routePath = page.routePath;
|
143
|
+
const pureRoutePath = `/${routePath.startsWith(base) ? routePath.slice(base.length) : routePath}`.replace(/^\/+/, "/");
|
144
|
+
if (typeof test === "string") {
|
145
|
+
return [routePath, pureRoutePath].some((path) => path.startsWith(test));
|
146
|
+
}
|
147
|
+
if (test instanceof RegExp) {
|
148
|
+
return [routePath, pureRoutePath].some((path) => test.test(path));
|
149
|
+
}
|
150
|
+
throw new Error(
|
151
|
+
"test must be of `RegExp` or `string` or `(page: PageIndexInfo, base: string) => boolean`"
|
152
|
+
);
|
153
|
+
}
|
154
|
+
function getDefaultFeedOption() {
|
155
|
+
return { id: "blog", test: "/blog/" };
|
156
|
+
}
|
157
|
+
function getFeedFileType(type) {
|
158
|
+
switch (type) {
|
159
|
+
case "rss":
|
160
|
+
return {
|
161
|
+
extension: "rss",
|
162
|
+
mime: "application/rss+xml",
|
163
|
+
getContent: (feed) => feed.rss2()
|
164
|
+
};
|
165
|
+
case "json":
|
166
|
+
return {
|
167
|
+
extension: "json",
|
168
|
+
mime: "application/json",
|
169
|
+
getContent: (feed) => feed.json1()
|
170
|
+
};
|
171
|
+
case "atom":
|
172
|
+
default:
|
173
|
+
return {
|
174
|
+
extension: "xml",
|
175
|
+
mime: "application/atom+xml",
|
176
|
+
getContent: (feed) => feed.atom1()
|
177
|
+
};
|
178
|
+
}
|
179
|
+
}
|
180
|
+
function getOutputInfo({ id, output }, {
|
181
|
+
siteUrl,
|
182
|
+
output: globalOutput
|
183
|
+
}) {
|
184
|
+
const type = output?.type || globalOutput?.type || "atom";
|
185
|
+
const { extension, mime, getContent } = getFeedFileType(type);
|
186
|
+
const filename = output?.filename || `${id}.${extension}`;
|
187
|
+
const dir = output?.dir || globalOutput?.dir || "rss";
|
188
|
+
const publicPath = output?.publicPath || globalOutput?.publicPath || siteUrl;
|
189
|
+
const url = [publicPath, `${dir}/`, filename].reduce(
|
190
|
+
(u, part) => u ? (0, import_node_url2.resolve)(u, part) : part
|
191
|
+
);
|
192
|
+
return { type, mime, filename, getContent, dir, publicPath, url };
|
193
|
+
}
|
194
|
+
|
195
|
+
// src/plugin-rss.ts
|
196
|
+
var FeedsSet = class {
|
197
|
+
constructor() {
|
198
|
+
this.feeds = [];
|
199
|
+
this.feedsMapById = /* @__PURE__ */ Object.create(null);
|
200
|
+
}
|
201
|
+
set({ feed, output, siteUrl }, config) {
|
202
|
+
this.feeds = (Array.isArray(feed) ? feed : [{ ...getDefaultFeedOption(), ...feed }]).map((options) => ({
|
203
|
+
title: config.title || "",
|
204
|
+
description: config.description || "",
|
205
|
+
favicon: config.icon && (0, import_node_url3.resolve)(siteUrl, config.icon),
|
206
|
+
copyright: config.themeConfig?.footer?.message || "",
|
207
|
+
link: siteUrl,
|
208
|
+
docs: "",
|
209
|
+
...options,
|
210
|
+
output: getOutputInfo(options, { siteUrl, output })
|
211
|
+
}));
|
212
|
+
this.feedsMapById = this.feeds.reduce(
|
213
|
+
(m, f) => ({ ...m, [f.id]: f }),
|
214
|
+
/* @__PURE__ */ Object.create(null)
|
215
|
+
);
|
216
|
+
}
|
217
|
+
get(id) {
|
218
|
+
if (id) {
|
219
|
+
return this.feedsMapById[id] || null;
|
220
|
+
}
|
221
|
+
return this.feeds.slice(0);
|
222
|
+
}
|
223
|
+
};
|
224
|
+
function getRssItems(feeds, page, config, siteUrl) {
|
225
|
+
return Promise.all(
|
226
|
+
feeds.filter((options) => testPage(options.test, page, config.base)).map(async (options) => {
|
227
|
+
const after = options.item || ((feed) => feed);
|
228
|
+
const item = await after(
|
229
|
+
generateFeedItem(page, siteUrl),
|
230
|
+
page,
|
231
|
+
siteUrl
|
232
|
+
);
|
233
|
+
return { ...item, channel: options.id };
|
234
|
+
})
|
235
|
+
);
|
236
|
+
}
|
237
|
+
function pluginRss(pluginRssOptions) {
|
238
|
+
const feedsSet = new FeedsSet();
|
239
|
+
let _rssWorkaround = null;
|
240
|
+
let _config;
|
241
|
+
return {
|
242
|
+
name: PluginName,
|
243
|
+
globalUIComponents: Object.values(PluginComponents),
|
244
|
+
beforeBuild(config, isProd) {
|
245
|
+
if (!isProd) {
|
246
|
+
_rssWorkaround = null;
|
247
|
+
return;
|
248
|
+
}
|
249
|
+
_rssWorkaround = {};
|
250
|
+
_config = config;
|
251
|
+
feedsSet.set(pluginRssOptions, config);
|
252
|
+
},
|
253
|
+
async extendPageData(_pageData) {
|
254
|
+
if (!_rssWorkaround)
|
255
|
+
return;
|
256
|
+
const pageData = _pageData;
|
257
|
+
_rssWorkaround[pageData.id] = _rssWorkaround[pageData.id] || getRssItems(
|
258
|
+
feedsSet.get(),
|
259
|
+
pageData,
|
260
|
+
_config,
|
261
|
+
pluginRssOptions.siteUrl
|
262
|
+
);
|
263
|
+
const feeds = await _rssWorkaround[pageData.id];
|
264
|
+
const showRssList = new Set(
|
265
|
+
concatArray(pageData.frontmatter["link-rss"])
|
266
|
+
);
|
267
|
+
for (const feed of feeds) {
|
268
|
+
showRssList.add(feed.channel);
|
269
|
+
}
|
270
|
+
pageData.feeds = Array.from(showRssList, (id) => {
|
271
|
+
const { output, language } = feedsSet.get(id);
|
272
|
+
return {
|
273
|
+
url: output.url,
|
274
|
+
mime: output.mime,
|
275
|
+
language: language || pageData.lang
|
276
|
+
};
|
277
|
+
});
|
278
|
+
},
|
279
|
+
async afterBuild(config) {
|
280
|
+
if (!_rssWorkaround)
|
281
|
+
return;
|
282
|
+
const items = concatArray(
|
283
|
+
...await Promise.all(Object.values(_rssWorkaround))
|
284
|
+
);
|
285
|
+
const feeds = /* @__PURE__ */ Object.create(null);
|
286
|
+
for (const { channel, ...item } of items) {
|
287
|
+
feeds[channel] = feeds[channel] || new import_feed.Feed(createFeed(feedsSet.get(channel), config));
|
288
|
+
feeds[channel].addItem(item);
|
289
|
+
}
|
290
|
+
for (const [channel, feed] of Object.entries(feeds)) {
|
291
|
+
const { output } = feedsSet.get(channel);
|
292
|
+
const path = import_node_path.default.resolve(
|
293
|
+
config.outDir || "doc_build",
|
294
|
+
output.dir,
|
295
|
+
output.filename
|
296
|
+
);
|
297
|
+
await writeFile(path, output.getContent(feed));
|
298
|
+
}
|
299
|
+
_rssWorkaround = null;
|
300
|
+
_config = null;
|
301
|
+
}
|
302
|
+
};
|
303
|
+
}
|
304
|
+
// Annotate the CommonJS export names for ESM import in node:
|
305
|
+
0 && (module.exports = {
|
306
|
+
PluginComponents,
|
307
|
+
PluginName,
|
308
|
+
createFeed,
|
309
|
+
generateFeedItem,
|
310
|
+
getDefaultFeedOption,
|
311
|
+
getFeedFileType,
|
312
|
+
getOutputInfo,
|
313
|
+
pluginRss,
|
314
|
+
testPage
|
315
|
+
});
|
316
|
+
|
317
|
+
//# sourceMappingURL=index.cjs.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,uBAAqB;AACrB,IAAAA,mBAAsC;AAEtC,kBAAqB;;;ACHd,IAAM,aAAa;AAEnB,IAAM,mBAAmB;AAAA,EAC9B,kBAAkB;AACpB;;;ACJA,sBAAsC;;;ACK/B,SAAS,WAAc,GAAiC;AAC7D,SAAO,MAAM,UAAa,MAAM;AAClC;AACO,SAAS,eAAkB,SAAkC;AAClE,SAAO,QAAQ;AAAA,IACb,CAAC,KAAK,SACJ,IAAI,QAAQ,MAAM,QAAQ,IAAI,IAAI,OAAO,CAAC,IAAI,GAAG,OAAO,UAAU,CAAC;AAAA,IACrE,CAAC;AAAA,EACH;AACF;AAEO,SAAS,4BAA4B,MAAiB;AAC3D,aAAW,QAAQ,MAAM;AACvB,QAAI,SAAS;AAAI,aAAO;AACxB,QAAI,SAAS;AAAG,aAAO;AACvB,QAAI,OAAO,SAAS;AAAU,aAAO,GAAG,IAAI;AAC5C,QAAI,OAAO,SAAS;AAAU,aAAO;AAAA,EACvC;AACF;AAEO,SAAS,OAAO,GAA+B;AACpD,QAAM,IAAI,IAAI,KAAK,CAAC;AACpB,SAAO,OAAO,MAAM,EAAE,QAAQ,CAAC,IAAI,OAAO;A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sB;AAAA,MACnE;AACA,iBAAW,QAAQ,OAAO;AACxB,oBAAY,IAAI,KAAK,OAAO;AAAA,MAC9B;AAEA,eAAS,QAAQ,MAAM,KAAK,aAAa,QAAM;AAC7C,cAAM,EAAE,QAAQ,SAAS,IAAI,SAAS,IAAI,EAAE;AAC5C,eAAO;AAAA,UACL,KAAK,OAAO;AAAA,UACZ,MAAM,OAAO;AAAA,UACb,UAAU,YAAY,SAAS;AAAA,QACjC;AAAA,MACF,CAAC;AAAA,IACH;AAAA,IACA,MAAM,WAAW,QAAQ;AACvB,UAAI,CAAC;AAAgB;AAErB,YAAM,QAAQ;AAAA,QACZ,GAAI,MAAM,QAAQ,IAAI,OAAO,OAAO,cAAc,CAAC;AAAA,MACrD;AACA,YAAM,QAA8B,uBAAO,OAAO,IAAI;AAEtD,iBAAW,EAAE,SAAS,GAAG,KAAK,KAAK,OAAO;AACxC,cAAM,OAAO,IACX,MAAM,OAAO,KACb,IAAI,iBAAK,WAAW,SAAS,IAAI,OAAO,GAAI,MAAM,CAAC;AACrD,cAAM,OAAO,EAAE,QAAQ,IAAI;AAAA,MAC7B;AAEA,iBAAW,CAAC,SAAS,IAAI,KAAK,OAAO,QAAQ,KAAK,GAAG;AACnD,cAAM,EAAE,OAAO,IAAI,SAAS,IAAI,OAAO;AAEvC,cAAM,OAAO,iBAAAE,QAAS;AAAA,UACpB,OAAO,UAAU;AAAA,UACjB,OAAO;AAAA,UACP,OAAO;AAAA,QACT;AACA,cAAM,UAAU,MAAM,OAAO,WAAW,IAAI,CAAC;AAAA,MAC/C;AACA,uBAAiB;AACjB,gBAAU;AAAA,IACZ;AAAA,EACF;AACF","names":["import_node_url","_writeFile","resolveUrl","author","NodePath"],"ignoreList":[],"sources":["../src/index.ts","../src/plugin-rss.ts","../src/exports.ts","../src/feed.ts","../src/internals/lang.ts","../src/internals/node.ts","../src/options.ts"],"sourcesContent":["export * from './plugin-rss';\nexport * from './type';\nexport * from './exports';\nexport * from './options';\nexport * from './feed';\n","import NodePath from 'node:path';\nimport { resolve as resolveUrl } from 'node:url';\nimport type { PageIndexInfo, RspressPlugin, UserConfig } from '@rspress/shared';\nimport { Feed } from 'feed';\nimport { PluginComponents, PluginName } from './exports';\nimport { createFeed, generateFeedItem } from './feed';\n\nimport {\n PageWithFeeds,\n ResolvedOutput,\n concatArray,\n writeFile,\n} from './internals';\nimport { getDefaultFeedOption, getOutputInfo, testPage } from './options';\nimport type { FeedChannel, FeedItem, PluginRssOptions } from './type';\n\ntype FeedItemWithChannel = FeedItem & { channel: string };\ntype TransformedFeedChannel = FeedChannel & { output: ResolvedOutput };\n\nclass FeedsSet {\n feeds: TransformedFeedChannel[] = [];\n feedsMapById: Record<string, TransformedFeedChannel> = Object.create(null);\n set({ feed, output, siteUrl }: PluginRssOptions, config: UserConfig) {\n this.feeds = (\n Array.isArray(feed) ? feed : [{ ...getDefaultFeedOption(), ...feed }]\n ).map(options => ({\n title: config.title || '',\n description: config.description || '',\n favicon: config.icon && resolveUrl(siteUrl, config.icon),\n copyright: config.themeConfig?.footer?.message || '',\n link: siteUrl,\n docs: '',\n ...options,\n output: getOutputInfo(options, { siteUrl, output }),\n }));\n\n this.feedsMapById = this.feeds.reduce(\n (m, f) => ({ ...m, [f.id]: f }),\n Object.create(null),\n );\n }\n get(): TransformedFeedChannel[];\n get(id: string): TransformedFeedChannel | null;\n get(id?: string): TransformedFeedChannel[] | TransformedFeedChannel | null {\n if (id) {\n return this.feedsMapById[id] || null;\n }\n return this.feeds.slice(0);\n }\n}\n\nfunction getRssItems(\n feeds: TransformedFeedChannel[],\n page: PageIndexInfo,\n config: UserConfig,\n siteUrl: string,\n): Promise<FeedItemWithChannel[]> {\n return Promise.all(\n feeds\n .filter(options => testPage(options.test, page, config.base))\n .map(async options => {\n const after = options.item || ((feed: FeedItem) => feed);\n const item = await after(\n generateFeedItem(page, siteUrl),\n page,\n siteUrl,\n );\n return { ...item, channel: options.id };\n }),\n );\n}\n\nexport function pluginRss(pluginRssOptions: PluginRssOptions): RspressPlugin {\n const feedsSet = new FeedsSet();\n\n /**\n * workaround for retrieving data of pages in `afterBuild`\n * TODO: get pageData list directly in `afterBuild`\n **/\n let _rssWorkaround: null | Record<\n string,\n PromiseLike<FeedItemWithChannel[]>\n > = null;\n let _config: null | UserConfig;\n\n return {\n name: PluginName,\n globalUIComponents: Object.values(PluginComponents),\n beforeBuild(config, isProd) {\n if (!isProd) {\n _rssWorkaround = null;\n return;\n }\n _rssWorkaround = {};\n _config = config;\n feedsSet.set(pluginRssOptions, config);\n },\n async extendPageData(_pageData) {\n if (!_rssWorkaround) return;\n\n const pageData = _pageData as PageWithFeeds;\n\n // rspress run `extendPageData` for each page\n // - let's cache rss items within a complete rspress build\n _rssWorkaround[pageData.id] =\n _rssWorkaround[pageData.id] ||\n getRssItems(\n feedsSet.get(),\n pageData,\n _config!,\n pluginRssOptions.siteUrl,\n );\n\n const feeds = await _rssWorkaround[pageData.id];\n const showRssList = new Set(\n concatArray(pageData.frontmatter['link-rss'] as string[] | string),\n );\n for (const feed of feeds) {\n showRssList.add(feed.channel);\n }\n\n pageData.feeds = Array.from(showRssList, id => {\n const { output, language } = feedsSet.get(id)!;\n return {\n url: output.url,\n mime: output.mime,\n language: language || pageData.lang,\n };\n });\n },\n async afterBuild(config) {\n if (!_rssWorkaround) return;\n\n const items = concatArray(\n ...(await Promise.all(Object.values(_rssWorkaround))),\n );\n const feeds: Record<string, Feed> = Object.create(null);\n\n for (const { channel, ...item } of items) {\n feeds[channel] =\n feeds[channel] ||\n new Feed(createFeed(feedsSet.get(channel)!, config));\n feeds[channel].addItem(item);\n }\n\n for (const [channel, feed] of Object.entries(feeds)) {\n const { output } = feedsSet.get(channel)!;\n\n const path = NodePath.resolve(\n config.outDir || 'doc_build',\n output.dir,\n output.filename,\n );\n await writeFile(path, output.getContent(feed));\n }\n _rssWorkaround = null;\n _config = null;\n },\n };\n}\n","export const PluginName = '@rspress/plugin-rss';\n\nexport const PluginComponents = {\n FeedsAnnotations: '@rspress/plugin-rss/FeedsAnnotations',\n} as const;\n","import { resolve as resolveUrl } from 'node:url';\nimport type { PageIndexInfo, UserConfig } from '@rspress/shared';\nimport type { Author, FeedOptions } from 'feed';\nimport {\n ResolvedOutput,\n concatArray,\n selectNonNullishProperty,\n toDate,\n} from './internals';\nimport type { FeedChannel, FeedItem } from './type';\n\n/**\n * @public\n * @param page Rspress Page Data\n * @param siteUrl\n */\nexport function generateFeedItem(page: PageIndexInfo, siteUrl: string) {\n const { frontmatter: fm } = page;\n return {\n id: selectNonNullishProperty(fm.slug, fm.id, page.id) || '',\n title: selectNonNullishProperty(fm.title, page.title) || '',\n author: toAuthors(fm.author),\n link: resolveUrl(\n siteUrl,\n selectNonNullishProperty(fm.permalink, page.routePath) || '',\n ),\n description: selectNonNullishProperty(fm.description) || '',\n content: selectNonNullishProperty(fm.summary, page.content) || '',\n date: toDate((fm.date as string) || (fm.published_at as string))!,\n category: concatArray(fm.categories as string[], fm.category as string).map(\n cat => ({ name: cat }),\n ),\n } satisfies FeedItem;\n}\n\nexport function createFeed(\n options: Omit<FeedChannel, 'test' | 'item' | 'output'> & {\n item?: any;\n test?: any;\n output: ResolvedOutput;\n },\n config: UserConfig,\n): FeedOptions {\n const { output, item, id, title, ..._options } = options;\n return {\n id,\n copyright: config.themeConfig?.footer?.message || '',\n description: config.description || '',\n link: output.url,\n ..._options,\n title: title || config.title || '',\n };\n}\n\nfunction toAuthors(author: unknown): Author[] | undefined {\n const authors = (Array.isArray(author) ? author : [author])\n .filter(Boolean)\n .map(author => ({\n // email is mandatory for RSS 2.0.\n ...(typeof author === 'string' ? { name: author } : author),\n }));\n return authors.length ? authors : undefined;\n}\n","export type PartialPartial<T, K extends keyof T> = Partial<Pick<T, K>> &\n Omit<T, K>;\n\nexport type ItemOf<T> = T extends Array<infer K> ? K : never;\n\nexport function notNullish<T>(n: T | undefined | null): n is T {\n return n !== undefined && n !== null;\n}\nexport function concatArray<T>(...arrList: (T[] | T | undefined)[]) {\n return arrList.reduce<T[]>(\n (arr, item) =>\n arr.concat((Array.isArray(item) ? item : [item]).filter(notNullish)),\n [] as T[],\n );\n}\n\nexport function selectNonNullishProperty(...list: unknown[]) {\n for (const item of list) {\n if (item === '') return '';\n if (item === 0) return '0';\n if (typeof item === 'number') return `${item}`;\n if (typeof item === 'string') return item;\n }\n}\n\nexport function toDate(s: string | Date): null | Date {\n const d = new Date(s);\n return Number.isNaN(d.getDate()) ? null : d;\n}\n","import { mkdir, writeFile as _writeFile } from 'node:fs/promises';\nimport * as NodePath from 'node:path';\n\nexport async function writeFile(path: string, content: string | Buffer) {\n const dir = NodePath.dirname(path);\n await mkdir(dir, { mode: 0o755, recursive: true });\n return _writeFile(path, content);\n}\n","import { resolve as resolveUrl } from 'node:url';\nimport type { PageIndexInfo } from '@rspress/shared';\nimport { Feed } from 'feed';\nimport type { ResolvedOutput } from './internals';\nimport type { FeedChannel, FeedOutputType, PluginRssOptions } from './type';\n\nexport function testPage(\n test: FeedChannel['test'],\n page: PageIndexInfo,\n base = '/',\n): boolean {\n if (Array.isArray(test)) {\n return test.some(item => testPage(item, page, base));\n }\n if (typeof test === 'function') {\n return test(page, base);\n }\n const routePath = page.routePath;\n const pureRoutePath = `/${\n routePath.startsWith(base) ? routePath.slice(base.length) : routePath\n }`.replace(/^\\/+/, '/');\n if (typeof test === 'string') {\n return [routePath, pureRoutePath].some(path => path.startsWith(test));\n }\n if (test instanceof RegExp) {\n return [routePath, pureRoutePath].some(path => test.test(path));\n }\n\n throw new Error(\n 'test must be of `RegExp` or `string` or `(page: PageIndexInfo, base: string) => boolean`',\n );\n}\n\nexport function getDefaultFeedOption() {\n return { id: 'blog', test: '/blog/' } satisfies FeedChannel;\n}\n\nexport function getFeedFileType(type: FeedOutputType) {\n switch (type) {\n case 'rss':\n return {\n extension: 'rss',\n mime: 'application/rss+xml',\n getContent: (feed: Feed) => feed.rss2(),\n };\n case 'json':\n return {\n extension: 'json',\n mime: 'application/json',\n getContent: (feed: Feed) => feed.json1(),\n };\n case 'atom':\n default:\n return {\n extension: 'xml',\n mime: 'application/atom+xml',\n getContent: (feed: Feed) => feed.atom1(),\n };\n }\n}\nexport function getOutputInfo(\n { id, output }: Pick<FeedChannel, 'id' | 'output'>,\n {\n siteUrl,\n output: globalOutput,\n }: Pick<PluginRssOptions, 'output' | 'siteUrl'>,\n): ResolvedOutput {\n const type = output?.type || globalOutput?.type || 'atom';\n const { extension, mime, getContent } = getFeedFileType(type);\n const filename = output?.filename || `${id}.${extension}`;\n const dir = output?.dir || globalOutput?.dir || 'rss';\n const publicPath = output?.publicPath || globalOutput?.publicPath || siteUrl;\n const url = [publicPath, `${dir}/`, filename].reduce((u, part) =>\n u ? resolveUrl(u, part) : part,\n );\n return { type, mime, filename, getContent, dir, publicPath, url };\n}\n"]}
|
package/dist/index.d.ts
ADDED
@@ -0,0 +1,136 @@
|
|
1
|
+
import { PageIndexInfo, RspressPlugin, UserConfig } from '@rspress/shared';
|
2
|
+
import { Feed, Item, FeedOptions, Author } from 'feed';
|
3
|
+
|
4
|
+
interface ResolvedOutput {
|
5
|
+
type: FeedOutputType;
|
6
|
+
mime: string;
|
7
|
+
filename: string;
|
8
|
+
getContent: (feed: Feed) => string;
|
9
|
+
dir: string;
|
10
|
+
publicPath: string;
|
11
|
+
url: string;
|
12
|
+
}
|
13
|
+
|
14
|
+
type PartialPartial<T, K extends keyof T> = Partial<Pick<T, K>> & Omit<T, K>;
|
15
|
+
|
16
|
+
/**
|
17
|
+
* feed information attached in `PageIndexInfo['feeds']` array
|
18
|
+
*/
|
19
|
+
interface PageFeedData {
|
20
|
+
url: string;
|
21
|
+
language: string;
|
22
|
+
mime: string;
|
23
|
+
}
|
24
|
+
type FeedItem = Item;
|
25
|
+
/**
|
26
|
+
* output feed file type
|
27
|
+
*/
|
28
|
+
type FeedOutputType = /** Atom 1.0 Feed */ 'atom' | /** RSS 2.0 Feed */ 'rss' | /** JSON1 Feed */ 'json';
|
29
|
+
/**
|
30
|
+
* output config of a feed.
|
31
|
+
* a feed will be written into path `${rspress.outDir || 'doc_build'}/${dir}/${filename}`
|
32
|
+
*/
|
33
|
+
interface FeedOutputOptions {
|
34
|
+
/**
|
35
|
+
* output dir of feed files, relative to rspress's outDir
|
36
|
+
*/
|
37
|
+
dir?: string;
|
38
|
+
/**
|
39
|
+
* type of feed files
|
40
|
+
*/
|
41
|
+
type?: FeedOutputType;
|
42
|
+
/**
|
43
|
+
* base filename of feed files. `${id}.${extension by type}` by default.
|
44
|
+
*/
|
45
|
+
filename?: string;
|
46
|
+
/**
|
47
|
+
* public path of feed files. siteUrl by default
|
48
|
+
*/
|
49
|
+
publicPath?: string;
|
50
|
+
}
|
51
|
+
interface FeedChannel extends PartialPartial<FeedOptions, 'title' | 'copyright'> {
|
52
|
+
/**
|
53
|
+
* used as the basename of rss file, should be unique
|
54
|
+
**/
|
55
|
+
id: string;
|
56
|
+
/**
|
57
|
+
* to match pages that should be listed in this feed
|
58
|
+
* if RegExp is given, it will match against the route path of each page
|
59
|
+
**/
|
60
|
+
test: RegExp | string | (RegExp | string)[] | ((item: PageIndexInfo, base: string) => boolean);
|
61
|
+
/**
|
62
|
+
* a function to modify feed item
|
63
|
+
* @param item pre-generated feed item
|
64
|
+
* @param page page data
|
65
|
+
* @param base base path of the rspress site
|
66
|
+
* @returns modified feed item
|
67
|
+
*/
|
68
|
+
item?: (item: FeedItem, page: PageIndexInfo, base: string) => FeedItem | PromiseLike<FeedItem>;
|
69
|
+
/**
|
70
|
+
* feed level output config
|
71
|
+
*/
|
72
|
+
output?: FeedOutputOptions;
|
73
|
+
}
|
74
|
+
/**
|
75
|
+
* plugin options for `pluginRss`
|
76
|
+
*/
|
77
|
+
interface PluginRssOptions {
|
78
|
+
/**
|
79
|
+
* site url of this rspress site. it will be used in feed files and feed link.
|
80
|
+
* @requires
|
81
|
+
*/
|
82
|
+
siteUrl: string;
|
83
|
+
/**
|
84
|
+
* Feed options for each rss. If array is given, this plugin will produce multiple feed files.
|
85
|
+
* @default {{ id: 'blog', test: /^\/blog\// }}
|
86
|
+
*/
|
87
|
+
feed?: PartialPartial<FeedChannel, 'id' | 'test'> | FeedChannel[];
|
88
|
+
/**
|
89
|
+
* output config for all feed files
|
90
|
+
*/
|
91
|
+
output?: Omit<FeedOutputOptions, 'filename'>;
|
92
|
+
}
|
93
|
+
|
94
|
+
declare function pluginRss(pluginRssOptions: PluginRssOptions): RspressPlugin;
|
95
|
+
|
96
|
+
declare const PluginName = "@rspress/plugin-rss";
|
97
|
+
declare const PluginComponents: {
|
98
|
+
readonly FeedsAnnotations: "@rspress/plugin-rss/FeedsAnnotations";
|
99
|
+
};
|
100
|
+
|
101
|
+
declare function testPage(test: FeedChannel['test'], page: PageIndexInfo, base?: string): boolean;
|
102
|
+
declare function getDefaultFeedOption(): {
|
103
|
+
id: string;
|
104
|
+
test: string;
|
105
|
+
};
|
106
|
+
declare function getFeedFileType(type: FeedOutputType): {
|
107
|
+
extension: string;
|
108
|
+
mime: string;
|
109
|
+
getContent: (feed: Feed) => string;
|
110
|
+
};
|
111
|
+
declare function getOutputInfo({ id, output }: Pick<FeedChannel, 'id' | 'output'>, { siteUrl, output: globalOutput, }: Pick<PluginRssOptions, 'output' | 'siteUrl'>): ResolvedOutput;
|
112
|
+
|
113
|
+
/**
|
114
|
+
* @public
|
115
|
+
* @param page Rspress Page Data
|
116
|
+
* @param siteUrl
|
117
|
+
*/
|
118
|
+
declare function generateFeedItem(page: PageIndexInfo, siteUrl: string): {
|
119
|
+
id: string;
|
120
|
+
title: string;
|
121
|
+
author: Author[] | undefined;
|
122
|
+
link: string;
|
123
|
+
description: string;
|
124
|
+
content: string;
|
125
|
+
date: Date;
|
126
|
+
category: {
|
127
|
+
name: string;
|
128
|
+
}[];
|
129
|
+
};
|
130
|
+
declare function createFeed(options: Omit<FeedChannel, 'test' | 'item' | 'output'> & {
|
131
|
+
item?: any;
|
132
|
+
test?: any;
|
133
|
+
output: ResolvedOutput;
|
134
|
+
}, config: UserConfig): FeedOptions;
|
135
|
+
|
136
|
+
export { type FeedChannel, type FeedItem, type FeedOutputOptions, type FeedOutputType, type PageFeedData, PluginComponents, PluginName, type PluginRssOptions, createFeed, generateFeedItem, getDefaultFeedOption, getFeedFileType, getOutputInfo, pluginRss, testPage };
|
package/dist/index.mjs
ADDED
@@ -0,0 +1,272 @@
|
|
1
|
+
// src/plugin-rss.ts
|
2
|
+
import NodePath2 from "path";
|
3
|
+
import { resolve as resolveUrl3 } from "url";
|
4
|
+
import { Feed } from "feed";
|
5
|
+
|
6
|
+
// src/exports.ts
|
7
|
+
var PluginName = "@rspress/plugin-rss";
|
8
|
+
var PluginComponents = {
|
9
|
+
FeedsAnnotations: "@rspress/plugin-rss/FeedsAnnotations"
|
10
|
+
};
|
11
|
+
|
12
|
+
// src/feed.ts
|
13
|
+
import { resolve as resolveUrl } from "url";
|
14
|
+
|
15
|
+
// src/internals/lang.ts
|
16
|
+
function notNullish(n) {
|
17
|
+
return n !== void 0 && n !== null;
|
18
|
+
}
|
19
|
+
function concatArray(...arrList) {
|
20
|
+
return arrList.reduce(
|
21
|
+
(arr, item) => arr.concat((Array.isArray(item) ? item : [item]).filter(notNullish)),
|
22
|
+
[]
|
23
|
+
);
|
24
|
+
}
|
25
|
+
function selectNonNullishProperty(...list) {
|
26
|
+
for (const item of list) {
|
27
|
+
if (item === "")
|
28
|
+
return "";
|
29
|
+
if (item === 0)
|
30
|
+
return "0";
|
31
|
+
if (typeof item === "number")
|
32
|
+
return `${item}`;
|
33
|
+
if (typeof item === "string")
|
34
|
+
return item;
|
35
|
+
}
|
36
|
+
}
|
37
|
+
function toDate(s) {
|
38
|
+
const d = new Date(s);
|
39
|
+
return Number.isNaN(d.getDate()) ? null : d;
|
40
|
+
}
|
41
|
+
|
42
|
+
// src/internals/node.ts
|
43
|
+
import { mkdir, writeFile as _writeFile } from "fs/promises";
|
44
|
+
import * as NodePath from "path";
|
45
|
+
async function writeFile(path, content) {
|
46
|
+
const dir = NodePath.dirname(path);
|
47
|
+
await mkdir(dir, { mode: 493, recursive: true });
|
48
|
+
return _writeFile(path, content);
|
49
|
+
}
|
50
|
+
|
51
|
+
// src/feed.ts
|
52
|
+
function generateFeedItem(page, siteUrl) {
|
53
|
+
const { frontmatter: fm } = page;
|
54
|
+
return {
|
55
|
+
id: selectNonNullishProperty(fm.slug, fm.id, page.id) || "",
|
56
|
+
title: selectNonNullishProperty(fm.title, page.title) || "",
|
57
|
+
author: toAuthors(fm.author),
|
58
|
+
link: resolveUrl(
|
59
|
+
siteUrl,
|
60
|
+
selectNonNullishProperty(fm.permalink, page.routePath) || ""
|
61
|
+
),
|
62
|
+
description: selectNonNullishProperty(fm.description) || "",
|
63
|
+
content: selectNonNullishProperty(fm.summary, page.content) || "",
|
64
|
+
date: toDate(fm.date || fm.published_at),
|
65
|
+
category: concatArray(fm.categories, fm.category).map(
|
66
|
+
(cat) => ({ name: cat })
|
67
|
+
)
|
68
|
+
};
|
69
|
+
}
|
70
|
+
function createFeed(options, config) {
|
71
|
+
const { output, item, id, title, ..._options } = options;
|
72
|
+
return {
|
73
|
+
id,
|
74
|
+
copyright: config.themeConfig?.footer?.message || "",
|
75
|
+
description: config.description || "",
|
76
|
+
link: output.url,
|
77
|
+
..._options,
|
78
|
+
title: title || config.title || ""
|
79
|
+
};
|
80
|
+
}
|
81
|
+
function toAuthors(author) {
|
82
|
+
const authors = (Array.isArray(author) ? author : [author]).filter(Boolean).map((author2) => ({
|
83
|
+
// email is mandatory for RSS 2.0.
|
84
|
+
...typeof author2 === "string" ? { name: author2 } : author2
|
85
|
+
}));
|
86
|
+
return authors.length ? authors : void 0;
|
87
|
+
}
|
88
|
+
|
89
|
+
// src/options.ts
|
90
|
+
import { resolve as resolveUrl2 } from "url";
|
91
|
+
function testPage(test, page, base = "/") {
|
92
|
+
if (Array.isArray(test)) {
|
93
|
+
return test.some((item) => testPage(item, page, base));
|
94
|
+
}
|
95
|
+
if (typeof test === "function") {
|
96
|
+
return test(page, base);
|
97
|
+
}
|
98
|
+
const routePath = page.routePath;
|
99
|
+
const pureRoutePath = `/${routePath.startsWith(base) ? routePath.slice(base.length) : routePath}`.replace(/^\/+/, "/");
|
100
|
+
if (typeof test === "string") {
|
101
|
+
return [routePath, pureRoutePath].some((path) => path.startsWith(test));
|
102
|
+
}
|
103
|
+
if (test instanceof RegExp) {
|
104
|
+
return [routePath, pureRoutePath].some((path) => test.test(path));
|
105
|
+
}
|
106
|
+
throw new Error(
|
107
|
+
"test must be of `RegExp` or `string` or `(page: PageIndexInfo, base: string) => boolean`"
|
108
|
+
);
|
109
|
+
}
|
110
|
+
function getDefaultFeedOption() {
|
111
|
+
return { id: "blog", test: "/blog/" };
|
112
|
+
}
|
113
|
+
function getFeedFileType(type) {
|
114
|
+
switch (type) {
|
115
|
+
case "rss":
|
116
|
+
return {
|
117
|
+
extension: "rss",
|
118
|
+
mime: "application/rss+xml",
|
119
|
+
getContent: (feed) => feed.rss2()
|
120
|
+
};
|
121
|
+
case "json":
|
122
|
+
return {
|
123
|
+
extension: "json",
|
124
|
+
mime: "application/json",
|
125
|
+
getContent: (feed) => feed.json1()
|
126
|
+
};
|
127
|
+
case "atom":
|
128
|
+
default:
|
129
|
+
return {
|
130
|
+
extension: "xml",
|
131
|
+
mime: "application/atom+xml",
|
132
|
+
getContent: (feed) => feed.atom1()
|
133
|
+
};
|
134
|
+
}
|
135
|
+
}
|
136
|
+
function getOutputInfo({ id, output }, {
|
137
|
+
siteUrl,
|
138
|
+
output: globalOutput
|
139
|
+
}) {
|
140
|
+
const type = output?.type || globalOutput?.type || "atom";
|
141
|
+
const { extension, mime, getContent } = getFeedFileType(type);
|
142
|
+
const filename = output?.filename || `${id}.${extension}`;
|
143
|
+
const dir = output?.dir || globalOutput?.dir || "rss";
|
144
|
+
const publicPath = output?.publicPath || globalOutput?.publicPath || siteUrl;
|
145
|
+
const url = [publicPath, `${dir}/`, filename].reduce(
|
146
|
+
(u, part) => u ? resolveUrl2(u, part) : part
|
147
|
+
);
|
148
|
+
return { type, mime, filename, getContent, dir, publicPath, url };
|
149
|
+
}
|
150
|
+
|
151
|
+
// src/plugin-rss.ts
|
152
|
+
var FeedsSet = class {
|
153
|
+
constructor() {
|
154
|
+
this.feeds = [];
|
155
|
+
this.feedsMapById = /* @__PURE__ */ Object.create(null);
|
156
|
+
}
|
157
|
+
set({ feed, output, siteUrl }, config) {
|
158
|
+
this.feeds = (Array.isArray(feed) ? feed : [{ ...getDefaultFeedOption(), ...feed }]).map((options) => ({
|
159
|
+
title: config.title || "",
|
160
|
+
description: config.description || "",
|
161
|
+
favicon: config.icon && resolveUrl3(siteUrl, config.icon),
|
162
|
+
copyright: config.themeConfig?.footer?.message || "",
|
163
|
+
link: siteUrl,
|
164
|
+
docs: "",
|
165
|
+
...options,
|
166
|
+
output: getOutputInfo(options, { siteUrl, output })
|
167
|
+
}));
|
168
|
+
this.feedsMapById = this.feeds.reduce(
|
169
|
+
(m, f) => ({ ...m, [f.id]: f }),
|
170
|
+
/* @__PURE__ */ Object.create(null)
|
171
|
+
);
|
172
|
+
}
|
173
|
+
get(id) {
|
174
|
+
if (id) {
|
175
|
+
return this.feedsMapById[id] || null;
|
176
|
+
}
|
177
|
+
return this.feeds.slice(0);
|
178
|
+
}
|
179
|
+
};
|
180
|
+
function getRssItems(feeds, page, config, siteUrl) {
|
181
|
+
return Promise.all(
|
182
|
+
feeds.filter((options) => testPage(options.test, page, config.base)).map(async (options) => {
|
183
|
+
const after = options.item || ((feed) => feed);
|
184
|
+
const item = await after(
|
185
|
+
generateFeedItem(page, siteUrl),
|
186
|
+
page,
|
187
|
+
siteUrl
|
188
|
+
);
|
189
|
+
return { ...item, channel: options.id };
|
190
|
+
})
|
191
|
+
);
|
192
|
+
}
|
193
|
+
function pluginRss(pluginRssOptions) {
|
194
|
+
const feedsSet = new FeedsSet();
|
195
|
+
let _rssWorkaround = null;
|
196
|
+
let _config;
|
197
|
+
return {
|
198
|
+
name: PluginName,
|
199
|
+
globalUIComponents: Object.values(PluginComponents),
|
200
|
+
beforeBuild(config, isProd) {
|
201
|
+
if (!isProd) {
|
202
|
+
_rssWorkaround = null;
|
203
|
+
return;
|
204
|
+
}
|
205
|
+
_rssWorkaround = {};
|
206
|
+
_config = config;
|
207
|
+
feedsSet.set(pluginRssOptions, config);
|
208
|
+
},
|
209
|
+
async extendPageData(_pageData) {
|
210
|
+
if (!_rssWorkaround)
|
211
|
+
return;
|
212
|
+
const pageData = _pageData;
|
213
|
+
_rssWorkaround[pageData.id] = _rssWorkaround[pageData.id] || getRssItems(
|
214
|
+
feedsSet.get(),
|
215
|
+
pageData,
|
216
|
+
_config,
|
217
|
+
pluginRssOptions.siteUrl
|
218
|
+
);
|
219
|
+
const feeds = await _rssWorkaround[pageData.id];
|
220
|
+
const showRssList = new Set(
|
221
|
+
concatArray(pageData.frontmatter["link-rss"])
|
222
|
+
);
|
223
|
+
for (const feed of feeds) {
|
224
|
+
showRssList.add(feed.channel);
|
225
|
+
}
|
226
|
+
pageData.feeds = Array.from(showRssList, (id) => {
|
227
|
+
const { output, language } = feedsSet.get(id);
|
228
|
+
return {
|
229
|
+
url: output.url,
|
230
|
+
mime: output.mime,
|
231
|
+
language: language || pageData.lang
|
232
|
+
};
|
233
|
+
});
|
234
|
+
},
|
235
|
+
async afterBuild(config) {
|
236
|
+
if (!_rssWorkaround)
|
237
|
+
return;
|
238
|
+
const items = concatArray(
|
239
|
+
...await Promise.all(Object.values(_rssWorkaround))
|
240
|
+
);
|
241
|
+
const feeds = /* @__PURE__ */ Object.create(null);
|
242
|
+
for (const { channel, ...item } of items) {
|
243
|
+
feeds[channel] = feeds[channel] || new Feed(createFeed(feedsSet.get(channel), config));
|
244
|
+
feeds[channel].addItem(item);
|
245
|
+
}
|
246
|
+
for (const [channel, feed] of Object.entries(feeds)) {
|
247
|
+
const { output } = feedsSet.get(channel);
|
248
|
+
const path = NodePath2.resolve(
|
249
|
+
config.outDir || "doc_build",
|
250
|
+
output.dir,
|
251
|
+
output.filename
|
252
|
+
);
|
253
|
+
await writeFile(path, output.getContent(feed));
|
254
|
+
}
|
255
|
+
_rssWorkaround = null;
|
256
|
+
_config = null;
|
257
|
+
}
|
258
|
+
};
|
259
|
+
}
|
260
|
+
export {
|
261
|
+
PluginComponents,
|
262
|
+
PluginName,
|
263
|
+
createFeed,
|
264
|
+
generateFeedItem,
|
265
|
+
getDefaultFeedOption,
|
266
|
+
getFeedFileType,
|
267
|
+
getOutputInfo,
|
268
|
+
pluginRss,
|
269
|
+
testPage
|
270
|
+
};
|
271
|
+
|
272
|
+
//# sourceMappingURL=index.mjs.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"mappings":";AAAA,OAAOA,eAAc;AACrB,SAAS,WAAWC,mBAAkB;AAEtC,SAAS,YAAY;;;ACHd,IAAM,aAAa;AAEnB,IAAM,mBAAmB;AAAA,EAC9B,kBAAkB;AACpB;;;ACJA,SAAS,WAAW,kBAAkB;;;ACK/B,SAAS,WAAc,GAAiC;AAC7D,SAAO,MAAM,UAAa,MAAM;AAClC;AACO,SAAS,eAAkB,SAAkC;AAClE,SAAO,QAAQ;AAAA,IACb,CAAC,KAAK,SACJ,IAAI,QAAQ,MAAM,QAAQ,IAAI,IAAI,OAAO,CAAC,IAAI,GAAG,OAAO,UAAU,CAAC;AAAA,IACrE,CAAC;AAAA,EACH;AACF;AAEO,SAAS,4BAA4B,MAAiB;AAC3D,aAAW,QAAQ,MAAM;AACvB,QAAI,SAAS;AAAI,aAAO;AACxB,QAAI,SAAS;AAAG,aAAO;AACvB,QAAI,OAAO,SAAS;AAAU,aAAO,GAAG,IAAI;AAC5C,QAAI,OAAO,SAAS;AAAU,aAAO;AAAA,EACvC;AACF;AAEO,SAAS,OAAO,GAA+B;AACpD,QAAM,IAAI,IAAI,KAAK,CAAC;AACpB,SAAO,OAAO,MAAM,EAAE,QAAQ,CAAC,IAAI,OAAO;AAC5C;;;AC5BA,SAAS,OAAO,aAAa,kBAAkB;AAC/C,YAAY,cAAc;AAE1B,eAAsB,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C9B;AAEA,eAAS,QAAQ,MAAM,KAAK,aAAa,QAAM;AAC7C,cAAM,EAAE,QAAQ,SAAS,IAAI,SAAS,IAAI,EAAE;AAC5C,eAAO;AAAA,UACL,KAAK,OAAO;AAAA,UACZ,MAAM,OAAO;AAAA,UACb,UAAU,YAAY,SAAS;AAAA,QACjC;AAAA,MACF,CAAC;AAAA,IACH;AAAA,IACA,MAAM,WAAW,QAAQ;AACvB,UAAI,CAAC;AAAgB;AAErB,YAAM,QAAQ;AAAA,QACZ,GAAI,MAAM,QAAQ,IAAI,OAAO,OAAO,cAAc,CAAC;AAAA,MACrD;AACA,YAAM,QAA8B,uBAAO,OAAO,IAAI;AAEtD,iBAAW,EAAE,SAAS,GAAG,KAAK,KAAK,OAAO;AACxC,cAAM,OAAO,IACX,MAAM,OAAO,KACb,IAAI,KAAK,WAAW,SAAS,IAAI,OAAO,GAAI,MAAM,CAAC;AACrD,cAAM,OAAO,EAAE,QAAQ,IAAI;AAAA,MAC7B;AAEA,iBAAW,CAAC,SAAS,IAAI,KAAK,OAAO,QAAQ,KAAK,GAAG;AACnD,cAAM,EAAE,OAAO,IAAI,SAAS,IAAI,OAAO;AAEvC,cAAM,OAAOD,UAAS;AAAA,UACpB,OAAO,UAAU;AAAA,UACjB,OAAO;AAAA,UACP,OAAO;AAAA,QACT;AACA,cAAM,UAAU,MAAM,OAAO,WAAW,IAAI,CAAC;AAAA,MAC/C;AACA,uBAAiB;AACjB,gBAAU;AAAA,IACZ;AAAA,EACF;AACF","names":["NodePath","resolveUrl","author"],"ignoreList":[],"sources":["../src/plugin-rss.ts","../src/exports.ts","../src/feed.ts","../src/internals/lang.ts","../src/internals/node.ts","../src/options.ts"],"sourcesContent":["import NodePath from 'node:path';\nimport { resolve as resolveUrl } from 'node:url';\nimport type { PageIndexInfo, RspressPlugin, UserConfig } from '@rspress/shared';\nimport { Feed } from 'feed';\nimport { PluginComponents, PluginName } from './exports';\nimport { createFeed, generateFeedItem } from './feed';\n\nimport {\n PageWithFeeds,\n ResolvedOutput,\n concatArray,\n writeFile,\n} from './internals';\nimport { getDefaultFeedOption, getOutputInfo, testPage } from './options';\nimport type { FeedChannel, FeedItem, PluginRssOptions } from './type';\n\ntype FeedItemWithChannel = FeedItem & { channel: string };\ntype TransformedFeedChannel = FeedChannel & { output: ResolvedOutput };\n\nclass FeedsSet {\n feeds: TransformedFeedChannel[] = [];\n feedsMapById: Record<string, TransformedFeedChannel> = Object.create(null);\n set({ feed, output, siteUrl }: PluginRssOptions, config: UserConfig) {\n this.feeds = (\n Array.isArray(feed) ? feed : [{ ...getDefaultFeedOption(), ...feed }]\n ).map(options => ({\n title: config.title || '',\n description: config.description || '',\n favicon: config.icon && resolveUrl(siteUrl, config.icon),\n copyright: config.themeConfig?.footer?.message || '',\n link: siteUrl,\n docs: '',\n ...options,\n output: getOutputInfo(options, { siteUrl, output }),\n }));\n\n this.feedsMapById = this.feeds.reduce(\n (m, f) => ({ ...m, [f.id]: f }),\n Object.create(null),\n );\n }\n get(): TransformedFeedChannel[];\n get(id: string): TransformedFeedChannel | null;\n get(id?: string): TransformedFeedChannel[] | TransformedFeedChannel | null {\n if (id) {\n return this.feedsMapById[id] || null;\n }\n return this.feeds.slice(0);\n }\n}\n\nfunction getRssItems(\n feeds: TransformedFeedChannel[],\n page: PageIndexInfo,\n config: UserConfig,\n siteUrl: string,\n): Promise<FeedItemWithChannel[]> {\n return Promise.all(\n feeds\n .filter(options => testPage(options.test, page, config.base))\n .map(async options => {\n const after = options.item || ((feed: FeedItem) => feed);\n const item = await after(\n generateFeedItem(page, siteUrl),\n page,\n siteUrl,\n );\n return { ...item, channel: options.id };\n }),\n );\n}\n\nexport function pluginRss(pluginRssOptions: PluginRssOptions): RspressPlugin {\n const feedsSet = new FeedsSet();\n\n /**\n * workaround for retrieving data of pages in `afterBuild`\n * TODO: get pageData list directly in `afterBuild`\n **/\n let _rssWorkaround: null | Record<\n string,\n PromiseLike<FeedItemWithChannel[]>\n > = null;\n let _config: null | UserConfig;\n\n return {\n name: PluginName,\n globalUIComponents: Object.values(PluginComponents),\n beforeBuild(config, isProd) {\n if (!isProd) {\n _rssWorkaround = null;\n return;\n }\n _rssWorkaround = {};\n _config = config;\n feedsSet.set(pluginRssOptions, config);\n },\n async extendPageData(_pageData) {\n if (!_rssWorkaround) return;\n\n const pageData = _pageData as PageWithFeeds;\n\n // rspress run `extendPageData` for each page\n // - let's cache rss items within a complete rspress build\n _rssWorkaround[pageData.id] =\n _rssWorkaround[pageData.id] ||\n getRssItems(\n feedsSet.get(),\n pageData,\n _config!,\n pluginRssOptions.siteUrl,\n );\n\n const feeds = await _rssWorkaround[pageData.id];\n const showRssList = new Set(\n concatArray(pageData.frontmatter['link-rss'] as string[] | string),\n );\n for (const feed of feeds) {\n showRssList.add(feed.channel);\n }\n\n pageData.feeds = Array.from(showRssList, id => {\n const { output, language } = feedsSet.get(id)!;\n return {\n url: output.url,\n mime: output.mime,\n language: language || pageData.lang,\n };\n });\n },\n async afterBuild(config) {\n if (!_rssWorkaround) return;\n\n const items = concatArray(\n ...(await Promise.all(Object.values(_rssWorkaround))),\n );\n const feeds: Record<string, Feed> = Object.create(null);\n\n for (const { channel, ...item } of items) {\n feeds[channel] =\n feeds[channel] ||\n new Feed(createFeed(feedsSet.get(channel)!, config));\n feeds[channel].addItem(item);\n }\n\n for (const [channel, feed] of Object.entries(feeds)) {\n const { output } = feedsSet.get(channel)!;\n\n const path = NodePath.resolve(\n config.outDir || 'doc_build',\n output.dir,\n output.filename,\n );\n await writeFile(path, output.getContent(feed));\n }\n _rssWorkaround = null;\n _config = null;\n },\n };\n}\n","export const PluginName = '@rspress/plugin-rss';\n\nexport const PluginComponents = {\n FeedsAnnotations: '@rspress/plugin-rss/FeedsAnnotations',\n} as const;\n","import { resolve as resolveUrl } from 'node:url';\nimport type { PageIndexInfo, UserConfig } from '@rspress/shared';\nimport type { Author, FeedOptions } from 'feed';\nimport {\n ResolvedOutput,\n concatArray,\n selectNonNullishProperty,\n toDate,\n} from './internals';\nimport type { FeedChannel, FeedItem } from './type';\n\n/**\n * @public\n * @param page Rspress Page Data\n * @param siteUrl\n */\nexport function generateFeedItem(page: PageIndexInfo, siteUrl: string) {\n const { frontmatter: fm } = page;\n return {\n id: selectNonNullishProperty(fm.slug, fm.id, page.id) || '',\n title: selectNonNullishProperty(fm.title, page.title) || '',\n author: toAuthors(fm.author),\n link: resolveUrl(\n siteUrl,\n selectNonNullishProperty(fm.permalink, page.routePath) || '',\n ),\n description: selectNonNullishProperty(fm.description) || '',\n content: selectNonNullishProperty(fm.summary, page.content) || '',\n date: toDate((fm.date as string) || (fm.published_at as string))!,\n category: concatArray(fm.categories as string[], fm.category as string).map(\n cat => ({ name: cat }),\n ),\n } satisfies FeedItem;\n}\n\nexport function createFeed(\n options: Omit<FeedChannel, 'test' | 'item' | 'output'> & {\n item?: any;\n test?: any;\n output: ResolvedOutput;\n },\n config: UserConfig,\n): FeedOptions {\n const { output, item, id, title, ..._options } = options;\n return {\n id,\n copyright: config.themeConfig?.footer?.message || '',\n description: config.description || '',\n link: output.url,\n ..._options,\n title: title || config.title || '',\n };\n}\n\nfunction toAuthors(author: unknown): Author[] | undefined {\n const authors = (Array.isArray(author) ? author : [author])\n .filter(Boolean)\n .map(author => ({\n // email is mandatory for RSS 2.0.\n ...(typeof author === 'string' ? { name: author } : author),\n }));\n return authors.length ? authors : undefined;\n}\n","export type PartialPartial<T, K extends keyof T> = Partial<Pick<T, K>> &\n Omit<T, K>;\n\nexport type ItemOf<T> = T extends Array<infer K> ? K : never;\n\nexport function notNullish<T>(n: T | undefined | null): n is T {\n return n !== undefined && n !== null;\n}\nexport function concatArray<T>(...arrList: (T[] | T | undefined)[]) {\n return arrList.reduce<T[]>(\n (arr, item) =>\n arr.concat((Array.isArray(item) ? item : [item]).filter(notNullish)),\n [] as T[],\n );\n}\n\nexport function selectNonNullishProperty(...list: unknown[]) {\n for (const item of list) {\n if (item === '') return '';\n if (item === 0) return '0';\n if (typeof item === 'number') return `${item}`;\n if (typeof item === 'string') return item;\n }\n}\n\nexport function toDate(s: string | Date): null | Date {\n const d = new Date(s);\n return Number.isNaN(d.getDate()) ? null : d;\n}\n","import { mkdir, writeFile as _writeFile } from 'node:fs/promises';\nimport * as NodePath from 'node:path';\n\nexport async function writeFile(path: string, content: string | Buffer) {\n const dir = NodePath.dirname(path);\n await mkdir(dir, { mode: 0o755, recursive: true });\n return _writeFile(path, content);\n}\n","import { resolve as resolveUrl } from 'node:url';\nimport type { PageIndexInfo } from '@rspress/shared';\nimport { Feed } from 'feed';\nimport type { ResolvedOutput } from './internals';\nimport type { FeedChannel, FeedOutputType, PluginRssOptions } from './type';\n\nexport function testPage(\n test: FeedChannel['test'],\n page: PageIndexInfo,\n base = '/',\n): boolean {\n if (Array.isArray(test)) {\n return test.some(item => testPage(item, page, base));\n }\n if (typeof test === 'function') {\n return test(page, base);\n }\n const routePath = page.routePath;\n const pureRoutePath = `/${\n routePath.startsWith(base) ? routePath.slice(base.length) : routePath\n }`.replace(/^\\/+/, '/');\n if (typeof test === 'string') {\n return [routePath, pureRoutePath].some(path => path.startsWith(test));\n }\n if (test instanceof RegExp) {\n return [routePath, pureRoutePath].some(path => test.test(path));\n }\n\n throw new Error(\n 'test must be of `RegExp` or `string` or `(page: PageIndexInfo, base: string) => boolean`',\n );\n}\n\nexport function getDefaultFeedOption() {\n return { id: 'blog', test: '/blog/' } satisfies FeedChannel;\n}\n\nexport function getFeedFileType(type: FeedOutputType) {\n switch (type) {\n case 'rss':\n return {\n extension: 'rss',\n mime: 'application/rss+xml',\n getContent: (feed: Feed) => feed.rss2(),\n };\n case 'json':\n return {\n extension: 'json',\n mime: 'application/json',\n getContent: (feed: Feed) => feed.json1(),\n };\n case 'atom':\n default:\n return {\n extension: 'xml',\n mime: 'application/atom+xml',\n getContent: (feed: Feed) => feed.atom1(),\n };\n }\n}\nexport function getOutputInfo(\n { id, output }: Pick<FeedChannel, 'id' | 'output'>,\n {\n siteUrl,\n output: globalOutput,\n }: Pick<PluginRssOptions, 'output' | 'siteUrl'>,\n): ResolvedOutput {\n const type = output?.type || globalOutput?.type || 'atom';\n const { extension, mime, getContent } = getFeedFileType(type);\n const filename = output?.filename || `${id}.${extension}`;\n const dir = output?.dir || globalOutput?.dir || 'rss';\n const publicPath = output?.publicPath || globalOutput?.publicPath || siteUrl;\n const url = [publicPath, `${dir}/`, filename].reduce((u, part) =>\n u ? resolveUrl(u, part) : part,\n );\n return { type, mime, filename, getContent, dir, publicPath, url };\n}\n"]}
|
package/package.json
ADDED
@@ -0,0 +1,67 @@
|
|
1
|
+
{
|
2
|
+
"name": "@rspress/plugin-rss",
|
3
|
+
"version": "0.0.0-nightly-20240402160431",
|
4
|
+
"description": "A plugin for rss generation for rspress",
|
5
|
+
"bugs": "https://github.com/web-infra-dev/rspress/issues",
|
6
|
+
"repository": {
|
7
|
+
"type": "git",
|
8
|
+
"url": "https://github.com/web-infra-dev/rspress",
|
9
|
+
"directory": "packages/plugin-rss"
|
10
|
+
},
|
11
|
+
"license": "MIT",
|
12
|
+
"jsnext:source": "./src/index.ts",
|
13
|
+
"types": "./dist/index.d.ts",
|
14
|
+
"main": "./dist/index.cjs",
|
15
|
+
"module": "./dist/index.mjs",
|
16
|
+
"exports": {
|
17
|
+
".": {
|
18
|
+
"import": "./dist/index.mjs",
|
19
|
+
"types": "./dist/index.d.ts",
|
20
|
+
"default": "./dist/index.cjs"
|
21
|
+
},
|
22
|
+
"./FeedsAnnotations": "./static/global-components/FeedsAnnotations"
|
23
|
+
},
|
24
|
+
"engines": {
|
25
|
+
"node": ">=14.17.6"
|
26
|
+
},
|
27
|
+
"dependencies": {
|
28
|
+
"feed": "^4.2.2"
|
29
|
+
},
|
30
|
+
"devDependencies": {
|
31
|
+
"@types/node": "^18.11.17",
|
32
|
+
"@types/react": "^18",
|
33
|
+
"react": "^18",
|
34
|
+
"typescript": "^5",
|
35
|
+
"@rspress/runtime": "0.0.0-nightly-20240402160431",
|
36
|
+
"@rspress/shared": "0.0.0-nightly-20240402160431"
|
37
|
+
},
|
38
|
+
"peerDependencies": {
|
39
|
+
"react": ">=17.0.0",
|
40
|
+
"@types/react": ">=17.0.0",
|
41
|
+
"@rspress/runtime": "0.0.0-nightly-20240402160431"
|
42
|
+
},
|
43
|
+
"files": [
|
44
|
+
"dist",
|
45
|
+
"static"
|
46
|
+
],
|
47
|
+
"publishConfig": {
|
48
|
+
"access": "public",
|
49
|
+
"provenance": true,
|
50
|
+
"registry": "https://registry.npmjs.org/"
|
51
|
+
},
|
52
|
+
"scripts": {
|
53
|
+
"dev": "modern build -w",
|
54
|
+
"build": "modern build",
|
55
|
+
"reset": "rimraf ./**/node_modules",
|
56
|
+
"lint": "modern lint",
|
57
|
+
"change": "modern change",
|
58
|
+
"bump": "modern bump",
|
59
|
+
"pre": "modern pre",
|
60
|
+
"change-status": "modern change-status",
|
61
|
+
"gen-release-note": "modern gen-release-note",
|
62
|
+
"release": "modern release",
|
63
|
+
"new": "modern new",
|
64
|
+
"test": "vitest run --passWithNoTests",
|
65
|
+
"upgrade": "modern upgrade"
|
66
|
+
}
|
67
|
+
}
|
@@ -0,0 +1,24 @@
|
|
1
|
+
import type { PageFeedData } from '@rspress/plugin-rss';
|
2
|
+
import { Helmet, usePageData } from '@rspress/runtime';
|
3
|
+
import { LinkHTMLAttributes } from 'react';
|
4
|
+
|
5
|
+
export default function FeedsAnnotations() {
|
6
|
+
const { page } = usePageData();
|
7
|
+
const feeds = (page.feeds as PageFeedData[]) || [];
|
8
|
+
|
9
|
+
return (
|
10
|
+
<Helmet>
|
11
|
+
{feeds.map(({ language, url, mime }) => {
|
12
|
+
const props: LinkHTMLAttributes<HTMLLinkElement> = {
|
13
|
+
rel: 'alternative',
|
14
|
+
type: mime,
|
15
|
+
href: url,
|
16
|
+
};
|
17
|
+
if (language) {
|
18
|
+
props.hrefLang = language;
|
19
|
+
}
|
20
|
+
return <link {...props} />;
|
21
|
+
})}
|
22
|
+
</Helmet>
|
23
|
+
);
|
24
|
+
}
|