Skip to content

Commit db35877

Browse files
zhiyuanzmjsxzz
andauthored
feat(docs): support twoslash (#706)
Co-authored-by: 三咲智子 Kevin Deng <sxzz@sxzz.moe>
1 parent 1e2be27 commit db35877

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+1089
-350
lines changed

docs/.vitepress/config.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,26 @@
1+
import { createRequire } from 'node:module'
12
import { defineConfig } from 'vitepress'
23
import { withPwa } from '@vite-pwa/vitepress'
4+
import { transformerTwoslash } from '@shikijs/vitepress-twoslash'
5+
import ts from 'typescript'
36
import { docsLink } from '../../macros'
47
import { getLocaleConfig, pwa } from './configs'
58

9+
const require = createRequire(import.meta.url)
10+
11+
// Volar plugins
12+
const defineOptions = require('@vue-macros/volar/define-options')
13+
const defineEmit = require('@vue-macros/volar/define-emit')
14+
const defineProp = require('@vue-macros/volar/define-prop')
15+
const defineProps = require('@vue-macros/volar/define-props')
16+
const definePropsRefs = require('@vue-macros/volar/define-props-refs')
17+
const defineSlots = require('@vue-macros/volar/define-slots')
18+
const defineModels = require('@vue-macros/volar/define-models')
19+
const exportExpose = require('@vue-macros/volar/export-expose')
20+
const exportRender = require('@vue-macros/volar/export-render')
21+
const jsxDirective = require('@vue-macros/volar/jsx-directive')
22+
const booleanProp = require('@vue-macros/volar/boolean-prop')
23+
624
export default withPwa(
725
defineConfig({
826
lastUpdated: true,
@@ -40,5 +58,33 @@ export default withPwa(
4058
hostname: docsLink,
4159
},
4260
pwa,
61+
markdown: {
62+
codeTransformers: [
63+
transformerTwoslash({
64+
twoslashOptions: {
65+
compilerOptions: {
66+
jsx: ts.JsxEmit.Preserve,
67+
jsxFactory: 'vue',
68+
types: ['unplugin-vue-macros/macros-global', 'vue/jsx'],
69+
},
70+
vueCompilerOptions: {
71+
plugins: [
72+
defineOptions,
73+
defineModels,
74+
defineSlots,
75+
defineEmit,
76+
defineProp,
77+
defineProps,
78+
definePropsRefs,
79+
exportRender,
80+
exportExpose,
81+
jsxDirective,
82+
booleanProp,
83+
],
84+
},
85+
},
86+
}),
87+
],
88+
},
4389
}),
4490
)

docs/.vitepress/theme/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
import Theme from 'vitepress/theme'
22
import { NolebaseGitChangelogPlugin } from '@nolebase/vitepress-plugin-git-changelog/client'
3+
import TwoslashFloatingVue from '@shikijs/vitepress-twoslash/client'
34
import WarnBadge from '../components/WarnBadge.vue'
45
import StabilityLevel from '../components/StabilityLevel.vue'
56
import Layout from './Layout.vue'
67
import type { EnhanceAppContext } from 'vitepress'
78
import 'uno.css'
89
import './style.css'
910
import '@nolebase/vitepress-plugin-git-changelog/client/style.css'
11+
import '@shikijs/vitepress-twoslash/style.css'
1012

1113
export default {
1214
...Theme,
@@ -26,5 +28,6 @@ export default {
2628
},
2729
],
2830
})
31+
app.use(TwoslashFloatingVue)
2932
},
3033
}

docs/features/better-define.md

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,27 @@ With enabling `betterDefine`, imported types are supported in `<script setup>` t
1717

1818
::: code-group
1919

20-
```vue [App.vue]
20+
```vue twoslash [App.vue]
2121
<script setup lang="ts">
22+
// #region basic
23+
type BaseProps = {
24+
title: string
25+
}
26+
27+
export type { BaseProps }
28+
// #endregion basic
29+
// ---cut---
30+
// @noErrors
2231
import type { BaseProps } from './types'
2332
2433
interface Props extends BaseProps {
2534
foo: string
2635
}
27-
defineProps<Props>()
36+
const props = defineProps<Props>()
2837
</script>
2938
```
3039

31-
```ts [types.ts]
32-
export interface BaseProps {
33-
title: string
34-
}
35-
```
40+
<<< ./better-define.md#basic{ts} [types.ts]
3641

3742
:::
3843

docs/features/boolean-prop.md

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,18 +26,35 @@ interface Options {
2626

2727
## Usage
2828

29-
```vue
29+
<!-- prettier-ignore-start -->
30+
```vue twoslash
31+
<script setup>
32+
import type { FunctionalComponent } from 'vue'
33+
34+
export const Comp: FunctionalComponent<
35+
{
36+
checked: true,
37+
enabled: false,
38+
},
39+
> = () => null
40+
// ---cut---
41+
// @noErrors
42+
import Comp from './Comp.vue'
43+
</script>
44+
3045
<template>
3146
<Comp checked !enabled />
47+
// ^?
3248
</template>
3349
```
50+
<!-- prettier-ignore-end -->
3451

