@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/lib/feed.js ADDED
@@ -0,0 +1,100 @@
1
+ "use strict";
2
+ /**
3
+ * Copyright (c) Facebook, Inc. and its affiliates.
4
+ *
5
+ * This source code is licensed under the MIT license found in the
6
+ * LICENSE file in the root directory of this source tree.
7
+ */
8
+ Object.defineProperty(exports, "__esModule", { value: true });
9
+ exports.createBlogFeedFiles = void 0;
10
+ const tslib_1 = require("tslib");
11
+ const feed_1 = require("feed");
12
+ const utils_1 = require("@docusaurus/utils");
13
+ const cheerio_1 = require("cheerio");
14
+ const path_1 = tslib_1.__importDefault(require("path"));
15
+ const fs_extra_1 = tslib_1.__importDefault(require("fs-extra"));
16
+ const utils_common_1 = require("@docusaurus/utils-common");
17
+ async function generateBlogFeed({ blogPosts, options, siteConfig, outDir, locale, }) {
18
+ if (!blogPosts.length) {
19
+ return null;
20
+ }
21
+ const { feedOptions, routeBasePath } = options;
22
+ const { url: siteUrl, baseUrl, title, favicon } = siteConfig;
23
+ const blogBaseUrl = (0, utils_1.normalizeUrl)([siteUrl, baseUrl, routeBasePath]);
24
+ const updated = blogPosts[0]?.metadata.date;
25
+ const feed = new feed_1.Feed({
26
+ id: blogBaseUrl,
27
+ title: feedOptions.title ?? `${title} Blog`,
28
+ updated,
29
+ language: feedOptions.language ?? locale,
30
+ link: blogBaseUrl,
31
+ description: feedOptions.description ?? `${siteConfig.title} Blog`,
32
+ favicon: favicon ? (0, utils_1.normalizeUrl)([siteUrl, baseUrl, favicon]) : undefined,
33
+ copyright: feedOptions.copyright,
34
+ });
35
+ function toFeedAuthor(author) {
36
+ return { name: author.name, link: author.url, email: author.email };
37
+ }
38
+ await Promise.all(blogPosts.map(async (post) => {
39
+ const { id, metadata: { title: metadataTitle, permalink, date, description, authors, tags, }, } = post;
40
+ const content = await (0, utils_1.readOutputHTMLFile)(permalink.replace(siteConfig.baseUrl, ''), outDir, siteConfig.trailingSlash);
41
+ const $ = (0, cheerio_1.load)(content);
42
+ const feedItem = {
43
+ title: metadataTitle,
44
+ id,
45
+ link: (0, utils_1.normalizeUrl)([siteUrl, permalink]),
46
+ date,
47
+ description,
48
+ // Atom feed demands the "term", while other feeds use "name"
49
+ category: tags.map((tag) => ({ name: tag.label, term: tag.label })),
50
+ content: $(`#${utils_common_1.blogPostContainerID}`).html(),
51
+ };
52
+ // json1() method takes the first item of authors array
53
+ // it causes an error when authors array is empty
54
+ const feedItemAuthors = authors.map(toFeedAuthor);
55
+ if (feedItemAuthors.length > 0) {
56
+ feedItem.author = feedItemAuthors;
57
+ }
58
+ return feedItem;
59
+ })).then((items) => items.forEach(feed.addItem));
60
+ return feed;
61
+ }
62
+ async function createBlogFeedFile({ feed, feedType, generatePath, }) {
63
+ const [feedContent, feedPath] = (() => {
64
+ switch (feedType) {
65
+ case 'rss':
66
+ return [feed.rss2(), 'rss.xml'];
67
+ case 'json':
68
+ return [feed.json1(), 'feed.json'];
69
+ case 'atom':
70
+ return [feed.atom1(), 'atom.xml'];
71
+ default:
72
+ throw new Error(`Feed type ${feedType} not supported.`);
73
+ }
74
+ })();
75
+ try {
76
+ await fs_extra_1.default.outputFile(path_1.default.join(generatePath, feedPath), feedContent);
77
+ }
78
+ catch (err) {
79
+ throw new Error(`Generating ${feedType} feed failed: ${err}.`);
80
+ }
81
+ }
82
+ async function createBlogFeedFiles({ blogPosts, options, siteConfig, outDir, locale, }) {
83
+ const feed = await generateBlogFeed({
84
+ blogPosts,
85
+ options,
86
+ siteConfig,
87
+ outDir,
88
+ locale,
89
+ });
90
+ const feedTypes = options.feedOptions.type;
91
+ if (!feed || !feedTypes) {
92
+ return;
93
+ }
94
+ await Promise.all(feedTypes.map((feedType) => createBlogFeedFile({
95
+ feed,
96
+ feedType,
97
+ generatePath: path_1.default.join(outDir, options.routeBasePath),
98
+ })));
99
+ }
100
+ exports.createBlogFeedFiles = createBlogFeedFiles;
@@ -0,0 +1,10 @@
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
+ import type { BlogPostFrontMatter } from '@docusaurus/plugin-content-blog';
8
+ export declare function validateBlogPostFrontMatter(frontMatter: {
9
+ [key: string]: unknown;
10
+ }): BlogPostFrontMatter;
@@ -0,0 +1,62 @@
1
+ "use strict";
2
+ /**
3
+ * Copyright (c) Facebook, Inc. and its affiliates.
4
+ *
5
+ * This source code is licensed under the MIT license found in the
6
+ * LICENSE file in the root directory of this source tree.
7
+ */
8
+ Object.defineProperty(exports, "__esModule", { value: true });
9
+ exports.validateBlogPostFrontMatter = void 0;
10
+ const utils_validation_1 = require("@docusaurus/utils-validation");
11
+ const BlogPostFrontMatterAuthorSchema = utils_validation_1.JoiFrontMatter.object({
12
+ key: utils_validation_1.JoiFrontMatter.string(),
13
+ name: utils_validation_1.JoiFrontMatter.string(),
14
+ title: utils_validation_1.JoiFrontMatter.string(),
15
+ url: utils_validation_1.URISchema,
16
+ imageURL: utils_validation_1.JoiFrontMatter.string(),
17
+ })
18
+ .or('key', 'name', 'imageURL')
19
+ .rename('image_url', 'imageURL', { alias: true });
20
+ const FrontMatterAuthorErrorMessage = '{{#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).';
21
+ const BlogFrontMatterSchema = utils_validation_1.JoiFrontMatter.object({
22
+ id: utils_validation_1.JoiFrontMatter.string(),
23
+ title: utils_validation_1.JoiFrontMatter.string().allow(''),
24
+ description: utils_validation_1.JoiFrontMatter.string().allow(''),
25
+ tags: utils_validation_1.FrontMatterTagsSchema,
26
+ draft: utils_validation_1.JoiFrontMatter.boolean(),
27
+ date: utils_validation_1.JoiFrontMatter.date().raw(),
28
+ // New multi-authors front matter:
29
+ authors: utils_validation_1.JoiFrontMatter.alternatives()
30
+ .try(utils_validation_1.JoiFrontMatter.string(), BlogPostFrontMatterAuthorSchema, utils_validation_1.JoiFrontMatter.array()
31
+ .items(utils_validation_1.JoiFrontMatter.string(), BlogPostFrontMatterAuthorSchema)
32
+ .messages({
33
+ 'array.sparse': FrontMatterAuthorErrorMessage,
34
+ 'array.includes': FrontMatterAuthorErrorMessage,
35
+ }))
36
+ .messages({
37
+ 'alternatives.match': FrontMatterAuthorErrorMessage,
38
+ }),
39
+ // Legacy author front matter
40
+ author: utils_validation_1.JoiFrontMatter.string(),
41
+ author_title: utils_validation_1.JoiFrontMatter.string(),
42
+ author_url: utils_validation_1.URISchema,
43
+ author_image_url: utils_validation_1.URISchema,
44
+ // TODO enable deprecation warnings later
45
+ authorURL: utils_validation_1.URISchema,
46
+ // .warning('deprecate.error', { alternative: '"author_url"'}),
47
+ authorTitle: utils_validation_1.JoiFrontMatter.string(),
48
+ // .warning('deprecate.error', { alternative: '"author_title"'}),
49
+ authorImageURL: utils_validation_1.URISchema,
50
+ // .warning('deprecate.error', { alternative: '"author_image_url"'}),
51
+ slug: utils_validation_1.JoiFrontMatter.string(),
52
+ image: utils_validation_1.URISchema,
53
+ keywords: utils_validation_1.JoiFrontMatter.array().items(utils_validation_1.JoiFrontMatter.string().required()),
54
+ hide_table_of_contents: utils_validation_1.JoiFrontMatter.boolean(),
55
+ ...utils_validation_1.FrontMatterTOCHeadingLevels,
56
+ }).messages({
57
+ 'deprecate.error': '{#label} blog frontMatter field is deprecated. Please use {#alternative} instead.',
58
+ });
59
+ function validateBlogPostFrontMatter(frontMatter) {
60
+ return (0, utils_validation_1.validateFrontMatter)(frontMatter, BlogFrontMatterSchema);
61
+ }
62
+ exports.validateBlogPostFrontMatter = validateBlogPostFrontMatter;
package/lib/index.d.ts CHANGED
@@ -4,7 +4,7 @@
4
4
  * This source code is licensed under the MIT license found in the
5
5
  * LICENSE file in the root directory of this source tree.
6
6
  */
7
- import { PluginOptions, BlogContent } from './types';
8
- import { LoadContext, Plugin, OptionValidationContext, ValidationResult } from '@docusaurus/types';
9
- export default function pluginContentBlog(context: LoadContext, options: PluginOptions): Plugin<BlogContent | null>;
10
- export declare function validateOptions({ validate, options, }: OptionValidationContext<PluginOptions>): ValidationResult<PluginOptions>;
7
+ import type { LoadContext, Plugin } from '@docusaurus/types';
8
+ import type { PluginOptions, BlogContent } from '@docusaurus/plugin-content-blog';
9
+ export default function pluginContentBlog(context: LoadContext, options: PluginOptions): Promise<Plugin<BlogContent>>;
10
+ export { validateOptions } from './options';