@docusaurus/plugin-content-blog 2.0.0-beta.15d451942 → 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.
Files changed (55) 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 +213 -141
  5. package/lib/feed.d.ts +16 -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 +5 -4
  10. package/lib/index.js +170 -182
  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/translations.d.ts +11 -0
  16. package/lib/translations.js +53 -0
  17. package/lib/types.d.ts +11 -89
  18. package/package.json +21 -17
  19. package/src/authors.ts +164 -0
  20. package/src/blogUtils.ts +306 -192
  21. package/{types.d.ts → src/deps.d.ts} +1 -1
  22. package/src/feed.ts +175 -0
  23. package/src/frontMatter.ts +81 -0
  24. package/src/index.ts +226 -223
  25. package/src/markdownLoader.ts +11 -16
  26. package/src/{pluginOptionSchema.ts → options.ts} +54 -14
  27. package/src/plugin-content-blog.d.ts +591 -0
  28. package/src/translations.ts +66 -0
  29. package/src/types.ts +15 -105
  30. package/index.d.ts +0 -138
  31. package/lib/.tsbuildinfo +0 -4459
  32. package/lib/blogFrontMatter.d.ts +0 -28
  33. package/lib/blogFrontMatter.js +0 -50
  34. package/lib/pluginOptionSchema.d.ts +0 -33
  35. package/src/__tests__/__fixtures__/website/blog/2018-12-14-Happy-First-Birthday-Slash.md +0 -5
  36. package/src/__tests__/__fixtures__/website/blog/complex-slug.md +0 -7
  37. package/src/__tests__/__fixtures__/website/blog/date-matter.md +0 -5
  38. package/src/__tests__/__fixtures__/website/blog/draft.md +0 -6
  39. package/src/__tests__/__fixtures__/website/blog/heading-as-title.md +0 -5
  40. package/src/__tests__/__fixtures__/website/blog/simple-slug.md +0 -7
  41. package/src/__tests__/__fixtures__/website/blog-with-ref/2018-12-14-Happy-First-Birthday-Slash.md +0 -5
  42. package/src/__tests__/__fixtures__/website/blog-with-ref/post-with-broken-links.md +0 -11
  43. package/src/__tests__/__fixtures__/website/blog-with-ref/post.md +0 -5
  44. package/src/__tests__/__fixtures__/website/i18n/en/docusaurus-plugin-content-blog/2018-12-14-Happy-First-Birthday-Slash.md +0 -5
  45. package/src/__tests__/__fixtures__/website-blog-without-date/blog/no date.md +0 -1
  46. package/src/__tests__/__snapshots__/generateBlogFeed.test.ts.snap +0 -101
  47. package/src/__tests__/__snapshots__/linkify.test.ts.snap +0 -24
  48. package/src/__tests__/__snapshots__/pluginOptionSchema.test.ts.snap +0 -5
  49. package/src/__tests__/blogFrontMatter.test.ts +0 -265
  50. package/src/__tests__/generateBlogFeed.test.ts +0 -100
  51. package/src/__tests__/index.test.ts +0 -336
  52. package/src/__tests__/linkify.test.ts +0 -93
  53. package/src/__tests__/pluginOptionSchema.test.ts +0 -150
  54. package/src/blogFrontMatter.ts +0 -87
  55. package/tsconfig.json +0 -9
@@ -6,14 +6,15 @@
6
6
  * LICENSE file in the root directory of this source tree.
7
7
  */
8
8
  Object.defineProperty(exports, "__esModule", { value: true });
9
- exports.PluginOptionSchema = exports.DEFAULT_OPTIONS = void 0;
9
+ exports.validateOptions = exports.DEFAULT_OPTIONS = void 0;
10
10
  const utils_validation_1 = require("@docusaurus/utils-validation");
