Skip to content

Commit 69db993

Browse files
feat(docs): add multi-file code samples documentation (#93)
* chore(deps): update pnpm workspace dependencies to ^0.41.7 * feat(docs): add multi-file code samples documentation and improve code block examples --------- Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
1 parent b258de3 commit 69db993

5 files changed

Lines changed: 101 additions & 16 deletions

File tree

docs/astro.config.mts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,10 @@ export default defineConfig({
187187
label: "Code Cutting",
188188
link: "usage/code-cutting",
189189
},
190+
{
191+
label: "Multi-file Code Samples",
192+
link: "usage/multi-file",
193+
},
190194
{
191195
label: "Overriding Compiler Options",
192196
link: "usage/ts-compiler-flags",
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
---
2+
title: Multi-file code samples
3+
sidebar:
4+
order: 2
5+
---
6+
7+
import { TabItem, Tabs } from "@astrojs/starlight/components";
8+
9+
Twoslash supports multi-file code samples. This is done by using the `@filename` directive which tells Twoslash to create a new file with the given name and put the following code in that file until the next `@filename` directive or the end of the code sample.
10+
11+
## Simple multi-file code sample
12+
13+
<Tabs>
14+
<TabItem label="Rendered Output">
15+
16+
```ts twoslash
17+
// @filename: a.ts
18+
export const helloWorld: string = 'Hi';
19+
20+
// @filename: b.ts
21+
import { helloWorld } from './a';
22+
23+
console.log(helloWorld);
24+
```
25+
26+
</TabItem>
27+
<TabItem label="Markdown">
28+
29+
``````md
30+
```ts twoslash
31+
// @filename: a.ts
32+
export const helloWorld: string = 'Hi';
33+
34+
// @filename: b.ts
35+
import { helloWorld } from './a';
36+
37+
console.log(helloWorld);
38+
```
39+
``````
40+
41+
</TabItem>
42+
</Tabs>
43+
44+
## Multi-file code sample with hidden files
45+
46+
Taking the above example, we can hide the contents of `a.ts` by using the `---cut---` directive. This is useful for showing only the relevant code to the reader while still having the full code sample available for Twoslash to compile and run.
47+
48+
<Tabs>
49+
<TabItem label="Rendered Output">
50+
51+
```ts twoslash
52+
// @filename: a.ts
53+
export const helloWorld: string = 'Hi'
54+
// ---cut---
55+
// @filename: b.ts
56+
import { helloWorld } from './a'
57+
58+
console.log(helloWorld)
59+
```
60+
61+
</TabItem>
62+
<TabItem label="Markdown">
63+
64+
``````md
65+
```ts twoslash
66+
// @filename: a.ts
67+
export const helloWorld: string = 'Hi'
68+
// ---cut---
69+
// @filename: b.ts
70+
import { helloWorld } from './a'
71+
72+
console.log(helloWorld)
73+
```
74+
``````
75+
76+
</TabItem>
77+
</Tabs>

docs/src/content/docs/usage/show-emitted-files.mdx

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ While the `.js` file is probably the most useful file out of the box, TypeScript
4343
<Tabs>
4444
<TabItem label="Rendered Output">
4545

46-
```ts twoslash title="index.d.ts"
46+
```ts twoslash
4747
// @declaration
4848
// @showEmit
4949
// @showEmittedFile: index.d.ts
@@ -54,7 +54,7 @@ export const hello = 'world'
5454
<TabItem label="Markdown">
5555

5656
``````md
57-
```ts twoslash title="index.d.ts"
57+
```ts twoslash
5858
// @declaration
5959
// @showEmit
6060
// @showEmittedFile: index.d.ts
@@ -70,7 +70,7 @@ export const hello = 'world'
7070
<Tabs>
7171
<TabItem label="Rendered Output">
7272

73-
```ts twoslash title="index.js.map"
73+
```ts twoslash
7474
// @sourceMap
7575
// @showEmit
7676
// @showEmittedFile: index.js.map
@@ -81,7 +81,7 @@ export const hello = 'world'
8181
<TabItem label="Markdown">
8282

8383
``````md
84-
```ts twoslash title="index.js.map"
84+
```ts twoslash
8585
// @sourceMap
8686
// @showEmit
8787
// @showEmittedFile: index.js.map
@@ -97,7 +97,7 @@ export const hello = 'world'
9797
<Tabs>
9898
<TabItem label="Rendered Output">
9999

100-
```ts twoslash title="index.d.ts.map"
100+
```ts twoslash
101101
// @declaration
102102
// @declarationMap
103103
// @showEmit
@@ -109,7 +109,7 @@ export const hello: string = 'world'
109109
<TabItem label="Markdown">
110110

111111
``````md
112-
```ts twoslash title="index.d.ts.map"
112+
```ts twoslash
113113
// @declaration
114114
// @declarationMap
115115
// @showEmit
@@ -123,19 +123,23 @@ export const hello: string = 'world'
123123

124124
### Multi-file code samples
125125

126+
<Aside variant="warning">
127+
When using `@showEmittedFile` in a multi-file code sample, you need to make sure that you also add a title to your code block. This is due to a limitation in the way Twoslash handles multi-file code samples. Without a title, the emitted code will be incorrectly displayed.
128+
</Aside>
129+
126130
<Tabs>
127131
<TabItem label="Rendered Output">
128132

129133
```ts twoslash title="b.js"
130134
// @showEmit
131135
// @showEmittedFile: b.js
132136
// @filename: a.ts
133-
export const helloWorld: string = 'Hi';
137+
export const helloWorld: string = 'Hi'
134138

135139
// @filename: b.ts
136-
import { helloWorld } from './a';
140+
import { helloWorld } from './a.js'
137141

138-
console.log(helloWorld);
142+
console.log(helloWorld)
139143
```
140144

141145
</TabItem>
@@ -146,12 +150,12 @@ console.log(helloWorld);
146150
// @showEmit
147151
// @showEmittedFile: b.js
148152
// @filename: a.ts
149-
export const helloWorld: string = 'Hi';
153+
export const helloWorld: string = 'Hi'
150154

151155
// @filename: b.ts
152-
import { helloWorld } from './a';
156+
import { helloWorld } from './a.js'
153157

154-
console.log(helloWorld);
158+
console.log(helloWorld)
155159
```
156160
``````
157161

pnpm-lock.yaml

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pnpm-workspace.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ catalogs:
1515
mdast-util-gfm: ^3.1.0
1616
mdast-util-to-hast: ^13.2.1
1717
min:
18-
"@expressive-code/core": ^0.41.6
19-
expressive-code: ^0.41.6
18+
"@expressive-code/core": ^0.41.7
19+
expressive-code: ^0.41.7
2020
typescript: ^5.5.0
2121
twoslash:
2222
"@vue/language-core": ^3.2.5

0 commit comments

Comments
 (0)