Skip to content

Commit 71cff11

Browse files
KianNHelithrar
authored andcommitted
[Docs Site] Add GitHubCode component (#17919)
1 parent ac85220 commit 71cff11

File tree

3 files changed

+194
-1
lines changed

3 files changed

+194
-1
lines changed

src/components/GitHubCode.astro

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
---
2+
import { z } from "astro:schema";
3+
import { Code } from "@astrojs/starlight/components";
4+
5+
type Props = z.infer<typeof props>;
6+
7+
const props = z
8+
.object({
9+
repo: z.string(),
10+
commit: z.string(),
11+
file: z.string(),
12+
lang: z.string(),
13+
lines: z
14+
.string()
15+
.transform((val) => val.split("-").map(Number))
16+
.optional(),
17+
tag: z.string().optional(),
18+
})
19+
.refine(
20+
(val) => {
21+
return !(val.lines && val.tag);
22+
},
23+
{ message: "Lines and tag are mutually exclusive filters." },
24+
);
25+
26+
const { repo, commit, file, lang, lines, tag } = props.parse(Astro.props);
27+
28+
const res = await fetch(
29+
`https://gh-code.developers.cloudflare.com/${repo}/${commit}/${file}`,
30+
);
31+
32+
if (!res.ok) {
33+
throw new Error(`[GitHubCode] Received ${res.status} from Worker.`);
34+
}
35+
36+
let content = await res.text();
37+
38+
if (lines) {
39+
const [start, end] = lines;
40+
41+
const contentLines = content.split("\n");
42+
43+
if (contentLines.length < end - 1) {
44+
throw new Error(
45+
`[GitHubCode] End line requested is beyond content length (${contentLines.length}).`,
46+
);
47+
}
48+
49+
content = contentLines.slice(start - 1, end).join("\n");
50+
} else if (tag) {
51+
const contentLines = content.split("\n");
52+
53+
const startTag = contentLines.findIndex((x) =>
54+
x.includes(`<docs-tag name="${tag}">`),
55+
);
56+
const endTag = contentLines.findIndex((x) =>
57+
x.includes(`</docs-tag name="${tag}">`),
58+
);
59+
60+
if (!startTag || !endTag) {
61+
throw new Error(`[GitHubCode] Unable to find a region using tag "${tag}".`);
62+
}
63+
64+
content = contentLines.slice(startTag + 1, endTag).join("\n");
65+
}
66+
---
67+
68+
<Code code={content} lang={lang} />

src/components/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ export { default as ExternalResources } from "./ExternalResources.astro";
1616
export { default as Feature } from "./Feature.astro";
1717
export { default as FeatureTable } from "./FeatureTable.astro";
1818
export { default as Flex } from "./Flex.astro";
19+
export { default as GitHubCode } from "./GitHubCode.astro";
1920
export { default as Glossary } from "./Glossary.astro";
2021
export { default as GlossaryDefinition } from "./GlossaryDefinition.astro";
2122
export { default as GlossaryTooltip } from "./GlossaryTooltip.astro";
@@ -51,7 +52,7 @@ export { default as ThreeCardGrid } from "./ThreeCardGrid.astro";
5152
export { default as TroubleshootingList } from "./TroubleshootingList.astro";
5253
export { default as TunnelCalculator } from "./TunnelCalculator.astro";
5354
export { default as Type } from "./Type.astro";
54-
export { default as TypeScriptExample } from "./TypeScriptExample.astro"
55+
export { default as TypeScriptExample } from "./TypeScriptExample.astro";
5556
export { default as WorkersArchitectureDiagram } from "./WorkersArchitectureDiagram.astro";
5657
export { default as WorkersIsolateDiagram } from "./WorkersIsolateDiagram.astro";
5758
export { default as WorkerStarter } from "./WorkerStarter.astro";
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
---
2+
title: GitHubCode
3+
---
4+
5+
The `GitHubCode` component allows you to include files from Cloudflare repositories.
6+
7+
The remote content can be filtered by lines or a region enclosed in tags.
8+
9+
## Import
10+
11+
```mdx
12+
import { GitHubCode } from "~/components";
13+
```
14+
15+
## Usage
16+
17+
```mdx live
18+
import { GitHubCode } from "~/components";
19+
20+
<GitHubCode
21+
repo="cloudflare/workers-rs"
22+
file="templates/hello-world/src/lib.rs"
23+
commit="ab3951b5c95329a600a7baa9f9bb1a7a95f1aeaa"
24+
lang="rs"
25+
/>
26+
```
27+
28+
### Filtering by lines
29+
30+
```mdx
31+
import { GitHubCode } from "~/components";
32+
33+
{/*
34+
import { foo } from "bar";
35+
36+
const baz = foo();
37+
38+
console.log(baz);
39+
*/}
40+
<GitHubCode
41+
repo="..."
42+
file="..."
43+
commit="..."
44+
lang="..."
45+
lines="1-3"
46+
/>
47+
{/*
48+
import { foo } from "bar";
49+
50+
const baz = foo();
51+
*/}
52+
```
53+
54+
### Filtering by tag
55+
56+
```mdx
57+
import { GitHubCode } from "~/components";
58+
59+
{/*
60+
<docs-tag name="no-logging">
61+
import { foo } from "bar";
62+
63+
const baz = foo();
64+
</docs-tag name="no-logging">
65+
66+
console.log(baz);
67+
*/}
68+
<GitHubCode
69+
repo="..."
70+
file="..."
71+
commit="..."
72+
lang="..."
73+
tag="no-logging"
74+
/>
75+
{/*
76+
import { foo } from "bar";
77+
78+
const baz = foo();
79+
*/}
80+
```
81+
82+
## `<GitHubCode>` Props
83+
84+
### `repo`
85+
86+
**required**
87+
**type:** `string`
88+
89+
The owner and repository to pull from, for example `cloudflare/workers-rs`.
90+
91+
### `file`
92+
93+
**required**
94+
**type:** `string`
95+
96+
The file path to pull from, for example `templates/hello-world/src/lib.rs`.
97+
98+
### `commit`
99+
100+
**required**
101+
**type:** `string`
102+
103+
The long (40-characters) Git commit hash to pull from, for example `ab3951b5c95329a600a7baa9f9bb1a7a95f1aeaa`.
104+
105+
### `lang`
106+
107+
**required**
108+
**type:** `string`
109+
110+
The language to use for the code block, for example `rs`.
111+
112+
### `lines`
113+
114+
**type:** `string`
115+
116+
A range of lines to filter the content using, for example `1-3`.
117+
118+
### `tag`
119+
120+
**type:** `string`
121+
122+
A region to filter the content with, for example `no-logging`.
123+
124+
This should be represented as starting `<docs-tag name="no-logging">` and closing `</docs-tag name="no-logging">` comments in the source file.

0 commit comments

Comments
 (0)