11
+ const utils_1 = require("@docusaurus/utils");
11
12
  exports.DEFAULT_OPTIONS = {
12
- feedOptions: { type: ['rss', 'atom'] },
13
+ feedOptions: { type: ['rss', 'atom'], copyright: '' },
13
14
  beforeDefaultRehypePlugins: [],
14
15
  beforeDefaultRemarkPlugins: [],
15
16
  admonitions: {},
16
- truncateMarker: /<!--\s*(truncate)\s*-->/,
17
+ truncateMarker: /<!--\s*truncate\s*-->/,
17
18
  rehypePlugins: [],
18
19
  remarkPlugins: [],
19
20
  showReadingTime: true,
@@ -21,37 +22,49 @@ exports.DEFAULT_OPTIONS = {
21
22
  blogTagsListComponent: '@theme/BlogTagsListPage',
22
23
  blogPostComponent: '@theme/BlogPostPage',
23
24
  blogListComponent: '@theme/BlogListPage',
25
+ blogArchiveComponent: '@theme/BlogArchivePage',
24
26
  blogDescription: 'Blog',
25
27
  blogTitle: 'Blog',
26
28
  blogSidebarCount: 5,
27
29
  blogSidebarTitle: 'Recent posts',
28
30
  postsPerPage: 10,
29
- include: ['*.md', '*.mdx'],
31
+ include: ['**/*.{md,mdx}'],
32
+ exclude: utils_1.GlobExcludeDefault,
30
33
  routeBasePath: 'blog',
34
+ tagsBasePath: 'tags',
35
+ archiveBasePath: 'archive',
31
36
  path: 'blog',
32
37
  editLocalizedFiles: false,
38
+ authorsMapPath: 'authors.yml',
39
+ readingTime: ({ content, defaultReadingTime }) => defaultReadingTime({ content }),
40
+ sortPosts: 'descending',
33
41
  };
34
- exports.PluginOptionSchema = utils_validation_1.Joi.object({
42
+ const PluginOptionSchema = utils_validation_1.Joi.object({
35
43
  path: utils_validation_1.Joi.string().default(exports.DEFAULT_OPTIONS.path),
44
+ archiveBasePath: utils_validation_1.Joi.string()
45
+ .default(exports.DEFAULT_OPTIONS.archiveBasePath)
46
+ .allow(null),
36
47
  routeBasePath: utils_validation_1.Joi.string()
37
48
  // '' not allowed, see https://github.com/facebook/docusaurus/issues/3374
38
49
  // .allow('')
39
50
  .default(exports.DEFAULT_OPTIONS.routeBasePath),
51
+ tagsBasePath: utils_validation_1.Joi.string().default(exports.DEFAULT_OPTIONS.tagsBasePath),
40
52
  include: utils_validation_1.Joi.array().items(utils_validation_1.Joi.string()).default(exports.DEFAULT_OPTIONS.include),
41
- postsPerPage: utils_validation_1.Joi.number()
42
- .integer()
43
- .min(1)
53
+ exclude: utils_validation_1.Joi.array().items(utils_validation_1.Joi.string()).default(exports.DEFAULT_OPTIONS.exclude),
54
+ postsPerPage: utils_validation_1.Joi.alternatives()
55
+ .try(utils_validation_1.Joi.equal('ALL').required(), utils_validation_1.Joi.number().integer().min(1).required())
44
56
  .default(exports.DEFAULT_OPTIONS.postsPerPage),
45
57
  blogListComponent: utils_validation_1.Joi.string().default(exports.DEFAULT_OPTIONS.blogListComponent),
46
58
  blogPostComponent: utils_validation_1.Joi.string().default(exports.DEFAULT_OPTIONS.blogPostComponent),
47
59
  blogTagsListComponent: utils_validation_1.Joi.string().default(exports.DEFAULT_OPTIONS.blogTagsListComponent),
48
60
  blogTagsPostsComponent: utils_validation_1.Joi.string().default(exports.DEFAULT_OPTIONS.blogTagsPostsComponent),
61
+ blogArchiveComponent: utils_validation_1.Joi.string().default(exports.DEFAULT_OPTIONS.blogArchiveComponent),
49
62
  blogTitle: utils_validation_1.Joi.string().allow('').default(exports.DEFAULT_OPTIONS.blogTitle),
50
63
  blogDescription: utils_validation_1.Joi.string()
51
64
  .allow('')
52
65
  .default(exports.DEFAULT_OPTIONS.blogDescription),
53
66
  blogSidebarCount: utils_validation_1.Joi.alternatives()
54
- .try(utils_validation_1.Joi.equal('ALL').required(), utils_validation_1.Joi.number().required())
67
+ .try(utils_validation_1.Joi.equal('ALL').required(), utils_validation_1.Joi.number().integer().min(0).required())
55
68
  .default(exports.DEFAULT_OPTIONS.blogSidebarCount),
56
69
  blogSidebarTitle: utils_validation_1.Joi.string().default(exports.DEFAULT_OPTIONS.blogSidebarTitle),
57
70
  showReadingTime: utils_validation_1.Joi.bool().default(exports.DEFAULT_OPTIONS.showReadingTime),
@@ -65,14 +78,31 @@ exports.PluginOptionSchema = utils_validation_1.Joi.object({
65
78
  beforeDefaultRehypePlugins: utils_validation_1.RehypePluginsSchema.default(exports.DEFAULT_OPTIONS.beforeDefaultRehypePlugins),
66
79
  feedOptions: utils_validation_1.Joi.object({
67
80
  type: utils_validation_1.Joi.alternatives()
68
- .try(utils_validation_1.Joi.array().items(utils_validation_1.Joi.string()), utils_validation_1.Joi.alternatives().conditional(utils_validation_1.Joi.string().equal('all', 'rss', 'atom'), {
69
- then: utils_validation_1.Joi.custom((val) => val === 'all' ? ['rss', 'atom'] : [val]),
81
+ .try(utils_validation_1.Joi.array().items(utils_validation_1.Joi.string().equal('rss', 'atom', 'json')), utils_validation_1.Joi.alternatives().conditional(utils_validation_1.Joi.string().equal('all', 'rss', 'atom', 'json'), {
82
+ then: utils_validation_1.Joi.custom((val) => val === 'all' ? ['rss', 'atom', 'json'] : [val]),
70
83
  }))
71
84
  .allow(null)
72
85
  .default(exports.DEFAULT_OPTIONS.feedOptions.type),
73
86
  title: utils_validation_1.Joi.string().allow(''),
74
87
  description: utils_validation_1.Joi.string().allow(''),
75
- copyright: utils_validation_1.Joi.string(),
88
+ // only add default value when user actually wants a feed (type is not null)
89
+ copyright: utils_validation_1.Joi.when('type', {
90
+ is: utils_validation_1.Joi.any().valid(null),
91
+ then: utils_validation_1.Joi.string().optional(),
92
+ otherwise: utils_validation_1.Joi.string()
93
+ .allow('')
94
+ .default(exports.DEFAULT_OPTIONS.feedOptions.copyright),
95
+ }),
76
96
  language: utils_validation_1.Joi.string(),
77
97
  }).default(exports.DEFAULT_OPTIONS.feedOptions),
78
- });
98
+ authorsMapPath: utils_validation_1.Joi.string().default(exports.DEFAULT_OPTIONS.authorsMapPath),
99
+ readingTime: utils_validation_1.Joi.function().default(() => exports.DEFAULT_OPTIONS.readingTime),
100
+ sortPosts: utils_validation_1.Joi.string()
101
+ .valid('descending', 'ascending')
102
+ .default(exports.DEFAULT_OPTIONS.sortPosts),
103
+ }).default(exports.DEFAULT_OPTIONS);
104
+ function validateOptions({ validate, options, }) {
105
+ const validatedOptions = validate(PluginOptionSchema, options);
106
+ return validatedOptions;
107
+ }
108
+ exports.validateOptions = validateOptions;
@@ -0,0 +1,11 @@
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 { BlogContent } from './types';
8
+ import type { TranslationFiles } from '@docusaurus/types';
9
+ import type { PluginOptions } from '@docusaurus/plugin-content-blog';
10
+ export declare function getTranslationFiles(options: PluginOptions): TranslationFiles;
11
+ export declare function translateContent(content: BlogContent, translationFiles: TranslationFiles): BlogContent;
@@ -0,0 +1,53 @@
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.translateContent = exports.getTranslationFiles = void 0;
10
+ function translateListPage(blogListPaginated, translations) {
11
+ return blogListPaginated.map((page) => {
12
+ const { items, metadata } = page;
13
+ return {
14
+ items,
15
+ metadata: {
16
+ ...metadata,
17
+ blogTitle: translations.title?.message ?? page.metadata.blogTitle,
18
+ blogDescription: translations.description?.message ?? page.metadata.blogDescription,
19
+ },
20
+ };
21
+ });
22
+ }
23
+ function getTranslationFiles(options) {
24
+ return [
25
+ {
26
+ path: 'options',
27
+ content: {
28
+ title: {
29
+ message: options.blogTitle,
30
+ description: 'The title for the blog used in SEO',
31
+ },
32
+ description: {
33
+ message: options.blogDescription,
34
+ description: 'The description for the blog used in SEO',
35
+ },
36
+ 'sidebar.title': {
37
+ message: options.blogSidebarTitle,
38
+ description: 'The label for the left sidebar',
39
+ },
40
+ },
41
+ },
42
+ ];
43
+ }
44
+ exports.getTranslationFiles = getTranslationFiles;
45
+ function translateContent(content, translationFiles) {
46
+ const { content: optionsTranslations } = translationFiles[0];
47
+ return {
48
+ ...content,
49
+ blogSidebarTitle: optionsTranslations['sidebar.title']?.message ?? content.blogSidebarTitle,
50
+ blogListPaginated: translateListPage(content.blogListPaginated, optionsTranslations),
51
+ };
52
+ }
53
+ exports.translateContent = translateContent;
package/lib/types.d.ts CHANGED
@@ -4,120 +4,42 @@
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 type { RemarkAndRehypePluginOptions } from '@docusaurus/mdx-loader';
8
- import { BrokenMarkdownLink, ContentPaths } from '@docusaurus/utils/lib/markdownLinks';
7
+ import type { BrokenMarkdownLink, ContentPaths } from '@docusaurus/utils';
8
+ import type { BlogPostMetadata } from '@docusaurus/plugin-content-blog';
9
+ import type { Metadata as BlogPaginatedMetadata } from '@theme/BlogListPage';
9
10
  export declare type BlogContentPaths = ContentPaths;
10
11
  export interface BlogContent {
12
+ blogSidebarTitle: string;
11
13
  blogPosts: BlogPost[];
12
14
  blogListPaginated: BlogPaginated[];
13
15
  blogTags: BlogTags;
14
16
  blogTagsListPath: string | null;
15
17
  }
16
- export interface DateLink {
17
- date: Date;
18
- link: string;
19
- }
20
- export declare type FeedType = 'rss' | 'atom';
21
- export declare type EditUrlFunction = (editUrlParams: {
22
- blogDirPath: string;
23
- blogPath: string;
24
- permalink: string;
25
- locale: string;
26
- }) => string | undefined;
27
- export interface PluginOptions extends RemarkAndRehypePluginOptions {
28
- id?: string;
29
- path: string;
30
- routeBasePath: string;
31
- include: string[];
32
- postsPerPage: number;
33
- blogListComponent: string;
34
- blogPostComponent: string;
35
- blogTagsListComponent: string;
36
- blogTagsPostsComponent: string;
37
- blogTitle: string;
38
- blogDescription: string;
39
- blogSidebarCount: number | 'ALL';
40
- blogSidebarTitle: string;
41
- truncateMarker: RegExp;
42
- showReadingTime: boolean;
43
- feedOptions: {
44
- type?: [FeedType] | null;
45
- title?: string;
46
- description?: string;
47
- copyright: string;
48
- language?: string;
49
- };
50
- editUrl?: string | EditUrlFunction;
51
- editLocalizedFiles?: boolean;
52
- admonitions: Record<string, unknown>;
53
- }
54
18
  export interface BlogTags {
55
- [key: string]: BlogTag;
19
+ [tagKey: string]: BlogTag;
56
20
  }
57
21
  export interface BlogTag {
58
22
  name: string;
59
23
  items: string[];
60
24
  permalink: string;
25
+ pages: BlogPaginated[];
61
26
  }
62
27
  export interface BlogPost {
63
28
  id: string;
64
- metadata: MetaData;
65
- }
66
- export interface BlogPaginatedMetadata {
67
- permalink: string;
68
- page: number;
69
- postsPerPage: number;
70
- totalPages: number;
71
- totalCount: number;
72
- previousPage: string | null;
73
- nextPage: string | null;
74
- blogTitle: string;
75
- blogDescription: string;
29
+ metadata: BlogPostMetadata;
30
+ content: string;
76
31
  }
77
32
  export interface BlogPaginated {
78
33
  metadata: BlogPaginatedMetadata;
79
34
  items: string[];
80
35
  }
81
- export interface MetaData {
82
- permalink: string;
83
- source: string;
84
- description: string;
85
- date: Date;
86
- formattedDate: string;
87
- tags: (Tag | string)[];
88
- title: string;
89
- readingTime?: number;
90
- prevItem?: Paginator;
91
- nextItem?: Paginator;
92
- truncated: boolean;
93
- editUrl?: string;
94
- }
95
- export interface Paginator {
96
- title: string;
97
- permalink: string;
98
- }
99
- export interface Tag {
100
- label: string;
101
- permalink: string;
102
- }
103
- export interface BlogItemsToMetadata {
104
- [key: string]: MetaData;
105
- }
106
- export interface TagsModule {
107
- [key: string]: TagModule;
108
- }
109
- export interface TagModule {
110
- allTagsPath: string;
111
- slug: string;
112
- name: string;
113
- count: number;
114
- permalink: string;
115
- }
116
36
  export declare type BlogBrokenMarkdownLink = BrokenMarkdownLink<BlogContentPaths>;
117
37
  export declare type BlogMarkdownLoaderOptions = {
118
38
  siteDir: string;
119
39
  contentPaths: BlogContentPaths;
120
40
  truncateMarker: RegExp;
121
- sourceToPermalink: Record<string, string>;
41
+ sourceToPermalink: {
42
+ [aliasedPath: string]: string;
43
+ };
122
44
  onBrokenMarkdownLink: (brokenMarkdownLink: BlogBrokenMarkdownLink) => void;
123
45
  };
package/package.json CHANGED
@@ -1,9 +1,9 @@
1
1
  {
2
2
  "name": "@docusaurus/plugin-content-blog",
3
- "version": "2.0.0-beta.15d451942",
3
+ "version": "2.0.0-beta.18",
4
4
  "description": "Blog plugin for Docusaurus.",
5
5
  "main": "lib/index.js",
6
- "types": "index.d.ts",
6
+ "types": "src/plugin-content-blog.d.ts",
7
7
  "scripts": {
8
8
  "build": "tsc",
9
9
  "watch": "tsc --watch"
@@ -18,28 +18,32 @@
18
18
  },
19
19
  "license": "MIT",
20
20
  "dependencies": {
21
- "@docusaurus/core": "2.0.0-beta.15d451942",
22
- "@docusaurus/mdx-loader": "2.0.0-beta.15d451942",
23
- "@docusaurus/types": "2.0.0-beta.15d451942",
24
- "@docusaurus/utils": "2.0.0-beta.15d451942",
25
- "@docusaurus/utils-validation": "2.0.0-beta.15d451942",
26
- "chalk": "^4.1.1",
21
+ "@docusaurus/core": "2.0.0-beta.18",
22
+ "@docusaurus/logger": "2.0.0-beta.18",
23
+ "@docusaurus/mdx-loader": "2.0.0-beta.18",
24
+ "@docusaurus/utils": "2.0.0-beta.18",
25
+ "@docusaurus/utils-common": "2.0.0-beta.18",
26
+ "@docusaurus/utils-validation": "2.0.0-beta.18",
27
+ "cheerio": "^1.0.0-rc.10",
27
28
  "feed": "^4.2.2",
28
- "fs-extra": "^10.0.0",
29
- "globby": "^11.0.2",
30
- "loader-utils": "^2.0.0",
31
- "lodash": "^4.17.20",
32
- "reading-time": "^1.3.0",
29
+ "fs-extra": "^10.0.1",
30
+ "lodash": "^4.17.21",
31
+ "reading-time": "^1.5.0",
33
32
  "remark-admonitions": "^1.2.1",
34
- "tslib": "^2.2.0",
35
- "webpack": "^5.37.0"
33
+ "tslib": "^2.3.1",
34
+ "utility-types": "^3.10.0",
35
+ "webpack": "^5.70.0"
36
+ },
37
+ "devDependencies": {
38
+ "@docusaurus/types": "2.0.0-beta.18",
39
+ "escape-string-regexp": "^4.0.0"
36
40
  },
37
41
  "peerDependencies": {
38
42
  "react": "^16.8.4 || ^17.0.0",
39
43
  "react-dom": "^16.8.4 || ^17.0.0"
40
44
  },
41
45
  "engines": {
42
- "node": ">=12.13.0"
46
+ "node": ">=14"
43
47
  },
44
- "gitHead": "dfceae8bd3e8b42ba8fc14f1449d861046c26143"
48
+ "gitHead": "1a945d06993d53376e61bed2c942799fe07dc336"
45
49
  }
package/src/authors.ts ADDED
@@ -0,0 +1,164 @@
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 type {BlogContentPaths} from './types';
9
+ import {getDataFileData} from '@docusaurus/utils';
10
+ import {Joi, URISchema} from '@docusaurus/utils-validation';
11
+ import type {
12
+ Author,
13
+ BlogPostFrontMatter,
14
+ BlogPostFrontMatterAuthor,
15
+ BlogPostFrontMatterAuthors,
16
+ } from '@docusaurus/plugin-content-blog';
17
+
18
+ export type AuthorsMap = {[authorKey: string]: Author};
19
+
20
+ const AuthorsMapSchema = Joi.object<AuthorsMap>()
21
+ .pattern(
22
+ Joi.string(),
23
+ Joi.object({
24
+ name: Joi.string(),
25
+ url: URISchema,
26
+ imageURL: URISchema,
27
+ title: Joi.string(),
28
+ email: Joi.string(),
29
+ })
30
+ .rename('image_url', 'imageURL')
31
+ .or('name', 'imageURL')
32
+ .unknown()
33
+ .required()
34
+ .messages({
35
+ 'object.base':
36
+ '{#label} should be an author object containing properties like name, title, and imageURL.',
37
+ 'any.required':
38
+ '{#label} cannot be undefined. It should be an author object containing properties like name, title, and imageURL.',
39
+ }),
40
+ )
41
+ .messages({
42
+ 'object.base':
43
+ "The authors map file should contain an object where each entry contains an author key and the corresponding author's data.",
44
+ });
45
+
46
+ export function validateAuthorsMap(content: unknown): AuthorsMap {
47
+ return Joi.attempt(content, AuthorsMapSchema);
48
+ }
49
+
50
+ export async function getAuthorsMap(params: {
51
+ authorsMapPath: string;
52
+ contentPaths: BlogContentPaths;
53
+ }): Promise<AuthorsMap | undefined> {
54
+ return getDataFileData(
55
+ {
56
+ filePath: params.authorsMapPath,
57
+ contentPaths: params.contentPaths,
58
+ fileType: 'authors map',
59
+ },
60
+ validateAuthorsMap,
61
+ );
62
+ }
63
+
64
+ type AuthorsParam = {
65
+ frontMatter: BlogPostFrontMatter;
66
+ authorsMap: AuthorsMap | undefined;
67
+ };
68
+
69
+ // Legacy v1/early-v2 front matter fields
70
+ // We may want to deprecate those in favor of using only frontMatter.authors
71
+ function getFrontMatterAuthorLegacy(
72
+ frontMatter: BlogPostFrontMatter,
73
+ ): Author | undefined {
74
+ const name = frontMatter.author;
75
+ const title = frontMatter.author_title ?? frontMatter.authorTitle;
76
+ const url = frontMatter.author_url ?? frontMatter.authorURL;
77
+ const imageURL = frontMatter.author_image_url ?? frontMatter.authorImageURL;
78
+
79
+ if (name || title || url || imageURL) {
80
+ return {
81
+ name,
82
+ title,
83
+ url,
84
+ imageURL,
85
+ };
86
+ }
87
+
88
+ return undefined;
89
+ }
90
+
91
+ function normalizeFrontMatterAuthors(
92
+ frontMatterAuthors: BlogPostFrontMatterAuthors = [],
93
+ ): BlogPostFrontMatterAuthor[] {
94
+ function normalizeAuthor(
95
+ authorInput: string | Author,
96
+ ): BlogPostFrontMatterAuthor {
97
+ if (typeof authorInput === 'string') {
98
+ // Technically, we could allow users to provide an author's name here, but
99
+ // we only support keys, otherwise, a typo in a key would fallback to
100
+ // becoming a name and may end up unnoticed
101
+ return {key: authorInput};
102
+ }
103
+ return authorInput;
104
+ }
105
+
106
+ return Array.isArray(frontMatterAuthors)
107
+ ? frontMatterAuthors.map(normalizeAuthor)
108
+ : [normalizeAuthor(frontMatterAuthors)];
109
+ }
110
+
111
+ function getFrontMatterAuthors(params: AuthorsParam): Author[] {
112
+ const {authorsMap} = params;
113
+ const frontMatterAuthors = normalizeFrontMatterAuthors(
114
+ params.frontMatter.authors,
115
+ );
116
+
117
+ function getAuthorsMapAuthor(key: string | undefined): Author | undefined {
118
+ if (key) {
119
+ if (!authorsMap || Object.keys(authorsMap).length === 0) {
120
+ throw new Error(`Can't reference blog post authors by a key (such as '${key}') because no authors map file could be loaded.
121
+ Please double-check your blog plugin config (in particular 'authorsMapPath'), ensure the file exists at the configured path, is not empty, and is valid!`);
122
+ }
123
+ const author = authorsMap[key];
124
+ if (!author) {
125
+ throw Error(`Blog author with key "${key}" not found in the authors map file.
126
+ Valid author keys are:
127
+ ${Object.keys(authorsMap)
128
+ .map((validKey) => `- ${validKey}`)
129
+ .join('\n')}`);
130
+ }
131
+ return author;
132
+ }
133
+ return undefined;
134
+ }
135
+
136
+ function toAuthor(frontMatterAuthor: BlogPostFrontMatterAuthor): Author {
137
+ return {
138
+ // Author def from authorsMap can be locally overridden by front matter
139
+ ...getAuthorsMapAuthor(frontMatterAuthor.key),
140
+ ...frontMatterAuthor,
141
+ };
142
+ }
143
+
144
+ return frontMatterAuthors.map(toAuthor);
145
+ }
146
+
147
+ export function getBlogPostAuthors(params: AuthorsParam): Author[] {
148
+ const authorLegacy = getFrontMatterAuthorLegacy(params.frontMatter);
149
+ const authors = getFrontMatterAuthors(params);
150
+
151
+ if (authorLegacy) {
152
+ // Technically, we could allow mixing legacy/authors front matter, but do we
153
+ // really want to?
154
+ if (authors.length > 0) {
155
+ throw new Error(
156
+ `To declare blog post authors, use the 'authors' front matter in priority.
157
+ Don't mix 'authors' with other existing 'author_*' front matter. Choose one or the other, not both at the same time.`,
158
+ );
159
+ }
160
+ return [authorLegacy];
161
+ }
162
+
163
+ return authors;
164
+ }