Skip to content

Commit 988060c

Browse files
committed
[Docs Site] Add ComponentUsage component
1 parent 16d0eb9 commit 988060c

File tree

5 files changed

+62
-34
lines changed

5 files changed

+62
-34
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
---
2+
import { z } from "astro:schema";
3+
import { getComponentsUsage } from "~/util/components";
4+
import Details from "./Details.astro";
5+
6+
type Props = z.infer<typeof props>;
7+
8+
const props = z.object({
9+
component: z.string(),
10+
});
11+
12+
const { component } = props.parse(Astro.props);
13+
14+
const usage = await getComponentsUsage(component);
15+
---
16+
17+
<>
18+
<p>
19+
<code>{component}</code> is used <code>{usage.count}</code> times on{" "}
20+
<code>{usage.pages.size}</code> pages.
21+
</p>
22+
<Details header={`Pages which use ${component}`}>
23+
<ul>
24+
{
25+
[...usage.pages].map((path) => (
26+
<li>
27+
<a
28+
href={
29+
"https://github.com/cloudflare/cloudflare-docs/blob/production/" +
30+
path
31+
}
32+
target="_blank"
33+
>
34+
{path}
35+
</a>
36+
</li>
37+
))
38+
}
39+
</ul>
40+
</Details>
41+
</>
Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
import { getComponentsUsage } from "~/util/components";
33
import AnchorHeading from "./AnchorHeading.astro";
4-
import Details from "./Details.astro";
4+
import ComponentUsage from "./ComponentUsage.astro";
55
66
const components = await getComponentsUsage();
77
@@ -11,30 +11,10 @@ const alphabetical = Object.entries(components).sort(([a], [b]) =>
1111
---
1212

1313
{
14-
alphabetical.map(([component, usage]) => (
14+
alphabetical.map(([component]) => (
1515
<>
1616
<AnchorHeading depth={2} title={component} />
17-
<p>
18-
<code>{component}</code> is used <code>{usage.count}</code> times on{" "}
19-
<code>{usage.pages.size}</code> pages.
20-
</p>
21-
<Details header={`Pages which use ${component}`}>
22-
<ul>
23-
{[...usage.pages].map((path) => (
24-
<li>
25-
<a
26-
href={
27-
"https://github.com/cloudflare/cloudflare-docs/blob/production/" +
28-
path
29-
}
30-
target="_blank"
31-
>
32-
{path}
33-
</a>
34-
</li>
35-
))}
36-
</ul>
37-
</Details>
17+
<ComponentUsage component={component} />
3818
</>
3919
))
4020
}

src/components/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ export { default as APIRequest } from "./APIRequest.astro";
99
export { default as AvailableNotifications } from "./AvailableNotifications.astro";
1010
export { default as CompatibilityFlag } from "./CompatibilityFlag.astro";
1111
export { default as CompatibilityFlags } from "./CompatibilityFlags.astro";
12+
export { default as ComponentUsage } from "./ComponentUsage.astro";
1213
export { default as ComponentsUsage } from "./ComponentsUsage.astro";
1314
export { default as CopyPageButton } from "./CopyPageButton.tsx";
1415
export { default as CURL } from "./CURL.astro";

src/components/overrides/PageTitle.astro

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ import { marked } from "marked";
55
import { Breadcrumbs } from "astro-breadcrumbs";
66
import "astro-breadcrumbs/breadcrumbs.css";
77
8-
import { Aside } from "@astrojs/starlight/components";
98
import Description from "~/components/Description.astro";
109
import SpotlightAuthorDetails from "~/components/SpotlightAuthorDetails.astro";
1110
import LastReviewed from "~/components/LastReviewed.astro";
1211
import CopyPageButton from "~/components/CopyPageButton.tsx";
12+
import ComponentUsage from "~/components/ComponentUsage.astro";
1313
1414
import { getEntry } from "astro:content";
1515
@@ -122,13 +122,9 @@ const hideBreadcrumbs = Astro.locals.starlightRoute.hideBreadcrumbs;
122122

123123
{
124124
component && (
125-
<Aside>
126-
To see a list of pages this component is used on, please visit the{" "}
127-
<a href={`/style-guide/components/usage/#${component.toLowerCase()}`}>
128-
usage page
129-
</a>
130-
.
131-
</Aside>
125+
<div class="sl-markdown-content">
126+
<ComponentUsage component={component} />
127+
</div>
132128
)
133129
}
134130

src/util/components.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,15 @@ import {
99
} from "mdast-util-mdx";
1010
import { visit } from "unist-util-visit";
1111

12-
let usages: Record<string, { count: number; pages: Set<string> }>;
12+
type Usage = { count: number; pages: Set<string> };
1313

14-
export const getComponentsUsage = async () => {
14+
let usages: Record<string, Usage>;
15+
16+
export function getComponentsUsage(): Promise<Record<string, Usage>>;
17+
export function getComponentsUsage(component: string): Promise<Usage>;
18+
export async function getComponentsUsage(
19+
component?: string,
20+
): Promise<Usage | Record<string, Usage>> {
1521
if (!usages) {
1622
usages = {};
1723

@@ -48,5 +54,9 @@ export const getComponentsUsage = async () => {
4854
}
4955
}
5056

57+
if (component) {
58+
return usages[component] || { count: 0, pages: new Set() };
59+
}
60+
5161
return usages;
52-
};
62+
}

0 commit comments

Comments
 (0)