@docusaurus/plugin-content-blog 2.0.0-beta.17 → 2.0.0-beta.18

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/src/feed.ts CHANGED
@@ -13,7 +13,7 @@ import {
13
13
  mapAsyncSequential,
14
14
  readOutputHTMLFile,
15
15
  } from '@docusaurus/utils';
16
- import cheerio from 'cheerio';
16
+ import {load as cheerioLoad} from 'cheerio';
17
17
  import type {DocusaurusConfig} from '@docusaurus/types';
18
18
  import path from 'path';
19
19
  import fs from 'fs-extra';
@@ -29,11 +29,13 @@ async function generateBlogFeed({
29
29
  options,
30
30
  siteConfig,
31
31
  outDir,
32
+ locale,
32
33
  }: {
33
34
  blogPosts: BlogPost[];
34
35
  options: PluginOptions;
35
36
  siteConfig: DocusaurusConfig;
36
37
  outDir: string;
38
+ locale: string;
37
39
  }): Promise<Feed | null> {
38
40
  if (!blogPosts.length) {
39
41
  return null;
@@ -43,15 +45,15 @@ async function generateBlogFeed({
43
45
  const {url: siteUrl, baseUrl, title, favicon} = siteConfig;
44
46
  const blogBaseUrl = normalizeUrl([siteUrl, baseUrl, routeBasePath]);
45
47
 
46
- const updated = blogPosts[0] && blogPosts[0].metadata.date;
48
+ const updated = blogPosts[0]?.metadata.date;
47
49
 
48
50
  const feed = new Feed({
49
51
  id: blogBaseUrl,
50
- title: feedOptions.title || `${title} Blog`,
52
+ title: feedOptions.title ?? `${title} Blog`,
51
53
  updated,
52
- language: feedOptions.language,
54
+ language: feedOptions.language ?? locale,
53
55
  link: blogBaseUrl,
54
- description: feedOptions.description || `${siteConfig.title} Blog`,
56
+ description: feedOptions.description ?? `${siteConfig.title} Blog`,
55
57
  favicon: favicon ? normalizeUrl([siteUrl, baseUrl, favicon]) : undefined,
56
58
  copyright: feedOptions.copyright,
57
59
  });
@@ -78,7 +80,7 @@ async function generateBlogFeed({
78
80
  outDir,
79
81
  siteConfig.trailingSlash,
80
82
  );
81
- const $ = cheerio.load(content);
83
+ const $ = cheerioLoad(content);
82
84
 
83
85
  const feedItem: FeedItem = {
84
86
  title: metadataTitle,
@@ -140,17 +142,20 @@ export async function createBlogFeedFiles({
140
142
  options,
141
143
  siteConfig,
142
144
  outDir,
145
+ locale,
143
146
  }: {
144
147
  blogPosts: BlogPost[];
145
148
  options: PluginOptions;
146
149
  siteConfig: DocusaurusConfig;
147
150
  outDir: string;
151
+ locale: string;
148
152
  }): Promise<void> {
149
153
  const feed = await generateBlogFeed({
150
154
  blogPosts,
151
155
  options,
152
156
  siteConfig,
153
157
  outDir,
158
+ locale,
154
159
  });
155
160
 
156
161
  const feedTypes = options.feedOptions.type;
@@ -74,8 +74,8 @@ const BlogFrontMatterSchema = Joi.object<BlogPostFrontMatter>({
74
74
  '{#label} blog frontMatter field is deprecated. Please use {#alternative} instead.',
75
75
  });
76
76
 
77
- export function validateBlogPostFrontMatter(
78
- frontMatter: Record<string, unknown>,
79
- ): BlogPostFrontMatter {
77
+ export function validateBlogPostFrontMatter(frontMatter: {
78
+ [key: string]: unknown;
79
+ }): BlogPostFrontMatter {
80
80
  return validateFrontMatter(frontMatter, BlogFrontMatterSchema);
81
81
  }
package/src/index.ts CHANGED
@@ -26,22 +26,11 @@ import type {
26
26
  BlogTag,
27
27
  BlogTags,
28
28
  BlogContent,
29
- BlogItemsToMetadata,
30
- TagsModule,
31
29
  BlogPaginated,
32
30
  BlogContentPaths,
33
31
  BlogMarkdownLoaderOptions,
34
- MetaData,
35
- TagModule,
36
32
  } from './types';
37
- import {PluginOptionSchema} from './pluginOptionSchema';
38
- import type {
39
- LoadContext,
40
- Plugin,
41
- HtmlTags,
42
- OptionValidationContext,
43
- ValidationResult,
44
- } from '@docusaurus/types';
33
+ import type {LoadContext, Plugin, HtmlTags} from '@docusaurus/types';
45
34
  import {
46
35
  generateBlogPosts,
47
36
  getSourceToPermalink,
@@ -52,7 +41,9 @@ import {createBlogFeedFiles} from './feed';
52
41
  import type {
53
42
  PluginOptions,
54
43
  BlogPostFrontMatter,
44
+ BlogPostMetadata,
55
45
  Assets,
46
+ TagModule,
56
47
  } from '@docusaurus/plugin-content-blog';
57
48
 
58
49
  export default async function pluginContentBlog(
@@ -214,14 +205,14 @@ export default async function pluginContentBlog(
214
205
  blogTagsListPath,
215
206
  } = blogContents;
216
207
 
217
- const blogItemsToMetadata: BlogItemsToMetadata = {};
208
+ const blogItemsToMetadata: {[postId: string]: BlogPostMetadata} = {};
218
209
 
219
210
  const sidebarBlogPosts =
220
211
  options.blogSidebarCount === 'ALL'
221
212
  ? blogPosts
222
213
  : blogPosts.slice(0, options.blogSidebarCount);
223
214
 
224
- if (archiveBasePath) {
215
+ if (archiveBasePath && blogPosts.length) {
225
216
  const archiveUrl = normalizeUrl([
226
217
  baseUrl,
227
218
  routeBasePath,
@@ -307,7 +298,7 @@ export default async function pluginContentBlog(
307
298
  ({
308
299
  content: {
309
300
  __import: true,
310
- path: blogItemsToMetadata[postID].source,
301
+ path: blogItemsToMetadata[postID]!.source,
311
302
  query: {
312
303
  truncated: true,
313
304
  },
@@ -325,11 +316,10 @@ export default async function pluginContentBlog(
325
316
  return;
326
317
  }
327
318
 
328
- const tagsModule: TagsModule = Object.fromEntries(
329
- Object.entries(blogTags).map(([tagKey, tag]) => {
319
+ const tagsModule: {[tagName: string]: TagModule} = Object.fromEntries(
320
+ Object.entries(blogTags).map(([, tag]) => {
330
321
  const tagModule: TagModule = {
331
322
  allTagsPath: blogTagsListPath,
332
- slug: tagKey,
333
323
  name: tag.name,
334
324
  count: tag.items.length,
335
325
  permalink: tag.permalink,
@@ -359,7 +349,7 @@ export default async function pluginContentBlog(
359
349
  modules: {
360
350
  sidebar: aliasedSource(sidebarProp),
361
351
  items: items.map((postID) => {
362
- const blogPostMetadata = blogItemsToMetadata[postID];
352
+ const blogPostMetadata = blogItemsToMetadata[postID]!;
363
353
  return {
364
354
  content: {
365
355
  __import: true,
@@ -479,7 +469,7 @@ export default async function pluginContentBlog(
479
469
  metadata,
480
470
  }: {
481
471
  frontMatter: BlogPostFrontMatter;
482
- metadata: MetaData;
472
+ metadata: BlogPostMetadata;
483
473
  }): Assets => ({
484
474
  image: frontMatter.image,
485
475
  authorsImageUrls: metadata.authors.map(
@@ -512,6 +502,7 @@ export default async function pluginContentBlog(
512
502
  options,
513
503
  outDir,
514
504
  siteConfig,
505
+ locale: currentLocale,
515
506
  });
516
507
  },
517
508
 
@@ -546,13 +537,11 @@ export default async function pluginContentBlog(
546
537
  const headTags: HtmlTags = [];
547
538
 
548
539
  feedTypes.forEach((feedType) => {
549
- const feedConfig = feedsConfig[feedType] || {};
550
-
551
- if (!feedsConfig) {
552
- return;
553
- }
554
-
555
- const {type, path: feedConfigPath, title: feedConfigTitle} = feedConfig;
540
+ const {
541
+ type,
542
+ path: feedConfigPath,
543
+ title: feedConfigTitle,
544
+ } = feedsConfig[feedType];
556
545
 
557
546
  headTags.push({
558
547
  tagName: 'link',
@@ -576,10 +565,4 @@ export default async function pluginContentBlog(
576
565
  };
577
566
  }
578
567
 
579
- export function validateOptions({
580
- validate,
581
- options,
582
- }: OptionValidationContext<PluginOptions>): ValidationResult<PluginOptions> {
583
- const validatedOptions = validate(PluginOptionSchema, options);
584
- return validatedOptions;
585
- }
568
+ export {validateOptions} from './options';
@@ -34,5 +34,5 @@ export default function markdownLoader(
34
34
  finalContent = truncate(finalContent, markdownLoaderOptions.truncateMarker);
35
35
  }
36
36
 
37
- return callback && callback(null, finalContent);
37
+ return callback?.(null, finalContent);
38
38
  }
@@ -13,7 +13,8 @@ import {
13
13
  URISchema,
14
14
  } from '@docusaurus/utils-validation';
15
15
  import {GlobExcludeDefault} from '@docusaurus/utils';
16
- import type {PluginOptions} from '@docusaurus/plugin-content-blog';
16
+ import type {PluginOptions, Options} from '@docusaurus/plugin-content-blog';
17
+ import type {OptionValidationContext} from '@docusaurus/types';
17
18
 
18
19
  export const DEFAULT_OPTIONS: PluginOptions = {
19
20
  feedOptions: {type: ['rss', 'atom'], copyright: ''},
@@ -46,7 +47,7 @@ export const DEFAULT_OPTIONS: PluginOptions = {
46
47
  sortPosts: 'descending',
47
48
  };
48
49
 
49
- export const PluginOptionSchema = Joi.object<PluginOptions>({
50
+ const PluginOptionSchema = Joi.object<PluginOptions>({
50
51
  path: Joi.string().default(DEFAULT_OPTIONS.path),
51
52
  archiveBasePath: Joi.string()
52
53
  .default(DEFAULT_OPTIONS.archiveBasePath)
@@ -125,4 +126,15 @@ export const PluginOptionSchema = Joi.object<PluginOptions>({
125
126
  sortPosts: Joi.string()
126
127
  .valid('descending', 'ascending')
127
128
  .default(DEFAULT_OPTIONS.sortPosts),
128
- });
129
+ }).default(DEFAULT_OPTIONS);
130
+
131
+ export function validateOptions({
132
+ validate,
133
+ options,
134
+ }: OptionValidationContext<Options, PluginOptions>): PluginOptions {
135
+ const validatedOptions = validate(
136
+ PluginOptionSchema,
137
+ options,
138
+ ) as PluginOptions;
139
+ return validatedOptions;
140
+ }