Skip to content

Commit 3630452

Browse files
committed
[Docs Site] Add useTypeScriptExample prop to GitHubCode
1 parent 3087334 commit 3630452

File tree

3 files changed

+50
-18
lines changed

3 files changed

+50
-18
lines changed

src/components/GitHubCode.astro

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ const props = z
1717
.transform((val) => val.split("-").map(Number))
1818
.optional(),
1919
tag: z.string().optional(),
20+
useTypeScriptExample: z.boolean().default(false),
2021
code: z.custom<ComponentProps<typeof Code>>().optional(),
2122
})
2223
.strict()
@@ -25,9 +26,19 @@ const props = z
2526
return !(val.lines && val.tag);
2627
},
2728
{ message: "Lines and tag are mutually exclusive filters." },
29+
)
30+
.refine(
31+
(val) => {
32+
return val.useTypeScriptExample && val.lang === "ts";
33+
},
34+
{
35+
message:
36+
"useTypeScriptExample can only be used when 'lang' is set to 'ts'",
37+
},
2838
);
2939
30-
const { repo, commit, file, lang, lines, tag, code } = props.parse(Astro.props);
40+
const { repo, commit, file, lang, lines, tag, useTypeScriptExample, code } =
41+
props.parse(Astro.props);
3142
3243
const res = await fetch(
3344
`https://gh-code.developers.cloudflare.com/${repo}/${commit}/${file}`,
@@ -69,9 +80,9 @@ contentLines = contentLines.filter(
6980
(line) => !/<[/]?docs-tag name=".*">/.test(line),
7081
);
7182
72-
const Wrapper = lang === "ts" ? TypeScriptExample : Fragment;
83+
const Wrapper = useTypeScriptExample ? TypeScriptExample : Fragment;
7384
---
7485

75-
<Wrapper>
86+
<Wrapper code={code}>
7687
<Code {...code} code={contentLines.join("\n")} lang={lang} />
7788
</Wrapper>

src/content/docs/style-guide/components/github-code.mdx

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,25 +16,15 @@ import { GitHubCode } from "~/components";
1616

1717
## Usage
1818

19-
<GitHubCode
20-
repo="cloudflare/workflows-starter"
21-
file="src/index.ts"
22-
commit="a844e629ec80968118d4b116d4b26f5dcb107137"
23-
lang="ts"
24-
code={{
25-
collapse: "3-6"
26-
}}
27-
/>
19+
```mdx live
20+
import { GitHubCode } from "~/components";
2821

29-
```mdx
3022
<GitHubCode
3123
repo="cloudflare/workflows-starter"
3224
file="src/index.ts"
3325
commit="a844e629ec80968118d4b116d4b26f5dcb107137"
3426
lang="ts"
35-
code={{
36-
collapse: "2-3"
37-
}}
27+
useTypeScriptExample={true}
3828
/>
3929
```
4030

@@ -122,7 +112,12 @@ The long (40-characters) Git commit hash to pull from, for example `ab3951b5c953
122112

123113
The language to use for the code block, for example `rs`.
124114

125-
If the `lang` is `ts`, the [`TypeScriptExample`](/style-guide/components/typescript-example/) component will be used to provide a JavaScript tab as well.
115+
### `useTypeScriptExample`
116+
117+
**type:** `boolean`
118+
119+
120+
If the `lang` is `"ts"` and `useTypeScriptExample` is `true`, the [`TypeScriptExample`](/style-guide/components/typescript-example/) component will be used to provide a JavaScript tab as well.
126121

127122
### `lines`
128123

src/content/docs/style-guide/components/typescript-example.mdx

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,39 @@ Some TypeScript syntax influences runtime behaviour, and cannot be stripped.
1616
Please refer to the [Unsupported Syntax](https://github.com/bloomberg/ts-blank-space?tab=readme-ov-file#unsupported-syntax) section of the project's README.
1717
:::
1818

19+
:::caution
20+
Code blocks are typically configured with options on the opening fence, like so:
21+
22+
````mdx
23+
```ts collapse={1-2}
24+
// ...
25+
```
26+
````
27+
28+
These cannot be extracted by `TypeScriptExample` so they must be moved to the [`code`](#code) prop.
29+
30+
````mdx
31+
<TypeScriptExample code={{
32+
collapse: "1-2"
33+
}}>
34+
```ts
35+
// ...
36+
```
37+
</TypeScriptExample>
38+
````
39+
:::
40+
1941
## Component
2042

2143
````mdx live
2244
import { TypeScriptExample } from "~/components";
2345

24-
<TypeScriptExample>
46+
<TypeScriptExample code={{
47+
collapse: "1-2"
48+
}}>
2549
```ts
50+
// comment to demonstrate
51+
// collapsible sections
2652
interface Environment {
2753
KV: KVNamespace;
2854
}

0 commit comments

Comments
 (0)