feat(v2): option and config validation life cycle method for official plugins (#2943)

* add validation for blog plugin

* fix wrong default component

* fix test and add yup to package.json

* remove console.log

* add validation for classic theme and code block theme

* add yup to packages

* remove console.log

* fix build

* fix logo required

* replaced yup with joi

* fix test

* remove hapi from docusuars core

* replace joi with @hapi/joi

* fix eslint

* fix remark plugin type

* change remark plugin validation to match documentation

* move schema to it's own file

* allow unknown only on outer theme object

* fix type for schema type

* fix yarn.lock

* support both commonjs and ES modules

* add docs for new lifecycle method
This commit is contained in:
Anshul Goyal 2020-06-24 23:38:16 +05:30 committed by GitHub
parent ce10646606
commit 81d855355e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
18 changed files with 490 additions and 63 deletions

View file

@ -10,6 +10,7 @@ import kebabCase from 'lodash.kebabcase';
import path from 'path';
import admonitions from 'remark-admonitions';
import {normalizeUrl, docuHash, aliasedSitePath} from '@docusaurus/utils';
import {ValidationError} from '@hapi/joi';
import {
PluginOptions,
@ -18,9 +19,9 @@ import {
BlogItemsToMetadata,
TagsModule,
BlogPaginated,
FeedType,
BlogPost,
} from './types';
import {PluginOptionSchema} from './validation';
import {
LoadContext,
PluginContentLoadedActions,
@ -28,56 +29,19 @@ import {
Props,
Plugin,
HtmlTags,
OptionValidationContext,
ValidationResult,
} from '@docusaurus/types';
import {Configuration, Loader} from 'webpack';
import {generateBlogFeed, generateBlogPosts} from './blogUtils';
const DEFAULT_OPTIONS: PluginOptions = {
path: 'blog', // Path to data on filesystem, relative to site dir.
routeBasePath: 'blog', // URL Route.
include: ['*.md', '*.mdx'], // Extensions to include.
postsPerPage: 10, // How many posts per page.
blogListComponent: '@theme/BlogListPage',
blogPostComponent: '@theme/BlogPostPage',
blogTagsListComponent: '@theme/BlogTagsListPage',
blogTagsPostsComponent: '@theme/BlogTagsPostsPage',
showReadingTime: true,
remarkPlugins: [],
rehypePlugins: [],
editUrl: undefined,
truncateMarker: /<!--\s*(truncate)\s*-->/, // Regex.
admonitions: {},
};
function assertFeedTypes(val: any): asserts val is FeedType {
if (typeof val !== 'string' && !['rss', 'atom', 'all'].includes(val)) {
throw new Error(
`Invalid feedOptions type: ${val}. It must be either 'rss', 'atom', or 'all'`,
);
}
}
const getFeedTypes = (type?: FeedType) => {
assertFeedTypes(type);
let feedTypes: ('rss' | 'atom')[] = [];
if (type === 'all') {
feedTypes = ['rss', 'atom'];
} else {
feedTypes.push(type);
}
return feedTypes;
};
export default function pluginContentBlog(
context: LoadContext,
opts: Partial<PluginOptions>,
): Plugin<BlogContent | null> {
const options: PluginOptions = {...DEFAULT_OPTIONS, ...opts};
options: PluginOptions,
): Plugin<BlogContent | null, typeof PluginOptionSchema> {
if (options.admonitions) {
options.remarkPlugins = options.remarkPlugins.concat([
[admonitions, opts.admonitions || {}],
[admonitions, options.admonitions],
]);
}
@ -426,7 +390,7 @@ export default function pluginContentBlog(
},
async postBuild({outDir}: Props) {
if (!options.feedOptions) {
if (!options.feedOptions?.type) {
return;
}
@ -436,7 +400,7 @@ export default function pluginContentBlog(
return;
}
const feedTypes = getFeedTypes(options.feedOptions?.type);
const feedTypes = options.feedOptions.type;
await Promise.all(
feedTypes.map(async (feedType) => {
@ -456,11 +420,10 @@ export default function pluginContentBlog(
},
injectHtmlTags() {
if (!options.feedOptions) {
if (!options.feedOptions?.type) {
return {};
}
const feedTypes = getFeedTypes(options.feedOptions?.type);
const feedTypes = options.feedOptions.type;
const {
siteConfig: {title},
baseUrl,
@ -509,3 +472,14 @@ export default function pluginContentBlog(
},
};
}
export function validateOptions({
validate,
options,
}: OptionValidationContext<PluginOptions, ValidationError>): ValidationResult<
PluginOptions,
ValidationError
> {
const validatedOptions = validate(PluginOptionSchema, options);
return validatedOptions;
}