@docusaurus/plugin-content-blog 2.0.0-beta.1decd6f80 → 2.0.0-beta.20

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 +118 -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 +100 -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 +182 -192
  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} +43 -13
  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 +22 -17
  21. package/src/authors.ts +164 -0
  22. package/src/blogUtils.ts +316 -196
  23. package/{types.d.ts → src/deps.d.ts} +1 -1
  24. package/src/feed.ts +169 -0
  25. package/src/frontMatter.ts +81 -0
  26. package/src/index.ts +245 -249
  27. package/src/markdownLoader.ts +11 -16
  28. package/src/{pluginOptionSchema.ts → options.ts} +54 -14
  29. package/src/plugin-content-blog.d.ts +580 -0
  30. package/src/remark/footnoteIDFixer.ts +29 -0
  31. package/src/translations.ts +69 -0
  32. package/src/types.ts +2 -128
  33. package/index.d.ts +0 -138
  34. package/lib/.tsbuildinfo +0 -4415
  35. package/lib/blogFrontMatter.d.ts +0 -28
  36. package/lib/blogFrontMatter.js +0 -50
  37. package/lib/pluginOptionSchema.d.ts +0 -33
  38. package/src/__tests__/__fixtures__/website/blog/2018-12-14-Happy-First-Birthday-Slash.md +0 -5
  39. package/src/__tests__/__fixtures__/website/blog/complex-slug.md +0 -7
  40. package/src/__tests__/__fixtures__/website/blog/date-matter.md +0 -5
  41. package/src/__tests__/__fixtures__/website/blog/draft.md +0 -6
  42. package/src/__tests__/__fixtures__/website/blog/heading-as-title.md +0 -5
  43. package/src/__tests__/__fixtures__/website/blog/simple-slug.md +0 -7
  44. package/src/__tests__/__fixtures__/website/blog-with-ref/2018-12-14-Happy-First-Birthday-Slash.md +0 -5
  45. package/src/__tests__/__fixtures__/website/blog-with-ref/post-with-broken-links.md +0 -11
  46. package/src/__tests__/__fixtures__/website/blog-with-ref/post.md +0 -5
  47. package/src/__tests__/__fixtures__/website/i18n/en/docusaurus-plugin-content-blog/2018-12-14-Happy-First-Birthday-Slash.md +0 -5
  48. package/src/__tests__/__fixtures__/website-blog-without-date/blog/no date.md +0 -1
  49. package/src/__tests__/__snapshots__/generateBlogFeed.test.ts.snap +0 -101
  50. package/src/__tests__/__snapshots__/linkify.test.ts.snap +0 -24
  51. package/src/__tests__/__snapshots__/pluginOptionSchema.test.ts.snap +0 -5
  52. package/src/__tests__/blogFrontMatter.test.ts +0 -265
  53. package/src/__tests__/generateBlogFeed.test.ts +0 -100
  54. package/src/__tests__/index.test.ts +0 -336
  55. package/src/__tests__/linkify.test.ts +0 -93
  56. package/src/__tests__/pluginOptionSchema.test.ts +0 -150
  57. package/src/blogFrontMatter.ts +0 -87
  58. package/tsconfig.json +0 -9
