fix(build): build error caused by non-exsisting readme
This commit is contained in:
parent
34c949d43f
commit
f2ceda77aa
|
|
@ -2,97 +2,103 @@ import React from 'react';
|
||||||
import clsx from 'clsx';
|
import clsx from 'clsx';
|
||||||
import Link from '@docusaurus/Link';
|
import Link from '@docusaurus/Link';
|
||||||
import {
|
import {
|
||||||
useDocById,
|
useDocById,
|
||||||
findFirstSidebarItemLink,
|
findFirstSidebarItemLink,
|
||||||
} from '@docusaurus/plugin-content-docs/client';
|
} from '@docusaurus/plugin-content-docs/client';
|
||||||
import {usePluralForm} from '@docusaurus/theme-common';
|
import { usePluralForm } from '@docusaurus/theme-common';
|
||||||
import isInternalUrl from '@docusaurus/isInternalUrl';
|
import isInternalUrl from '@docusaurus/isInternalUrl';
|
||||||
import {translate} from '@docusaurus/Translate';
|
import { translate } from '@docusaurus/Translate';
|
||||||
import Heading from '@theme/Heading';
|
import Heading from '@theme/Heading';
|
||||||
import styles from './styles.module.css';
|
import styles from './styles.module.css';
|
||||||
function useCategoryItemsPlural() {
|
function useCategoryItemsPlural() {
|
||||||
const {selectMessage} = usePluralForm();
|
const { selectMessage } = usePluralForm();
|
||||||
return (count) =>
|
return (count) =>
|
||||||
selectMessage(
|
selectMessage(
|
||||||
count,
|
count,
|
||||||
translate(
|
translate(
|
||||||
{
|
{
|
||||||
message: '1 item|{count} items',
|
message: '1 item|{count} items',
|
||||||
id: 'theme.docs.DocCard.categoryDescription.plurals',
|
id: 'theme.docs.DocCard.categoryDescription.plurals',
|
||||||
description:
|
description:
|
||||||
'The default description for a category card in the generated index about how many items this category includes',
|
'The default description for a category card in the generated index about how many items this category includes',
|
||||||
},
|
},
|
||||||
{count},
|
{ count },
|
||||||
),
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
function CardContainer({ className, href, children }) {
|
||||||
|
return (
|
||||||
|
<Link
|
||||||
|
href={href}
|
||||||
|
className={clsx('card padding--lg', styles.cardContainer, className)}>
|
||||||
|
{children}
|
||||||
|
</Link>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
function CardContainer({className, href, children}) {
|
function CardLayout({ className, href, icon, title, description }) {
|
||||||
return (
|
return (
|
||||||
<Link
|
<CardContainer href={href} className={className}>
|
||||||
href={href}
|
<Heading
|
||||||
className={clsx('card padding--lg', styles.cardContainer, className)}>
|
as="h2"
|
||||||
{children}
|
className={clsx('text--truncate', styles.cardTitle)}
|
||||||
</Link>
|
title={title}>
|
||||||
);
|
{icon} {title}
|
||||||
|
</Heading>
|
||||||
|
{description && (
|
||||||
|
<p
|
||||||
|
className={clsx('text--truncate', styles.cardDescription)}
|
||||||
|
title={description}>
|
||||||
|
{description}
|
||||||
|
</p>
|
||||||
|
)}
|
||||||
|
</CardContainer>
|
||||||
|
);
|
||||||
}
|
}
|
||||||
function CardLayout({className, href, icon, title, description}) {
|
function CardCategory({ item }) {
|
||||||
return (
|
const href = findFirstSidebarItemLink(item);
|
||||||
<CardContainer href={href} className={className}>
|
const categoryItemsPlural = useCategoryItemsPlural();
|
||||||
<Heading
|
// Unexpected: categories that don't have a link have been filtered upfront
|
||||||
as="h2"
|
if (!href) {
|
||||||
className={clsx('text--truncate', styles.cardTitle)}
|
return null;
|
||||||
title={title}>
|
}
|
||||||
{icon} {title}
|
const ReadMeSubfix = href.endsWith('/') ? 'README' : '/README';
|
||||||
</Heading>
|
const docId = href.substring(6) + ReadMeSubfix;
|
||||||
{description && (
|
let doc;
|
||||||
<p
|
try {
|
||||||
className={clsx('text--truncate', styles.cardDescription)}
|
doc = useDocById(docId ?? undefined);
|
||||||
title={description}>
|
} catch (e) {
|
||||||
{description}
|
doc = null;
|
||||||
</p>
|
}
|
||||||
)}
|
return (
|
||||||
</CardContainer>
|
<CardLayout
|
||||||
);
|
className={item.className}
|
||||||
|
href={href}
|
||||||
|
icon="🗃️"
|
||||||
|
title={item.label}
|
||||||
|
description={item.description ?? doc?.description ?? categoryItemsPlural(item.items.length)}
|
||||||
|
/>
|
||||||
|
);
|
||||||
}
|
}
|
||||||
function CardCategory({item}) {
|
function CardLink({ item }) {
|
||||||
const href = findFirstSidebarItemLink(item);
|
const icon = isInternalUrl(item.href) ? '📄️' : '🔗';
|
||||||
const categoryItemsPlural = useCategoryItemsPlural();
|
const doc = useDocById(item.docId ?? undefined);
|
||||||
// Unexpected: categories that don't have a link have been filtered upfront
|
return (
|
||||||
if (!href) {
|
<CardLayout
|
||||||
return null;
|
className={item.className}
|
||||||
}
|
href={item.href}
|
||||||
const docId = href.substring(6) + 'README';
|
icon={icon}
|
||||||
const doc = useDocById(docId ?? undefined);
|
title={item.label}
|
||||||
return (
|
description={item.description ?? doc?.description}
|
||||||
<CardLayout
|
/>
|
||||||
className={item.className}
|
);
|
||||||
href={href}
|
|
||||||
icon="🗃️"
|
|
||||||
title={item.label}
|
|
||||||
description={item.description ?? doc.description ?? categoryItemsPlural(item.items.length)}
|
|
||||||
/>
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
function CardLink({item}) {
|
export default function DocCard({ item }) {
|
||||||
const icon = isInternalUrl(item.href) ? '📄️' : '🔗';
|
switch (item.type) {
|
||||||
const doc = useDocById(item.docId ?? undefined);
|
case 'link':
|
||||||
return (
|
return <CardLink item={item} />;
|
||||||
<CardLayout
|
case 'category':
|
||||||
className={item.className}
|
return <CardCategory item={item} />;
|
||||||
href={item.href}
|
default:
|
||||||
icon={icon}
|
throw new Error(`unknown item type ${JSON.stringify(item)}`);
|
||||||
title={item.label}
|
}
|
||||||
description={item.description ?? doc?.description}
|
|
||||||
/>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
export default function DocCard({item}) {
|
|
||||||
switch (item.type) {
|
|
||||||
case 'link':
|
|
||||||
return <CardLink item={item} />;
|
|
||||||
case 'category':
|
|
||||||
return <CardCategory item={item} />;
|
|
||||||
default:
|
|
||||||
throw new Error(`unknown item type ${JSON.stringify(item)}`);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue