@docusaurus/plugin-content-blog 2.0.0-beta.ff31de0ff → 2.0.0-rc.1

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 (58) hide show
  1. package/lib/authors.d.ts +22 -0
  2. package/lib/authors.js +122 -0
  3. package/lib/blogUtils.d.ts +27 -7
  4. package/lib/blogUtils.js +214 -141
  5. package/lib/feed.d.ts +15 -0
  6. package/lib/feed.js +102 -0
  7. package/lib/frontMatter.d.ts +10 -0
  8. package/lib/frontMatter.js +62 -0
  9. package/lib/index.d.ts +4 -4
  10. package/lib/index.js +179 -205
  11. package/lib/markdownLoader.d.ts +3 -6
  12. package/lib/markdownLoader.js +6 -7
  13. package/lib/options.d.ts +10 -0
  14. package/lib/{pluginOptionSchema.js → options.js} +44 -14
  15. package/lib/remark/footnoteIDFixer.d.ts +14 -0
  16. package/lib/remark/footnoteIDFixer.js +29 -0
  17. package/lib/translations.d.ts +10 -0
  18. package/lib/translations.js +53 -0
  19. package/lib/types.d.ts +4 -109
  20. package/package.json +23 -18
  21. package/src/authors.ts +168 -0
  22. package/src/blogUtils.ts +316 -196
  23. package/src/feed.ts +171 -0
  24. package/src/frontMatter.ts +81 -0
  25. package/src/index.ts +246 -268
  26. package/src/markdownLoader.ts +11 -16
  27. package/src/{pluginOptionSchema.ts → options.ts} +57 -16
  28. package/src/plugin-content-blog.d.ts +587 -0
  29. package/src/remark/footnoteIDFixer.ts +29 -0
  30. package/src/translations.ts +69 -0
  31. package/src/types.ts +2 -128
  32. package/index.d.ts +0 -138
  33. package/lib/.tsbuildinfo +0 -4415
  34. package/lib/blogFrontMatter.d.ts +0 -28
  35. package/lib/blogFrontMatter.js +0 -50
  36. package/lib/pluginOptionSchema.d.ts +0 -33
  37. package/src/__tests__/__fixtures__/website/blog/2018-12-14-Happy-First-Birthday-Slash.md +0 -5
  38. package/src/__tests__/__fixtures__/website/blog/complex-slug.md +0 -7
  39. package/src/__tests__/__fixtures__/website/blog/date-matter.md +0 -5
  40. package/src/__tests__/__fixtures__/website/blog/draft.md +0 -6
  41. package/src/__tests__/__fixtures__/website/blog/heading-as-title.md +0 -5
  42. package/src/__tests__/__fixtures__/website/blog/simple-slug.md +0 -7
  43. package/src/__tests__/__fixtures__/website/blog-with-ref/2018-12-14-Happy-First-Birthday-Slash.md +0 -5
  44. package/src/__tests__/__fixtures__/website/blog-with-ref/post-with-broken-links.md +0 -11
  45. package/src/__tests__/__fixtures__/website/blog-with-ref/post.md +0 -5
  46. package/src/__tests__/__fixtures__/website/i18n/en/docusaurus-plugin-content-blog/2018-12-14-Happy-First-Birthday-Slash.md +0 -5
  47. package/src/__tests__/__fixtures__/website-blog-without-date/blog/no date.md +0 -1
  48. package/src/__tests__/__snapshots__/generateBlogFeed.test.ts.snap +0 -101
  49. package/src/__tests__/__snapshots__/linkify.test.ts.snap +0 -24
  50. package/src/__tests__/__snapshots__/pluginOptionSchema.test.ts.snap +0 -5
  51. package/src/__tests__/blogFrontMatter.test.ts +0 -265
  52. package/src/__tests__/generateBlogFeed.test.ts +0 -100
  53. package/src/__tests__/index.test.ts +0 -336
  54. package/src/__tests__/linkify.test.ts +0 -93
  55. package/src/__tests__/pluginOptionSchema.test.ts +0 -150
  56. package/src/blogFrontMatter.ts +0 -87
  57. package/tsconfig.json +0 -9
  58. package/types.d.ts +0 -13