35-
```vue
52+
```vue twoslash
3653
<script setup lang="ts">
3754
// Comp.vue
3855
defineProps<{
39-
checked?: any
40-
enabled: boolean
56+
checked: true
57+
enabled: false
4158
}>()
4259
</script>
4360
```

docs/features/export-expose.md

Lines changed: 49 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,13 @@ Support these syntaxes:
2323

2424
### 1. local variable/function/class
2525

26-
```vue
26+
```vue twoslash
2727
<script setup lang="ts">
2828
export const foo: string = 'foo',
2929
bar = 10
3030
export let baz: string | undefined
3131
export var qux = fn()
32+
// @errors: 2448 2454 2695
3233
export const { a, b, c } = { a: 1, b: 2, c: 3 }
3334
3435
export function fn() {}
@@ -38,8 +39,8 @@ export class A {}
3839

3940
::: details Compiled Code
4041

41-
```vue
42-
<script lang="ts">
42+
```vue twoslash
43+
<script setup lang="ts">
4344
const foo: string = 'foo',
4445
bar = 10
4546
let baz: string | undefined
@@ -67,16 +68,20 @@ defineExpose({
6768

6869
### 2. export with alias
6970

70-
```vue
71+
```vue twoslash
7172
<script setup lang="ts">
73+
const foo = ''
74+
7275
export { foo as foo1 }
7376
</script>
7477
```
7578

7679
::: details Compiled Code
7780

78-
```vue
81+
```vue twoslash
7982
<script setup lang="ts">
83+
const foo = 'foo'
84+
8085
defineExpose({
8186
foo1: foo,
8287
})
@@ -87,16 +92,35 @@ defineExpose({
8792

8893
### 3. export from other file
8994

90-
```vue
95+
::: code-group
96+
97+
```vue [App.vue] twoslash
9198
<script setup lang="ts">
99+
// #region export-file
100+
const foo = 'foo'
101+
type Foo = string
102+
103+
export { type Foo, foo }
104+
// #endregion export-file
105+
// ---cut---
106+
// @noErrors
92107
export { foo, type Foo, foo as bar } from './types'
93108
</script>
94109
```
95110

111+
<<< ./export-expose.md#export-file{ts} [types.ts]
112+
113+
:::
114+
96115
::: details Compiled Code
97116

98-
```vue
117+
```vue twoslash
99118
<script setup lang="ts">
119+
const __MACROS_expose_0 = 'foo'
120+
const __MACROS_expose_1 = 'foo'
121+
type Foo = string
122+
// ---cut---
123+
// @noErrors
100124
import {
101125
type Foo,
102126
foo as __MACROS_expose_0,
@@ -113,16 +137,32 @@ defineExpose({
113137

114138
### 4. namespace export
115139

116-
```vue
140+
::: code-group
141+
142+
```vue [App.vue] twoslash
117143
<script setup lang="ts">
144+
const foo = { foo: 'foo' }
145+
// ---cut---
146+
// @noErrors
118147
export * as foo from './types'
119148
</script>
120149
```
121150

151+
```ts [types.ts]
152+
export const foo = 'foo'
153+
```
154+
155+
:::
156+
122157
::: details Compiled Code
123158

124-
```vue
159+
```vue twoslash
125160
<script setup lang="ts">
161+
const __MACROS_expose_0 = {
162+
foo: 'foo',
163+
}
164+
// ---cut---
165+
// @noErrors
126166
import * as __MACROS_expose_0 from './types'
127167
defineExpose({
128168
foo: __MACROS_expose_0,

docs/features/export-props.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
Using export syntax to declare props.
1717

18-
```vue
18+
```vue twoslash
1919
<script setup lang="ts">
2020
export let foo: string
2121
export const bar: number = 1 // with default value

docs/features/hoist-static.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ For Vue >= 3.3, this feature will be turned off by default.
1414

1515
## Basic Usage
1616

17-
```vue
17+
```vue twoslash
1818
<script setup lang="ts">
1919
const name = 'AppFoo'
2020
defineOptions({
@@ -38,8 +38,10 @@ export default {
3838

3939
## Magic Comments
4040

41-
```vue
41+
```vue twoslash
4242
<script setup lang="ts">
43+
const fn = () => 'AppFoo'
44+
4345
// A value that's even not a constant
4446
const name = /* hoist-static */ fn()
4547
defineOptions({

0 commit comments

Comments
 (0)