@docusaurus/plugin-content-blog 3.3.2 → 3.5.0

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.
Files changed (62) hide show
  1. package/assets/atom.css +75 -0
  2. package/assets/atom.xsl +92 -0
  3. package/assets/rss.css +75 -0
  4. package/assets/rss.xsl +86 -0
  5. package/lib/authors.d.ts +9 -11
  6. package/lib/authors.js +42 -64
  7. package/lib/authorsMap.d.ts +23 -0
  8. package/lib/authorsMap.js +116 -0
  9. package/lib/authorsProblems.d.ts +21 -0
  10. package/lib/authorsProblems.js +51 -0
  11. package/lib/authorsSocials.d.ts +10 -0
  12. package/lib/authorsSocials.js +48 -0
  13. package/lib/blogUtils.d.ts +7 -12
  14. package/lib/blogUtils.js +44 -34
  15. package/lib/client/contexts.d.ts +33 -0
  16. package/lib/client/contexts.js +54 -0
  17. package/lib/client/index.d.ts +3 -3
  18. package/lib/client/index.js +3 -9
  19. package/lib/client/sidebarUtils.d.ts +21 -0
  20. package/lib/client/sidebarUtils.js +49 -0
  21. package/lib/client/sidebarUtils.test.d.ts +7 -0
  22. package/lib/client/sidebarUtils.test.js +43 -0
  23. package/lib/client/structuredDataUtils.d.ts +10 -0
  24. package/lib/client/structuredDataUtils.js +122 -0
  25. package/lib/feed.d.ts +8 -3
  26. package/lib/feed.js +111 -20
  27. package/lib/frontMatter.d.ts +0 -1
  28. package/lib/frontMatter.js +3 -2
  29. package/lib/index.d.ts +0 -1
  30. package/lib/index.js +132 -105
  31. package/lib/markdownLoader.js +3 -7
  32. package/lib/options.d.ts +4 -1
  33. package/lib/options.js +107 -26
  34. package/lib/props.d.ts +9 -2
  35. package/lib/props.js +23 -3
  36. package/lib/remark/footnoteIDFixer.js +1 -1
  37. package/lib/routes.d.ts +0 -1
  38. package/lib/routes.js +82 -14
  39. package/lib/translations.d.ts +0 -1
  40. package/lib/translations.js +2 -3
  41. package/lib/types.d.ts +1 -8
  42. package/package.json +13 -10
  43. package/src/authors.ts +56 -93
  44. package/src/authorsMap.ts +171 -0
  45. package/src/authorsProblems.ts +72 -0
  46. package/src/authorsSocials.ts +64 -0
  47. package/src/blogUtils.ts +51 -46
  48. package/src/client/contexts.tsx +95 -0
  49. package/src/client/index.tsx +24 -0
  50. package/src/client/sidebarUtils.test.ts +52 -0
  51. package/src/client/sidebarUtils.tsx +85 -0
  52. package/src/client/structuredDataUtils.ts +178 -0
  53. package/src/feed.ts +197 -18
  54. package/src/frontMatter.ts +2 -0
  55. package/src/index.ts +182 -137
  56. package/src/markdownLoader.ts +3 -7
  57. package/src/options.ts +132 -32
  58. package/src/plugin-content-blog.d.ts +252 -113
  59. package/src/props.ts +41 -1
  60. package/src/routes.ts +102 -12
  61. package/src/types.ts +1 -6
  62. package/src/client/index.ts +0 -20
package/src/feed.ts CHANGED
@@ -7,22 +7,29 @@
7
7
 
8
8
  import path from 'path';
9
9
  import fs from 'fs-extra';
10
- import logger from '@docusaurus/logger';
11
10
  import {Feed, type Author as FeedAuthor} from 'feed';
12
11
  import * as srcset from 'srcset';
13
- import {normalizeUrl, readOutputHTMLFile} from '@docusaurus/utils';
12
+ import {
13
+ getDataFilePath,
14
+ normalizeUrl,
15
+ readOutputHTMLFile,
16
+ } from '@docusaurus/utils';
14
17
  import {
15
18
  blogPostContainerID,
16
19
  applyTrailingSlash,
17
20
  } from '@docusaurus/utils-common';
18
21
  import {load as cheerioLoad} from 'cheerio';
19
- import type {DocusaurusConfig} from '@docusaurus/types';
22
+ import logger from '@docusaurus/logger';
23
+ import type {BlogContentPaths} from './types';
24
+ import type {DocusaurusConfig, HtmlTags, LoadContext} from '@docusaurus/types';
20
25
  import type {
21
26
  FeedType,
22
27
  PluginOptions,
23
28
  Author,
24
29
  BlogPost,
25
30
  BlogFeedItem,
31
+ FeedOptions,
32
+ FeedXSLTOptions,
26
33
  } from '@docusaurus/plugin-content-blog';
27
34
 