@@ -1,100 +0,0 @@
1
- /**
2
- * Copyright (c) Facebook, Inc. and its affiliates.
3
- *
4
- * This source code is licensed under the MIT license found in the
5
- * LICENSE file in the root directory of this source tree.
6
- */
7
-
8
- import path from 'path';
9
- import {generateBlogFeed} from '../blogUtils';
10
- import {LoadContext, I18n} from '@docusaurus/types';
11
- import {PluginOptions, BlogContentPaths} from '../types';
12
-
13
- const DefaultI18N: I18n = {
14
- currentLocale: 'en',
15
- locales: ['en'],
16
- defaultLocale: 'en',
17
- localeConfigs: {},
18
- };
19
-
20
- function getBlogContentPaths(siteDir: string): BlogContentPaths {
21
- return {
22
- contentPath: path.resolve(siteDir, 'blog'),
23
- contentPathLocalized: path.resolve(
24
- siteDir,
25
- 'i18n',
26
- 'en',
27
- 'docusaurus-plugin-content-blog',
28
- ),
29
- };
30
- }
31
-
32
- describe('blogFeed', () => {
33
- (['atom', 'rss'] as const).forEach((feedType) => {
34
- describe(`${feedType}`, () => {
35
- test('can show feed without posts', async () => {
36
- const siteDir = __dirname;
37
- const siteConfig = {
38
- title: 'Hello',
39
- baseUrl: '/',
40
- url: 'https://docusaurus.io',
41
- favicon: 'image/favicon.ico',
42
- };
43
-
44
- const feed = await generateBlogFeed(
45
- getBlogContentPaths(siteDir),
46
- {
47
- siteDir,
48
- siteConfig,
49
- i18n: DefaultI18N,
50
- } as LoadContext,
51
- {
52
- path: 'invalid-blog-path',
53
- routeBasePath: 'blog',
54
- include: ['*.md', '*.mdx'],
55
- feedOptions: {
56
- type: [feedType],
57
- copyright: 'Copyright',
58
- },
59
- } as PluginOptions,
60
- );
61
- const feedContent =
62
- feed && (feedType === 'rss' ? feed.rss2() : feed.atom1());
63
- expect(feedContent).toMatchSnapshot();
64
- });
65
-
66
- test('shows feed item for each post', async () => {
67
- const siteDir = path.join(__dirname, '__fixtures__', 'website');
68
- const generatedFilesDir = path.resolve(siteDir, '.docusaurus');
69
- const siteConfig = {
70
- title: 'Hello',
71
- baseUrl: '/',
72
- url: 'https://docusaurus.io',
73
- favicon: 'image/favicon.ico',
74
- };
75
-
76
- const feed = await generateBlogFeed(
77
- getBlogContentPaths(siteDir),
78
- {
79
- siteDir,
80
- siteConfig,
81
- generatedFilesDir,
82
- i18n: DefaultI18N,
83
- } as LoadContext,
84
- {
85
- path: 'blog',
86
- routeBasePath: 'blog',
87
- include: ['*r*.md', '*.mdx'], // skip no-date.md - it won't play nice with snapshots
88
- feedOptions: {
89
- type: [feedType],
90
- copyright: 'Copyright',
91
- },
92
- } as PluginOptions,
93
- );
94
- const feedContent =
95
- feed && (feedType === 'rss' ? feed.rss2() : feed.atom1());
96
- expect(feedContent).toMatchSnapshot();
97
- });
98
- });
99
- });
100
- });
@@ -1,336 +0,0 @@
1
- /**
2
- * Copyright (c) Facebook, Inc. and its affiliates.
3
- *
4
- * This source code is licensed under the MIT license found in the
5
- * LICENSE file in the root directory of this source tree.
6
- */
7
-
8
- /* eslint-disable @typescript-eslint/no-non-null-assertion */
9
-
10
- import fs from 'fs-extra';
11
- import path from 'path';
12
- import pluginContentBlog from '../index';
13
- import {DocusaurusConfig, LoadContext, I18n} from '@docusaurus/types';
14
- import {PluginOptionSchema} from '../pluginOptionSchema';
15
- import {PluginOptions, EditUrlFunction, BlogPost} from '../types';
16
- import {Joi} from '@docusaurus/utils-validation';
17
-
18
- function findByTitle(
19
- blogPosts: BlogPost[],
20
- title: string,
21
- ): BlogPost | undefined {
22
- return blogPosts.find((v) => v.metadata.title === title);
23
- }
24
- function getByTitle(blogPosts: BlogPost[], title: string): BlogPost {
25
- const post = findByTitle(blogPosts, title);
26
- if (!post) {
27
- throw new Error(`can't find blog post with title ${title}.
28
- Available blog post titles are:\n- ${blogPosts
29
- .map((p) => p.metadata.title)
30
- .join('\n- ')}`);
31
- }
32
- return post;
33
- }
34
-
35
- function getI18n(locale: string): I18n {
36
- return {
37
- currentLocale: locale,
38
- locales: [locale],
39
- defaultLocale: locale,
40
- localeConfigs: {},
41
- };
42
- }
43
-
44
- const DefaultI18N: I18n = getI18n('en');
45
-
46
- function validateAndNormalize(
47
- schema: Joi.ObjectSchema,
48
- options: Partial<PluginOptions>,
49
- ) {
50
- const {value, error} = schema.validate(options);
51
- if (error) {
52
- throw error;
53
- } else {
54
- return value;
55
- }
56
- }
57
-
58
- describe('loadBlog', () => {
59
- const PluginPath = 'blog';
60
-
61
- const BaseEditUrl = 'https://baseEditUrl.com/edit';
62
-
63
- const getBlogPosts = async (
64
- siteDir: string,
65
- pluginOptions: Partial<PluginOptions> = {},
66
- i18n: I18n = DefaultI18N,
67
- ) => {
68
- const generatedFilesDir: string = path.resolve(siteDir, '.docusaurus');
69
- const siteConfig = {
70
- title: 'Hello',
71
- baseUrl: '/',
72
- url: 'https://docusaurus.io',
73
- } as DocusaurusConfig;
74
- const plugin = pluginContentBlog(
75
- {
76
- siteDir,
77
- siteConfig,
78
- generatedFilesDir,
79
- i18n,
80
- } as LoadContext,
81
- validateAndNormalize(PluginOptionSchema, {
82
- path: PluginPath,
83
- editUrl: BaseEditUrl,
84
- ...pluginOptions,
85
- }),
86
- );
87
- const {blogPosts} = (await plugin.loadContent!())!;
88
-
89
- return blogPosts;
90
- };
91
-
92
- test('simple website', async () => {
93
- const siteDir = path.join(__dirname, '__fixtures__', 'website');
94
- const blogPosts = await getBlogPosts(siteDir);
95
-
96
- expect({
97
- ...getByTitle(blogPosts, 'date-matter').metadata,
98
- ...{prevItem: undefined},
99
- }).toEqual({
100
- editUrl: `${BaseEditUrl}/blog/date-matter.md`,
101
- permalink: '/blog/date-matter',
102
- readingTime: 0.02,
103
- source: path.posix.join('@site', PluginPath, 'date-matter.md'),
104
- title: 'date-matter',
105
- description: `date inside front matter`,
106
- date: new Date('2019-01-01'),
107
- formattedDate: 'January 1, 2019',
108
- prevItem: undefined,
109
- tags: [],
110
- nextItem: {
111
- permalink: '/blog/2018/12/14/Happy-First-Birthday-Slash',
112
- title: 'Happy 1st Birthday Slash! (translated)',
113
- },
114
- truncated: false,
115
- });
116
-
117
- expect(
118
- getByTitle(blogPosts, 'Happy 1st Birthday Slash! (translated)').metadata,
119
- ).toEqual({
120
- editUrl: `${BaseEditUrl}/blog/2018-12-14-Happy-First-Birthday-Slash.md`,
121
- permalink: '/blog/2018/12/14/Happy-First-Birthday-Slash',
122
- readingTime: 0.015,
123
- source: path.posix.join(
124
- '@site',
125
- // pluginPath,
126
- path.posix.join('i18n', 'en', 'docusaurus-plugin-content-blog'),
127
- '2018-12-14-Happy-First-Birthday-Slash.md',
128
- ),
129
- title: 'Happy 1st Birthday Slash! (translated)',
130
- description: `Happy birthday! (translated)`,
131
- date: new Date('2018-12-14'),
132
- formattedDate: 'December 14, 2018',
133
- tags: [],
134
- prevItem: {
135
- permalink: '/blog/date-matter',
136
- title: 'date-matter',
137
- },
138
- truncated: false,
139
- });
140
-
141
- expect({
142
- ...getByTitle(blogPosts, 'Complex Slug').metadata,
143
- ...{prevItem: undefined},
144
- }).toEqual({
145
- editUrl: `${BaseEditUrl}/blog/complex-slug.md`,
146
- permalink: '/blog/hey/my super path/héllô',
147
- readingTime: 0.015,
148
- source: path.posix.join('@site', PluginPath, 'complex-slug.md'),
149
- title: 'Complex Slug',
150
- description: `complex url slug`,
151
- prevItem: undefined,
152
- nextItem: {
153
- permalink: '/blog/simple/slug',
154
- title: 'Simple Slug',
155
- },
156
- date: new Date('2020-08-16'),
157
- formattedDate: 'August 16, 2020',
158
- tags: [],
159
- truncated: false,
160
- });
161
-
162
- expect({
163
- ...getByTitle(blogPosts, 'Simple Slug').metadata,
164
- ...{prevItem: undefined},
165
- }).toEqual({
166
- editUrl: `${BaseEditUrl}/blog/simple-slug.md`,
167
- permalink: '/blog/simple/slug',
168
- readingTime: 0.015,
169
- source: path.posix.join('@site', PluginPath, 'simple-slug.md'),
170
- title: 'Simple Slug',
171
- description: `simple url slug`,
172
- prevItem: undefined,
173
- nextItem: {
174
- permalink: '/blog/draft',
175
- title: 'draft',
176
- },
177
- date: new Date('2020-08-15'),
178
- formattedDate: 'August 15, 2020',
179
- tags: [],
180
- truncated: false,
181
- });
182
-
183
- expect({
184
- ...getByTitle(blogPosts, 'some heading').metadata,
185
- prevItem: undefined,
186
- }).toEqual({
187
- editUrl: `${BaseEditUrl}/blog/heading-as-title.md`,
188
- permalink: '/blog/heading-as-title',
189
- readingTime: 0,
190
- source: path.posix.join('@site', PluginPath, 'heading-as-title.md'),
191
- title: 'some heading',
192
- description: '',
193
- date: new Date('2019-01-02'),
194
- formattedDate: 'January 2, 2019',
195
- prevItem: undefined,
196
- tags: [],
197
- nextItem: {
198
- permalink: '/blog/date-matter',
199
- title: 'date-matter',
200
- },
201
- truncated: false,
202
- });
203
- });
204
-
205
- test('simple website blog dates localized', async () => {
206
- const siteDir = path.join(__dirname, '__fixtures__', 'website');
207
- const blogPostsFrench = await getBlogPosts(siteDir, {}, getI18n('fr'));
208
- expect(blogPostsFrench).toHaveLength(6);
209
- expect(blogPostsFrench[0].metadata.formattedDate).toMatchInlineSnapshot(
210
- `"16 août 2020"`,
211
- );
212
- expect(blogPostsFrench[1].metadata.formattedDate).toMatchInlineSnapshot(
213
- `"15 août 2020"`,
214
- );
215
- expect(blogPostsFrench[2].metadata.formattedDate).toMatchInlineSnapshot(
216
- `"27 février 2020"`,
217
- );
218
- expect(blogPostsFrench[3].metadata.formattedDate).toMatchInlineSnapshot(
219
- `"2 janvier 2019"`,
220
- );
221
- expect(blogPostsFrench[4].metadata.formattedDate).toMatchInlineSnapshot(
222
- `"1 janvier 2019"`,
223
- );
224
- expect(blogPostsFrench[5].metadata.formattedDate).toMatchInlineSnapshot(
225
- `"14 décembre 2018"`,
226
- );
227
- });
228
-
229
- test('edit url with editLocalizedBlogs true', async () => {
230
- const siteDir = path.join(__dirname, '__fixtures__', 'website');
231
- const blogPosts = await getBlogPosts(siteDir, {editLocalizedFiles: true});
232
-
233
- const localizedBlogPost = blogPosts.find(
234
- (v) => v.metadata.title === 'Happy 1st Birthday Slash! (translated)',
235
- )!;
236
-
237
- expect(localizedBlogPost.metadata.editUrl).toEqual(
238
- `${BaseEditUrl}/i18n/en/docusaurus-plugin-content-blog/2018-12-14-Happy-First-Birthday-Slash.md`,
239
- );
240
- });
241
-
242
- test('edit url with editUrl function', async () => {
243
- const siteDir = path.join(__dirname, '__fixtures__', 'website');
244
-
245
- const hardcodedEditUrl = 'hardcoded-edit-url';
246
- const editUrlFunction: EditUrlFunction = jest.fn(() => hardcodedEditUrl);
247
-
248
- const blogPosts = await getBlogPosts(siteDir, {editUrl: editUrlFunction});
249
-
250
- blogPosts.forEach((blogPost) => {
251
- expect(blogPost.metadata.editUrl).toEqual(hardcodedEditUrl);
252
- });
253
-
254
- expect(editUrlFunction).toHaveBeenCalledTimes(6);
255
- expect(editUrlFunction).toHaveBeenCalledWith({
256
- blogDirPath: 'blog',
257
- blogPath: 'date-matter.md',
258
- permalink: '/blog/date-matter',
259
- locale: 'en',
260
- });
261
- expect(editUrlFunction).toHaveBeenCalledWith({
262
- blogDirPath: 'blog',
263
- blogPath: 'draft.md',
264
- permalink: '/blog/draft',
265
- locale: 'en',
266
- });
267
- expect(editUrlFunction).toHaveBeenCalledWith({
268
- blogDirPath: 'blog',
269
- blogPath: 'complex-slug.md',
270
- permalink: '/blog/hey/my super path/héllô',
271
- locale: 'en',
272
- });
273
- expect(editUrlFunction).toHaveBeenCalledWith({
274
- blogDirPath: 'blog',
275
- blogPath: 'simple-slug.md',
276
- permalink: '/blog/simple/slug',
277
- locale: 'en',
278
- });
279
- expect(editUrlFunction).toHaveBeenCalledWith({
280
- blogDirPath: 'i18n/en/docusaurus-plugin-content-blog',
281
- blogPath: '2018-12-14-Happy-First-Birthday-Slash.md',
282
- permalink: '/blog/2018/12/14/Happy-First-Birthday-Slash',
283
- locale: 'en',
284
- });
285
- expect(editUrlFunction).toHaveBeenCalledWith({
286
- blogDirPath: 'blog',
287
- blogPath: 'heading-as-title.md',
288
- locale: 'en',
289
- permalink: '/blog/heading-as-title',
290
- });
291
- });
292
-
293
- test('draft blog post not exists in production build', async () => {
294
- process.env.NODE_ENV = 'production';
295
- const siteDir = path.join(__dirname, '__fixtures__', 'website');
296
- const blogPosts = await getBlogPosts(siteDir);
297
-
298
- expect(blogPosts.find((v) => v.metadata.title === 'draft')).toBeUndefined();
299
- });
300
-
301
- test('create blog post without date', async () => {
302
- const siteDir = path.join(
303
- __dirname,
304
- '__fixtures__',
305
- 'website-blog-without-date',
306
- );
307
- const blogPosts = await getBlogPosts(siteDir);
308
- const noDateSource = path.posix.join('@site', PluginPath, 'no date.md');
309
- const noDateSourceBirthTime = (
310
- await fs.stat(noDateSource.replace('@site', siteDir))
311
- ).birthtime;
312
- const formattedDate = Intl.DateTimeFormat('en', {
313
- day: 'numeric',
314
- month: 'long',
315
- year: 'numeric',
316
- }).format(noDateSourceBirthTime);
317
-
318
- expect({
319
- ...getByTitle(blogPosts, 'no date').metadata,
320
- ...{prevItem: undefined},
321
- }).toEqual({
322
- editUrl: `${BaseEditUrl}/blog/no date.md`,
323
- permalink: '/blog/no date',
324
- readingTime: 0.01,
325
- source: noDateSource,
326
- title: 'no date',
327
- description: `no date`,
328
- date: noDateSourceBirthTime,
329
- formattedDate,
330
- tags: [],
331
- prevItem: undefined,
332
- nextItem: undefined,
333
- truncated: false,
334
- });
335
- });
336
- });
@@ -1,93 +0,0 @@
1
- /**
2
- * Copyright (c) Facebook, Inc. and its affiliates.
3
- *
4
- * This source code is licensed under the MIT license found in the
5
- * LICENSE file in the root directory of this source tree.
6
- */
7
-
8
- import fs from 'fs-extra';
9
- import path from 'path';
10
- import {linkify, LinkifyParams, getSourceToPermalink} from '../blogUtils';
11
- import {BlogBrokenMarkdownLink, BlogContentPaths, BlogPost} from '../types';
12
-
13
- const siteDir = path.join(__dirname, '__fixtures__', 'website');
14
- const contentPaths: BlogContentPaths = {
15
- contentPath: path.join(siteDir, 'blog-with-ref'),
16
- contentPathLocalized: path.join(siteDir, 'blog-with-ref-localized'),
17
- };
18
- const pluginDir = 'blog-with-ref';
19
- const blogPosts: BlogPost[] = [
20
- {
21
- id: 'Happy 1st Birthday Slash!',
22
- metadata: {
23
- permalink: '/blog/2018/12/14/Happy-First-Birthday-Slash',
24
- source: path.posix.join(
25
- '@site',
26
- pluginDir,
27
- '2018-12-14-Happy-First-Birthday-Slash.md',
28
- ),
29
- title: 'Happy 1st Birthday Slash!',
30
- description: `pattern name`,
31
- date: new Date('2018-12-14'),
32
- tags: [],
33
- prevItem: {
34
- permalink: '/blog/2019/01/01/date-matter',
35
- title: 'date-matter',
36
- },
37
- truncated: false,
38
- },
39
- },
40
- ];
41
-
42
- const transform = (filePath: string, options?: Partial<LinkifyParams>) => {
43
- const fileContent = fs.readFileSync(filePath, 'utf-8');
44
- const transformedContent = linkify({
45
- filePath,
46
- fileString: fileContent,
47
- siteDir,
48
- contentPaths,
49
- sourceToPermalink: getSourceToPermalink(blogPosts),
50
- onBrokenMarkdownLink: (brokenMarkdownLink) => {
51
- throw new Error(
52
- `Broken markdown link found: ${JSON.stringify(brokenMarkdownLink)}`,
53
- );
54
- },
55
- ...options,
56
- });
57
- return [fileContent, transformedContent];
58
- };
59
-
60
- test('transform to correct link', () => {
61
- const post = path.join(contentPaths.contentPath, 'post.md');
62
- const [content, transformedContent] = transform(post);
63
- expect(transformedContent).toMatchSnapshot();
64
- expect(transformedContent).toContain(
65
- '](/blog/2018/12/14/Happy-First-Birthday-Slash',
66
- );
67
- expect(transformedContent).not.toContain(
68
- '](2018-12-14-Happy-First-Birthday-Slash.md)',
69
- );
70
- expect(content).not.toEqual(transformedContent);
71
- });
72
-
73
- test('report broken markdown links', () => {
74
- const filePath = 'post-with-broken-links.md';
75
- const folderPath = contentPaths.contentPath;
76
- const postWithBrokenLinks = path.join(folderPath, filePath);
77
- const onBrokenMarkdownLink = jest.fn();
78
- const [, transformedContent] = transform(postWithBrokenLinks, {
79
- onBrokenMarkdownLink,
80
- });
81
- expect(transformedContent).toMatchSnapshot();
82
- expect(onBrokenMarkdownLink).toHaveBeenCalledTimes(2);
83
- expect(onBrokenMarkdownLink).toHaveBeenNthCalledWith(1, {
84
- filePath: path.resolve(folderPath, filePath),
85
- contentPaths,
86
- link: 'postNotExist1.md',
87
- } as BlogBrokenMarkdownLink);
88
- expect(onBrokenMarkdownLink).toHaveBeenNthCalledWith(2, {
89
- filePath: path.resolve(folderPath, filePath),
90
- contentPaths,
91
- link: './postNotExist2.mdx',
92
- } as BlogBrokenMarkdownLink);
93
- });
@@ -1,150 +0,0 @@
1
- /**
2
- * Copyright (c) Facebook, Inc. and its affiliates.
3
- *
4
- * This source code is licensed under the MIT license found in the
5
- * LICENSE file in the root directory of this source tree.
6
- */
7
-
8
- import {PluginOptionSchema, DEFAULT_OPTIONS} from '../pluginOptionSchema';
9
-
10
- // the type of remark/rehype plugins can be either function, object or array
11
- const markdownPluginsFunctionStub = () => {};
12
- const markdownPluginsObjectStub = {};
13
-
14
- test('should normalize options', () => {
15
- const {value, error} = PluginOptionSchema.validate({});
16
- expect(value).toEqual(DEFAULT_OPTIONS);
17
- expect(error).toBe(undefined);
18
- });
19
-
20
- test('should accept correctly defined user options', () => {
21
- const userOptions = {
22
- ...DEFAULT_OPTIONS,
23
- feedOptions: {type: 'rss', title: 'myTitle'},
24
- path: 'not_blog',
25
- routeBasePath: 'myBlog',
26
- postsPerPage: 5,
27
- include: ['api/*', 'docs/*'],
28
- };
29
- const {value, error} = PluginOptionSchema.validate(userOptions);
30
- expect(value).toEqual({
31
- ...userOptions,
32
- feedOptions: {type: ['rss'], title: 'myTitle'},
33
- });
34
- expect(error).toBe(undefined);
35
- });
36
-
37
- test('should accept valid user options', async () => {
38
- const userOptions = {
39
- ...DEFAULT_OPTIONS,
40
- routeBasePath: 'myBlog',
41
- beforeDefaultRemarkPlugins: [],
42
- beforeDefaultRehypePlugins: [markdownPluginsFunctionStub],
43
- remarkPlugins: [[markdownPluginsFunctionStub, {option1: '42'}]],
44
- rehypePlugins: [
45
- markdownPluginsObjectStub,
46
- [markdownPluginsFunctionStub, {option1: '42'}],
47
- ],
48
- };
49
- const {value, error} = await PluginOptionSchema.validate(userOptions);
50
- expect(value).toEqual(userOptions);
51
- expect(error).toBe(undefined);
52
- });
53
-
54
- test('should throw Error in case of invalid options', () => {
55
- const {error} = PluginOptionSchema.validate({
56
- path: 'not_blog',
57
- postsPerPage: -1,
58
- include: ['api/*', 'docs/*'],
59
- routeBasePath: 'not_blog',
60
- });
61
-
62
- expect(error).toMatchSnapshot();
63
- });
64
-
65
- test('should throw Error in case of invalid feedtype', () => {
66
- const {error} = PluginOptionSchema.validate({
67
- feedOptions: {
68
- type: 'none',
69
- },
70
- });
71
-
72
- expect(error).toMatchSnapshot();
73
- });
74
-
75
- test('should convert all feed type to array with other feed type', () => {
76
- const {value} = PluginOptionSchema.validate({
77
- feedOptions: {type: 'all'},
78
- });
79
- expect(value).toEqual({
80
- ...DEFAULT_OPTIONS,
81
- feedOptions: {type: ['rss', 'atom']},
82
- });
83
- });
84
-
85
- test('should accept null type and return same', () => {
86
- const {value, error} = PluginOptionSchema.validate({
87
- feedOptions: {type: null},
88
- });
89
- expect(value).toEqual({
90
- ...DEFAULT_OPTIONS,
91
- feedOptions: {type: null},
92
- });
93
- expect(error).toBe(undefined);
94
- });
95
-
96
- test('should contain array with rss + atom for missing feed type', () => {
97
- const {value} = PluginOptionSchema.validate({
98
- feedOptions: {},
99
- });
100
- expect(value).toEqual(DEFAULT_OPTIONS);
101
- });
102
-
103
- test('should have array with rss + atom, title for missing feed type', () => {
104
- const {value} = PluginOptionSchema.validate({
105
- feedOptions: {title: 'title'},
106
- });
107
- expect(value).toEqual({
108
- ...DEFAULT_OPTIONS,
109
- feedOptions: {type: ['rss', 'atom'], title: 'title'},
110
- });
111
- });
112
-
113
- describe('blog sidebar', () => {
114
- test('should accept 0 sidebar count', () => {
115
- const userOptions = {blogSidebarCount: 0};
116
- const {value, error} = PluginOptionSchema.validate(userOptions);
117
- expect(value).toEqual({...DEFAULT_OPTIONS, ...userOptions});
118
- expect(error).toBe(undefined);
119
- });
120
-
121
- test('should accept "ALL" sidebar count', () => {
122
- const userOptions = {blogSidebarCount: 'ALL'};
123
- const {value, error} = PluginOptionSchema.validate(userOptions);
124
- expect(value).toEqual({...DEFAULT_OPTIONS, ...userOptions});
125
- expect(error).toBe(undefined);
126
- });
127
-
128
- test('should reject "abcdef" sidebar count', () => {
129
- const userOptions = {blogSidebarCount: 'abcdef'};
130
- const {error} = PluginOptionSchema.validate(userOptions);
131
- expect(error).toMatchInlineSnapshot(
132
- `[ValidationError: "blogSidebarCount" must be one of [ALL, number]]`,
133
- );
134
- });
135
-
136
- test('should accept "all posts" sidebar title', () => {
137
- const userOptions = {blogSidebarTitle: 'all posts'};
138
- const {value, error} = PluginOptionSchema.validate(userOptions);
139
- expect(value).toEqual({...DEFAULT_OPTIONS, ...userOptions});
140
- expect(error).toBe(undefined);
141
- });
142
-
143
- test('should reject 42 sidebar title', () => {
144
- const userOptions = {blogSidebarTitle: 42};
145
- const {error} = PluginOptionSchema.validate(userOptions);
146
- expect(error).toMatchInlineSnapshot(
147
- `[ValidationError: "blogSidebarTitle" must be a string]`,
148
- );
149
- });
150
- });