package/src/feed.ts ADDED
@@ -0,0 +1,169 @@
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 {Feed, type Author as FeedAuthor, type Item as FeedItem} from 'feed';
9
+ import {normalizeUrl, readOutputHTMLFile} from '@docusaurus/utils';
10
+ import {load as cheerioLoad} from 'cheerio';
11
+ import type {DocusaurusConfig} from '@docusaurus/types';
12
+ import path from 'path';
13
+ import fs from 'fs-extra';
14
+ import type {
15
+ FeedType,
16
+ PluginOptions,
17
+ Author,
18
+ BlogPost,
19
+ } from '@docusaurus/plugin-content-blog';
20
+ import {blogPostContainerID} from '@docusaurus/utils-common';
21
+
22
+ async function generateBlogFeed({
23
+ blogPosts,
24
+ options,
25
+ siteConfig,
26
+ outDir,
27
+ locale,
28
+ }: {
29
+ blogPosts: BlogPost[];
30
+ options: PluginOptions;
31
+ siteConfig: DocusaurusConfig;
32
+ outDir: string;
33
+ locale: string;
34
+ }): Promise<Feed | null> {
35
+ if (!blogPosts.length) {
36
+ return null;
37
+ }
38
+
39
+ const {feedOptions, routeBasePath} = options;
40
+ const {url: siteUrl, baseUrl, title, favicon} = siteConfig;
41
+ const blogBaseUrl = normalizeUrl([siteUrl, baseUrl, routeBasePath]);
42
+
43
+ const updated = blogPosts[0]?.metadata.date;
44
+
45
+ const feed = new Feed({
46
+ id: blogBaseUrl,
47
+ title: feedOptions.title ?? `${title} Blog`,
48
+ updated,
49
+ language: feedOptions.language ?? locale,
50
+ link: blogBaseUrl,
51
+ description: feedOptions.description ?? `${siteConfig.title} Blog`,
52
+ favicon: favicon ? normalizeUrl([siteUrl, baseUrl, favicon]) : undefined,
53
+ copyright: feedOptions.copyright,
54
+ });
55
+
56
+ function toFeedAuthor(author: Author): FeedAuthor {
57
+ return {name: author.name, link: author.url, email: author.email};
58
+ }
59
+
60
+ await Promise.all(
61
+ blogPosts.map(async (post) => {
62
+ const {
63
+ id,
64
+ metadata: {
65
+ title: metadataTitle,
66
+ permalink,
67
+ date,
68
+ description,
69
+ authors,
70
+ tags,
71
+ },
72
+ } = post;
73
+
74
+ const content = await readOutputHTMLFile(
75
+ permalink.replace(siteConfig.baseUrl, ''),
76
+ outDir,
77
+ siteConfig.trailingSlash,
78
+ );
79
+ const $ = cheerioLoad(content);
80
+
81
+ const feedItem: FeedItem = {
82
+ title: metadataTitle,
83
+ id,
84
+ link: normalizeUrl([siteUrl, permalink]),
85
+ date,
86
+ description,
87
+ // Atom feed demands the "term", while other feeds use "name"
88
+ category: tags.map((tag) => ({name: tag.label, term: tag.label})),
89
+ content: $(`#${blogPostContainerID}`).html()!,
90
+ };
91
+
92
+ // json1() method takes the first item of authors array
93
+ // it causes an error when authors array is empty
94
+ const feedItemAuthors = authors.map(toFeedAuthor);
95
+ if (feedItemAuthors.length > 0) {
96
+ feedItem.author = feedItemAuthors;
97
+ }
98
+
99
+ return feedItem;
100
+ }),
101
+ ).then((items) => items.forEach(feed.addItem));
102
+
103
+ return feed;
104
+ }
105
+
106
+ async function createBlogFeedFile({
107
+ feed,
108
+ feedType,
109
+ generatePath,
110
+ }: {
111
+ feed: Feed;
112
+ feedType: FeedType;
113
+ generatePath: string;
114
+ }) {
115
+ const [feedContent, feedPath] = (() => {
116
+ switch (feedType) {
117
+ case 'rss':
118
+ return [feed.rss2(), 'rss.xml'];
119
+ case 'json':
120
+ return [feed.json1(), 'feed.json'];
121
+ case 'atom':
122
+ return [feed.atom1(), 'atom.xml'];
123
+ default:
124
+ throw new Error(`Feed type ${feedType} not supported.`);
125
+ }
126
+ })();
127
+ try {
128
+ await fs.outputFile(path.join(generatePath, feedPath), feedContent);
129
+ } catch (err) {
130
+ throw new Error(`Generating ${feedType} feed failed: ${err}.`);
131
+ }
132
+ }
133
+
134
+ export async function createBlogFeedFiles({
135
+ blogPosts,
136
+ options,
137
+ siteConfig,
138
+ outDir,
139
+ locale,
140
+ }: {
141
+ blogPosts: BlogPost[];
142
+ options: PluginOptions;
143
+ siteConfig: DocusaurusConfig;
144
+ outDir: string;
145
+ locale: string;
146
+ }): Promise<void> {
147
+ const feed = await generateBlogFeed({
148
+ blogPosts,
149
+ options,
150
+ siteConfig,
151
+ outDir,
152
+ locale,
153
+ });
154
+
155
+ const feedTypes = options.feedOptions.type;
156
+ if (!feed || !feedTypes) {
157
+ return;
158
+ }
159
+
160
+ await Promise.all(
161
+ feedTypes.map((feedType) =>
162
+ createBlogFeedFile({
163
+ feed,
164
+ feedType,
165
+ generatePath: path.join(outDir, options.routeBasePath),
166
+ }),
167
+ ),
168
+ );
169
+ }
@@ -0,0 +1,81 @@
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 {
9
+ JoiFrontMatter as Joi, // Custom instance for front matter
10
+ URISchema,
11
+ validateFrontMatter,
12
+ FrontMatterTagsSchema,
13
+ FrontMatterTOCHeadingLevels,
14
+ } from '@docusaurus/utils-validation';
15
+ import type {BlogPostFrontMatter} from '@docusaurus/plugin-content-blog';
16
+
17
+ const BlogPostFrontMatterAuthorSchema = Joi.object({
18
+ key: Joi.string(),
19
+ name: Joi.string(),
20
+ title: Joi.string(),
21
+ url: URISchema,
22
+ imageURL: Joi.string(),
23
+ })
24
+ .or('key', 'name', 'imageURL')
25
+ .rename('image_url', 'imageURL', {alias: true});
26
+
27
+ const FrontMatterAuthorErrorMessage =
28
+ '{{#label}} does not look like a valid blog post author. Please use an author key or an author object (with a key and/or name).';
29
+
30
+ const BlogFrontMatterSchema = Joi.object<BlogPostFrontMatter>({
31
+ id: Joi.string(),
32
+ title: Joi.string().allow(''),
33
+ description: Joi.string().allow(''),
34
+ tags: FrontMatterTagsSchema,
35
+ draft: Joi.boolean(),
36
+ date: Joi.date().raw(),
37
+
38
+ // New multi-authors front matter:
39
+ authors: Joi.alternatives()
40
+ .try(
41
+ Joi.string(),
42
+ BlogPostFrontMatterAuthorSchema,
43
+ Joi.array()
44
+ .items(Joi.string(), BlogPostFrontMatterAuthorSchema)
45
+ .messages({
46
+ 'array.sparse': FrontMatterAuthorErrorMessage,
47
+ 'array.includes': FrontMatterAuthorErrorMessage,
48
+ }),
49
+ )
50
+ .messages({
51
+ 'alternatives.match': FrontMatterAuthorErrorMessage,
52
+ }),
53
+ // Legacy author front matter
54
+ author: Joi.string(),
55
+ author_title: Joi.string(),
56
+ author_url: URISchema,
57
+ author_image_url: URISchema,
58
+ // TODO enable deprecation warnings later
59
+ authorURL: URISchema,
60
+ // .warning('deprecate.error', { alternative: '"author_url"'}),
61
+ authorTitle: Joi.string(),
62
+ // .warning('deprecate.error', { alternative: '"author_title"'}),
63
+ authorImageURL: URISchema,
64
+ // .warning('deprecate.error', { alternative: '"author_image_url"'}),
65
+
66
+ slug: Joi.string(),
67
+ image: URISchema,
68
+ keywords: Joi.array().items(Joi.string().required()),
69
+ hide_table_of_contents: Joi.boolean(),
70
+
71
+ ...FrontMatterTOCHeadingLevels,
72
+ }).messages({
73
+ 'deprecate.error':
74
+ '{#label} blog frontMatter field is deprecated. Please use {#alternative} instead.',
75
+ });
76
+
77
+ export function validateBlogPostFrontMatter(frontMatter: {
78
+ [key: string]: unknown;
79
+ }): BlogPostFrontMatter {
80
+ return validateFrontMatter(frontMatter, BlogFrontMatterSchema);
81
+ }