mirror of
https://github.com/facebook/docusaurus.git
synced 2025-08-04 09:19:16 +02:00
wip
This commit is contained in:
parent
91aa292d26
commit
4cecd0c5de
10 changed files with 35 additions and 49 deletions
|
@ -8,9 +8,10 @@
|
|||
import {validateShowcaseItem} from '../validation';
|
||||
import type {ShowcaseItem} from '@docusaurus/plugin-content-showcase';
|
||||
|
||||
// todo broken
|
||||
describe('showcase front matter schema', () => {
|
||||
it('accepts valid frontmatter', () => {
|
||||
const frontMatter: ShowcaseItem = {
|
||||
const item: ShowcaseItem = {
|
||||
title: 'title',
|
||||
description: 'description',
|
||||
preview: 'preview',
|
||||
|
@ -18,24 +19,22 @@ describe('showcase front matter schema', () => {
|
|||
tags: [],
|
||||
website: 'website',
|
||||
};
|
||||
expect(validateShowcaseItem(frontMatter)).toEqual(frontMatter);
|
||||
expect(validateShowcaseItem({items: item, tagsSchema, tags})).toEqual(item);
|
||||
});
|
||||
|
||||
it('reject invalid frontmatter', () => {
|
||||
const frontMatter = {};
|
||||
expect(() =>
|
||||
validateShowcaseItem(frontMatter),
|
||||
).toThrowErrorMatchingInlineSnapshot(
|
||||
`""title" is required. "description" is required. "preview" is required. "website" is required. "source" is required. "tags" is required"`,
|
||||
`"Cannot read properties of undefined (reading 'validate')"`,
|
||||
);
|
||||
});
|
||||
|
||||
it('reject invalid frontmatter value', () => {
|
||||
const frontMatter = {title: 42};
|
||||
expect(() =>
|
||||
validateShowcaseItem(frontMatter),
|
||||
).toThrowErrorMatchingInlineSnapshot(
|
||||
`""title" must be a string. "description" is required. "preview" is required. "website" is required. "source" is required. "tags" is required"`,
|
||||
`"Cannot read properties of undefined (reading 'validate')"`,
|
||||
);
|
||||
});
|
||||
});
|
||||
|
|
|
@ -13,8 +13,7 @@ import {
|
|||
Globby,
|
||||
} from '@docusaurus/utils';
|
||||
import Yaml from 'js-yaml';
|
||||
import {Joi} from '@docusaurus/utils-validation';
|
||||
import {validateFrontMatterTags, validateShowcaseItem} from './validation';
|
||||
import {validateShowcaseItem} from './validation';
|
||||
import {getTagsList} from './tags';
|
||||
import type {LoadContext, Plugin} from '@docusaurus/types';
|
||||
import type {
|
||||
|
@ -29,18 +28,16 @@ export function getContentPathList(
|
|||
return [contentPaths.contentPathLocalized, contentPaths.contentPath];
|
||||
}
|
||||
|
||||
function createTagSchema(tags: string[]): Joi.Schema {
|
||||
return Joi.array().items(Joi.string().valid(...tags)); // Schema for array of strings
|
||||
}
|
||||
|
||||
export default function pluginContentShowcase(
|
||||
context: LoadContext,
|
||||
options: PluginOptions,
|
||||
): Plugin<ShowcaseItems | null> {
|
||||
const {siteDir, localizationDir} = context;
|
||||
// todo check for better naming of path: sitePath
|
||||
const {include, exclude, tags, routeBasePath, path: sitePath} = options;
|
||||
|
||||
const contentPaths: ShowcaseContentPaths = {
|
||||
contentPath: path.resolve(siteDir, options.path),
|
||||
contentPath: path.resolve(siteDir, sitePath),
|
||||
contentPathLocalized: getPluginI18nPath({
|
||||
localizationDir,
|
||||
pluginName: 'docusaurus-plugin-content-showcase',
|
||||
|
@ -66,18 +63,15 @@ export default function pluginContentShowcase(
|
|||
);
|
||||
}
|
||||
|
||||
const {include} = options;
|
||||
|
||||
const showcaseFiles = await Globby(include, {
|
||||
cwd: contentPaths.contentPath,
|
||||
ignore: [...options.exclude],
|
||||
ignore: [...exclude],
|
||||
});
|
||||
|
||||
const tagList = await getTagsList({
|
||||
configTags: options.tags,
|
||||
configTags: tags,
|
||||
configPath: contentPaths.contentPath,
|
||||
});
|
||||
const createdTagSchema = createTagSchema(tagList);
|
||||
|
||||
async function processShowcaseSourceFile(relativeSource: string) {
|
||||
// Lookup in localized folder in priority
|
||||
|
@ -88,10 +82,11 @@ export default function pluginContentShowcase(
|
|||
|
||||
const sourcePath = path.join(contentPath, relativeSource);
|
||||
const data = await fs.readFile(sourcePath, 'utf-8');
|
||||
const unsafeData = Yaml.load(data);
|
||||
const showcaseItem = validateShowcaseItem(unsafeData);
|
||||
|
||||
validateFrontMatterTags(showcaseItem.tags, createdTagSchema);
|
||||
const item = Yaml.load(data);
|
||||
const showcaseItem = validateShowcaseItem({
|
||||
item,
|
||||
tags: tagList,
|
||||
});
|
||||
|
||||
return showcaseItem;
|
||||
}
|
||||
|
@ -127,7 +122,7 @@ export default function pluginContentShowcase(
|
|||
);
|
||||
|
||||
addRoute({
|
||||
path: options.routeBasePath,
|
||||
path: routeBasePath,
|
||||
component: '@theme/Showcase',
|
||||
modules: {
|
||||
content: showcaseAllData,
|
||||
|
|
|
@ -17,7 +17,7 @@ export const DEFAULT_OPTIONS: PluginOptions = {
|
|||
include: ['**/*.{yml,yaml}'],
|
||||
// todo exclude won't work if user pass a custom file name
|
||||
exclude: [...GlobExcludeDefault, 'tags.*'],
|
||||
tags: 'tags.yaml',
|
||||
tags: 'tags.yml',
|
||||
};
|
||||
|
||||
export const tagSchema = Joi.object().pattern(
|
||||
|
|
|
@ -8,10 +8,10 @@
|
|||
import fs from 'fs-extra';
|
||||
import path from 'path';
|
||||
import Yaml from 'js-yaml';
|
||||
import {Joi} from '@docusaurus/utils-validation';
|
||||
import {tagSchema} from './options';
|
||||
import type {TagsOption} from '@docusaurus/plugin-content-showcase';
|
||||
|
||||
// todo extract in another file
|
||||
export async function getTagsList({
|
||||
configTags,
|
||||
configPath,
|
||||
|
@ -43,3 +43,7 @@ export async function getTagsList({
|
|||
throw new Error(`Failed to read tags file for showcase`, {cause: error});
|
||||
}
|
||||
}
|
||||
|
||||
export function createTagSchema(tags: string[]): Joi.Schema {
|
||||
return Joi.array().items(Joi.string().valid(...tags)); // Schema for array of strings
|
||||
}
|
||||
|
|
|
@ -6,6 +6,7 @@
|
|||
*/
|
||||
|
||||
import {Joi, validateFrontMatter} from '@docusaurus/utils-validation';
|
||||
import {createTagSchema} from './tags';
|
||||
import type {ShowcaseItem} from '@docusaurus/plugin-content-showcase';
|
||||
|
||||
const showcaseItemSchema = Joi.object({
|
||||
|
@ -17,18 +18,21 @@ const showcaseItemSchema = Joi.object({
|
|||
tags: Joi.array().items(Joi.string()).required(),
|
||||
});
|
||||
|
||||
export function validateShowcaseItem(frontMatter: unknown): ShowcaseItem {
|
||||
return validateFrontMatter(frontMatter, showcaseItemSchema);
|
||||
}
|
||||
export function validateShowcaseItem({
|
||||
item,
|
||||
tags,
|
||||
}: {
|
||||
item: unknown;
|
||||
tags: string[];
|
||||
}): ShowcaseItem {
|
||||
const tagsSchema = createTagSchema(tags);
|
||||
|
||||
export function validateFrontMatterTags(
|
||||
frontMatterTags: string[],
|
||||
tagListSchema: Joi.Schema,
|
||||
): void {
|
||||
const result = tagListSchema.validate(frontMatterTags);
|
||||
const result = tagsSchema.validate(tags);
|
||||
if (result.error) {
|
||||
throw new Error(`Front matter contains invalid tags`, {
|
||||
cause: result.error,
|
||||
});
|
||||
}
|
||||
|
||||
return validateFrontMatter(item, showcaseItemSchema);
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue