mirror of
https://github.com/facebook/docusaurus.git
synced 2025-07-28 14:08:21 +02:00
refactor: unify how validateOptions is handled (#6961)
* refactor: unify how validateOptions is handled * fix types * fix again
This commit is contained in:
parent
44107fb879
commit
6e2eb44964
43 changed files with 542 additions and 540 deletions
81
packages/docusaurus-plugin-content-blog/src/frontMatter.ts
Normal file
81
packages/docusaurus-plugin-content-blog/src/frontMatter.ts
Normal file
|
@ -0,0 +1,81 @@
|
|||
/**
|
||||
* Copyright (c) Facebook, Inc. and its affiliates.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the
|
||||
* LICENSE file in the root directory of this source tree.
|
||||
*/
|
||||
|
||||
import {
|
||||
JoiFrontMatter as Joi, // Custom instance for front matter
|
||||
URISchema,
|
||||
validateFrontMatter,
|
||||
FrontMatterTagsSchema,
|
||||
FrontMatterTOCHeadingLevels,
|
||||
} from '@docusaurus/utils-validation';
|
||||
import type {BlogPostFrontMatter} from '@docusaurus/plugin-content-blog';
|
||||
|
||||
const BlogPostFrontMatterAuthorSchema = Joi.object({
|
||||
key: Joi.string(),
|
||||
name: Joi.string(),
|
||||
title: Joi.string(),
|
||||
url: URISchema,
|
||||
imageURL: Joi.string(),
|
||||
})
|
||||
.or('key', 'name', 'imageURL')
|
||||
.rename('image_url', 'imageURL', {alias: true});
|
||||
|
||||
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).';
|
||||
|
||||
const BlogFrontMatterSchema = Joi.object<BlogPostFrontMatter>({
|
||||
id: Joi.string(),
|
||||
title: Joi.string().allow(''),
|
||||
description: Joi.string().allow(''),
|
||||
tags: FrontMatterTagsSchema,
|
||||
draft: Joi.boolean(),
|
||||
date: Joi.date().raw(),
|
||||
|
||||
// New multi-authors front matter:
|
||||
authors: Joi.alternatives()
|
||||
.try(
|
||||
Joi.string(),
|
||||
BlogPostFrontMatterAuthorSchema,
|
||||
Joi.array()
|
||||
.items(Joi.string(), BlogPostFrontMatterAuthorSchema)
|
||||
.messages({
|
||||
'array.sparse': FrontMatterAuthorErrorMessage,
|
||||
'array.includes': FrontMatterAuthorErrorMessage,
|
||||
}),
|
||||
)
|
||||
.messages({
|
||||
'alternatives.match': FrontMatterAuthorErrorMessage,
|
||||
}),
|
||||
// Legacy author front matter
|
||||
author: Joi.string(),
|
||||
author_title: Joi.string(),
|
||||
author_url: URISchema,
|
||||
author_image_url: URISchema,
|
||||
// TODO enable deprecation warnings later
|
||||
authorURL: URISchema,
|
||||
// .warning('deprecate.error', { alternative: '"author_url"'}),
|
||||
authorTitle: Joi.string(),
|
||||
// .warning('deprecate.error', { alternative: '"author_title"'}),
|
||||
authorImageURL: URISchema,
|
||||
// .warning('deprecate.error', { alternative: '"author_image_url"'}),
|
||||
|
||||
slug: Joi.string(),
|
||||
image: URISchema,
|
||||
keywords: Joi.array().items(Joi.string().required()),
|
||||
hide_table_of_contents: Joi.boolean(),
|
||||
|
||||
...FrontMatterTOCHeadingLevels,
|
||||
}).messages({
|
||||
'deprecate.error':
|
||||
'{#label} blog frontMatter field is deprecated. Please use {#alternative} instead.',
|
||||
});
|
||||
|
||||
export function validateBlogPostFrontMatter(
|
||||
frontMatter: Record<string, unknown>,
|
||||
): BlogPostFrontMatter {
|
||||
return validateFrontMatter(frontMatter, BlogFrontMatterSchema);
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue