-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathindex.js
More file actions
132 lines (127 loc) · 3.97 KB
/
Copy pathindex.js
File metadata and controls
132 lines (127 loc) · 3.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/**
* 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 React from 'react';
import clsx from 'clsx';
import Link from '@docusaurus/Link';
import {
useDocById,
findFirstSidebarItemLink,
} from '@docusaurus/plugin-content-docs/client';
import {usePluralForm} from '@docusaurus/theme-common';
import isInternalUrl from '@docusaurus/isInternalUrl';
import {translate} from '@docusaurus/Translate';
import Heading from '@theme/Heading';
import styles from './styles.module.css';
import useBaseUrl from '@docusaurus/useBaseUrl';
function useCategoryItemsPlural() {
const {selectMessage} = usePluralForm();
return (count) =>
selectMessage(
count,
translate(
{
message: '1 item|{count} items',
id: 'theme.docs.DocCard.categoryDescription.plurals',
description:
'The default description for a category card in the generated index about how many items this category includes',
},
{count},
),
);
}
function CardContainer({className, href, children}) {
return (
<Link
href={href}
className={clsx('card padding--lg', styles.cardContainer, className)}>
{children}
</Link>
);
}
function CardLayout({className, href, icon, title, description}) {
return (
<CardContainer href={href} className={className}>
<Heading
as="h2"
className={clsx('text--truncate', styles.cardTitle)}
title={title}>
{icon} {title}
</Heading>
{description && (
<p
className={clsx('text--truncate', styles.cardDescription)}
title={description}>
{description}
</p>
)}
</CardContainer>
);
}
/**
* Resolve a card's icon from the sidebar item's customProps, falling back to
* the theme default. Link items (the tiles on docs/providers.md, the homepage
* cards) and category items (the per-provider Quick Starts groups in
* sidebars.js) both support:
* iconComponent a React node, e.g. a react-icons component
* icon an image path under static/, e.g. '/img/providers/aws/favicon.ico'
* invertOnDark with `icon`, invert the image in dark mode
* emoji a string rendered in place of the default emoji
*/
function useItemIcon(item, defaultIcon) {
const cp = item?.customProps || {};
// Hooks must run unconditionally, so resolve the path even when unused.
const src = useBaseUrl(cp.icon || '');
if (cp.iconComponent) return cp.iconComponent;
if (cp.icon) {
const className = clsx(styles.cardIcon, cp.invertOnDark && styles.cardIconInvert);
return <img src={src} alt="" className={className} />;
}
if (cp.emoji) return <span className={styles.cardEmoji}>{cp.emoji}</span>;
return defaultIcon;
}
function CardCategory({item}) {
const href = findFirstSidebarItemLink(item);
const categoryItemsPlural = useCategoryItemsPlural();
const icon = useItemIcon(item, '🗃️');
// Unexpected: categories that don't have a link have been filtered upfront
if (!href) {
return null;
}
return (
<CardLayout
className={item.className}
href={href}
icon={icon}
title={item.label}
description={item.description ?? categoryItemsPlural(item.items.length)}
/>
);
}
function CardLink({item}) {
const defaultIcon = isInternalUrl(item.href) ? '📄️' : '🔗';
const icon = useItemIcon(item, defaultIcon);
const doc = useDocById(item.docId ?? undefined);
return (
<CardLayout
className={item.className}
href={item.href}
icon={icon}
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)}`);
}
}