ShowcaseDetails component

This commit is contained in:
ozakione 2024-03-24 23:06:16 +01:00
parent 7daa9a1d04
commit c542c81cb8
4 changed files with 68 additions and 35 deletions

View file

@ -16,6 +16,17 @@ import {contentAuthorsSchema} from './options';
import type {LoadContext, Plugin} from '@docusaurus/types'; import type {LoadContext, Plugin} from '@docusaurus/types';
import type {PluginOptions, Content} from '@docusaurus/plugin-showcase'; import type {PluginOptions, Content} from '@docusaurus/plugin-showcase';
// https://stackoverflow.com/a/71166133
const walk = async (dirPath: string): Promise<any[]> =>
Promise.all(
await fs.readdir(dirPath, {withFileTypes: true}).then((entries) =>
entries.map((entry) => {
const childPath = path.join(dirPath, entry.name);
return entry.isDirectory() ? walk(childPath) : childPath;
}),
),
);
export default function pluginContentShowcase( export default function pluginContentShowcase(
context: LoadContext, context: LoadContext,
options: PluginOptions, options: PluginOptions,
@ -36,35 +47,33 @@ export default function pluginContentShowcase(
// }, // },
async loadContent(): Promise<Content> { async loadContent(): Promise<Content> {
const files = await fs.readdir(path.join(siteDir, options.path)); const files: string[] = await walk(path.join(siteDir, options.path));
const yamlFiles = files.filter((file) => file.endsWith('.yaml')); console.log('allFiles:', files.flat(Number.POSITIVE_INFINITY));
const contentPromises = files
.flat(Number.POSITIVE_INFINITY)
.map(async (file) => {
const rawYaml = await fs.readFile(path.join(file), 'utf-8');
const yaml = Yaml.load(rawYaml);
const parsedYaml = contentAuthorsSchema.validate(yaml);
const contentPromises = yamlFiles.map(async (file) => { if (parsedYaml.error) {
const rawYaml = await fs.readFile( throw new Error(`Validation failed: ${parsedYaml.error.message}`, {
path.join(siteDir, options.path, file), cause: parsedYaml.error,
'utf-8', });
); }
const yaml = Yaml.load(rawYaml);
const parsedYaml = contentAuthorsSchema.validate(yaml);
if (parsedYaml.error) { const {title, description, preview, website, source, tags} =
throw new Error(`Validation failed: ${parsedYaml.error.message}`, { parsedYaml.value;
cause: parsedYaml.error,
});
}
const {title, description, preview, website, source, tags} = return {
parsedYaml.value; title,
description,
return { preview,
title, website,
description, source,
preview, tags,
website, };
source, });
tags,
};
});
const content = await Promise.all(contentPromises); const content = await Promise.all(contentPromises);
return { return {
@ -88,7 +97,7 @@ export default function pluginContentShowcase(
addRoute({ addRoute({
path: `/showcaseAll/${item.title}`, path: `/showcaseAll/${item.title}`,
component: '@theme/Showcase', component: '@theme/ShowcaseDetails',
modules: { modules: {
content: dataAuthor, content: dataAuthor,
}, },

View file

@ -247,10 +247,20 @@ declare module '@theme/BlogPostItems' {
export default function BlogPostItem(props: Props): JSX.Element; export default function BlogPostItem(props: Props): JSX.Element;
} }
declare module '@theme/Showcase' { declare module '@theme/ShowcaseDetails' {
import type {Content} from '@docusaurus/plugin-showcase'; import type {Content} from '@docusaurus/plugin-showcase';
export function prepareUserState(): UserState | undefined; export type User = Content['website'][number];
export type Props = {
content: User;
};
export default function Showcase(props: Props): JSX.Element;
}
declare module '@theme/Showcase' {
import type {Content} from '@docusaurus/plugin-showcase';
export type User = Content['website'][number]; export type User = Content['website'][number];
@ -296,16 +306,12 @@ declare module '@theme/Showcase/ShowcaseTagSelect' {
tag: TagType; tag: TagType;
} }
export function readSearchTags(search: string): TagType[];
export default function ShowcaseTagSelect(props: Props): JSX.Element; export default function ShowcaseTagSelect(props: Props): JSX.Element;
} }
declare module '@theme/Showcase/ShowcaseFilterToggle' { declare module '@theme/Showcase/ShowcaseFilterToggle' {
export type Operator = 'OR' | 'AND'; export type Operator = 'OR' | 'AND';
export const OperatorQueryKey = 'operator'; export const OperatorQueryKey = 'operator';
export function readOperator(search: string): Operator;
export default function ShowcaseFilterToggle(): JSX.Element; export default function ShowcaseFilterToggle(): JSX.Element;
} }

View file

@ -448,8 +448,7 @@ function ShowcaseCards({users}: {users: Users}) {
} }
export default function Showcase(props: Props): JSX.Element { export default function Showcase(props: Props): JSX.Element {
// TODO remove temporary to test showcase specific page const users = props.content;
const users = Array.isArray(props.content) ? props.content : [props.content];
return ( return (
<Layout title="Showcase"> <Layout title="Showcase">

View file

@ -0,0 +1,19 @@
/**
* 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 type {Props} from '@theme/ShowcaseDetails';
import Layout from '@theme/Layout';
export default function Showcase(props: Props): JSX.Element {
const user = props.content;
return (
<Layout title="Showcase Details">
<div>{user.title}</div>
</Layout>
);
}