28
35
  async function generateBlogFeed({
@@ -180,32 +187,144 @@ async function defaultCreateFeedItems({
180
187
  );
181
188
  }
182
189
 
190
+ async function resolveXsltFilePaths({
191
+ xsltFilePath,
192
+ contentPaths,
193
+ }: {
194
+ xsltFilePath: string;
195
+ contentPaths: BlogContentPaths;
196
+ }) {
197
+ const xsltAbsolutePath: string = path.isAbsolute(xsltFilePath)
198
+ ? xsltFilePath
199
+ : (await getDataFilePath({filePath: xsltFilePath, contentPaths})) ??
200
+ path.resolve(contentPaths.contentPath, xsltFilePath);
201
+
202
+ if (!(await fs.pathExists(xsltAbsolutePath))) {
203
+ throw new Error(
204
+ logger.interpolate`Blog feed XSLT file not found at path=${path.relative(
205
+ process.cwd(),
206
+ xsltAbsolutePath,
207
+ )}`,
208
+ );
209
+ }
210
+
211
+ const parsedPath = path.parse(xsltAbsolutePath);
212
+ const cssAbsolutePath = path.resolve(
213
+ parsedPath.dir,
214
+ `${parsedPath.name}.css`,
215
+ );
216
+ if (!(await fs.pathExists(xsltAbsolutePath))) {
217
+ throw new Error(
218
+ logger.interpolate`Blog feed XSLT file was found at path=${path.relative(
219
+ process.cwd(),
220
+ xsltAbsolutePath,
221
+ )}
222
+ But its expected co-located CSS file could not be found at path=${path.relative(
223
+ process.cwd(),
224
+ cssAbsolutePath,
225
+ )}
226
+ If you want to provide a custom XSLT file, you must provide a CSS file with the exact same name.`,
227
+ );
228
+ }
229
+
230
+ return {xsltAbsolutePath, cssAbsolutePath};
231
+ }
232
+
233
+ async function generateXsltFiles({
234
+ xsltFilePath,
235
+ generatePath,
236
+ contentPaths,
237
+ }: {
238
+ xsltFilePath: string;
239
+ generatePath: string;
240
+ contentPaths: BlogContentPaths;
241
+ }) {
242
+ const {xsltAbsolutePath, cssAbsolutePath} = await resolveXsltFilePaths({
243
+ xsltFilePath,
244
+ contentPaths,
245
+ });
246
+ const xsltOutputPath = path.join(
247
+ generatePath,
248
+ path.basename(xsltAbsolutePath),
249
+ );
250
+ const cssOutputPath = path.join(generatePath, path.basename(cssAbsolutePath));
251
+ await fs.copy(xsltAbsolutePath, xsltOutputPath);
252
+ await fs.copy(cssAbsolutePath, cssOutputPath);
253
+ }
254
+
255
+ // This modifies the XML feed content to add a relative href to the XSLT file
256
+ // Good enough for now: we probably don't need a full XML parser just for that
257
+ // See also https://darekkay.com/blog/rss-styling/
258
+ function injectXslt({
259
+ feedContent,
260
+ xsltFilePath,
261
+ }: {
262
+ feedContent: string;
263
+ xsltFilePath: string;
264
+ }) {
265
+ return feedContent.replace(
266
+ '<?xml version="1.0" encoding="utf-8"?>',
267
+ `<?xml version="1.0" encoding="utf-8"?><?xml-stylesheet type="text/xsl" href="${path.basename(
268
+ xsltFilePath,
269
+ )}"?>`,
270
+ );
271
+ }
272
+
273
+ const FeedConfigs: Record<
274
+ FeedType,
275
+ {
276
+ outputFileName: string;
277
+ getContent: (feed: Feed) => string;
278
+ getXsltFilePath: (xslt: FeedXSLTOptions) => string | null;
279
+ }
280
+ > = {
281
+ rss: {
282
+ outputFileName: 'rss.xml',
283
+ getContent: (feed) => feed.rss2(),
284
+ getXsltFilePath: (xslt) => xslt.rss,
285
+ },
286
+ atom: {
287
+ outputFileName: 'atom.xml',
288
+ getContent: (feed) => feed.atom1(),
289
+ getXsltFilePath: (xslt) => xslt.atom,
290
+ },
291
+ json: {
292
+ outputFileName: 'feed.json',
293
+ getContent: (feed) => feed.json1(),
294
+ getXsltFilePath: () => null,
295
+ },
296
+ };
297
+
183
298
  async function createBlogFeedFile({
184
299
  feed,
185
300
  feedType,
186
301
  generatePath,
302
+ feedOptions,
303
+ contentPaths,
187
304
  }: {
188
305
  feed: Feed;
189
306
  feedType: FeedType;
190
307
  generatePath: string;
308
+ feedOptions: FeedOptions;
309
+ contentPaths: BlogContentPaths;
191
310
  }) {
192
- const [feedContent, feedPath] = (() => {
193
- switch (feedType) {
194
- case 'rss':
195
- return [feed.rss2(), 'rss.xml'];
196
- case 'json':
197
- return [feed.json1(), 'feed.json'];
198
- case 'atom':
199
- return [feed.atom1(), 'atom.xml'];
200
- default:
201
- throw new Error(`Feed type ${feedType} not supported.`);
202
- }
203
- })();
204
311
  try {
205
- await fs.outputFile(path.join(generatePath, feedPath), feedContent);
312
+ const feedConfig = FeedConfigs[feedType];
313
+
314
+ let feedContent = feedConfig.getContent(feed);
315
+
316
+ const xsltFilePath = feedConfig.getXsltFilePath(feedOptions.xslt);
317
+ if (xsltFilePath) {
318
+ await generateXsltFiles({xsltFilePath, contentPaths, generatePath});
319
+ feedContent = injectXslt({feedContent, xsltFilePath});
320
+ }
321
+
322
+ const outputPath = path.join(generatePath, feedConfig.outputFileName);
323
+ await fs.outputFile(outputPath, feedContent);
206
324
  } catch (err) {
207
- logger.error(`Generating ${feedType} feed failed.`);
208
- throw err;
325
+ throw new Error(`Generating ${feedType} feed failed.`, {
326
+ cause: err as Error,
327
+ });
209
328
  }
210
329
  }
211
330
 
@@ -222,12 +341,14 @@ export async function createBlogFeedFiles({
222
341
  siteConfig,
223
342
  outDir,
224
343
  locale,
344
+ contentPaths,
225
345
  }: {
226
346
  blogPosts: BlogPost[];
227
347
  options: PluginOptions;
228
348
  siteConfig: DocusaurusConfig;
229
349
  outDir: string;
230
350
  locale: string;
351
+ contentPaths: BlogContentPaths;
231
352
  }): Promise<void> {
232
353
  const blogPosts = allBlogPosts.filter(shouldBeInFeed);
233
354
 
@@ -250,7 +371,65 @@ export async function createBlogFeedFiles({
250
371
  feed,
251
372
  feedType,
252
373
  generatePath: path.join(outDir, options.routeBasePath),
374
+ feedOptions: options.feedOptions,
375
+ contentPaths,
253
376
  }),
254
377
  ),
255
378
  );
256
379
  }
380
+
381
+ export function createFeedHtmlHeadTags({
382
+ context,
383
+ options,
384
+ }: {
385
+ context: LoadContext;
386
+ options: PluginOptions;
387
+ }): HtmlTags {
388
+ const feedTypes = options.feedOptions.type;
389
+ if (!feedTypes) {
390
+ return [];
391
+ }
392
+ const feedTitle = options.feedOptions.title ?? context.siteConfig.title;
393
+ const feedsConfig = {
394
+ rss: {
395
+ type: 'application/rss+xml',
396
+ path: 'rss.xml',
397
+ title: `${feedTitle} RSS Feed`,
398
+ },
399
+ atom: {
400
+ type: 'application/atom+xml',
401
+ path: 'atom.xml',
402
+ title: `${feedTitle} Atom Feed`,
403
+ },
404
+ json: {
405
+ type: 'application/json',
406
+ path: 'feed.json',
407
+ title: `${feedTitle} JSON Feed`,
408
+ },
409
+ };
410
+ const headTags: HtmlTags = [];
411
+
412
+ feedTypes.forEach((feedType) => {
413
+ const {
414
+ type,
415
+ path: feedConfigPath,
416
+ title: feedConfigTitle,
417
+ } = feedsConfig[feedType];
418
+
419
+ headTags.push({
420
+ tagName: 'link',
421
+ attributes: {
422
+ rel: 'alternate',
423
+ type,
424
+ href: normalizeUrl([
425
+ context.siteConfig.baseUrl,
426
+ options.routeBasePath,
427
+ feedConfigPath,
428
+ ]),
429
+ title: feedConfigTitle,
430
+ },
431
+ });
432
+ });
433
+
434
+ return headTags;
435
+ }
@@ -13,6 +13,7 @@ import {
13
13
  URISchema,
14
14
  validateFrontMatter,
15
15
  } from '@docusaurus/utils-validation';
16
+ import {AuthorSocialsSchema} from './authorsSocials';
16
17
  import type {BlogPostFrontMatter} from '@docusaurus/plugin-content-blog';
17
18
 
18
19
  const BlogPostFrontMatterAuthorSchema = Joi.object({
@@ -21,6 +22,7 @@ const BlogPostFrontMatterAuthorSchema = Joi.object({
21
22
  title: Joi.string(),
22
23
  url: URISchema,
23
24
  imageURL: Joi.string(),
25
+ socials: AuthorSocialsSchema,
24
26
  })
25
27
  .or('key', 'name', 'imageURL')
26
28
  .rename('image_url', 'imageURL', {alias: true});