VitePress Twoslash
Static code examples for VitePress using Shiki Twoslash — powered by the syntax engine of Visual Studio Code and the TypeScript compiler.
Overview
Try moving your cursor into the code block below:
tsTry
// Removes 'readonly' attributes from a type's propertiestypeCreateMutable <Type > = {-readonly [Property in keyofType ]:Type [Property ]}typeLockedAccount = {readonlyid : stringreadonlyname : string}typeUnlockedAccount =CreateMutable <LockedAccount >
tsTry
// Removes 'readonly' attributes from a type's propertiestypeCreateMutable <Type > = {-readonly [Property in keyofType ]:Type [Property ]}typeLockedAccount = {readonlyid : stringreadonlyname : string}typeUnlockedAccount =CreateMutable <LockedAccount >
Pretty neat, right? To some extent, anything your editor can show you about code, Twoslash can show. For example, here is the real auto-complete for a VitePress config:
tsTry
import {defineConfig } from 'vitepress'export defaultdefineConfig ({ti })
tsTry
import {defineConfig } from 'vitepress'export defaultdefineConfig ({ti })
The name Twoslash refers to specially formatted comments (e.g. // ^?
) which can be used to set up your environment, like compiler flags or separate input files. It couldn't be easier to set up and start creating incredible code examples!
Install
Install vitepress-plugin-shiki-twoslash
(requires vitepress@>=1.0.0-alpha.61
).
pnpm add vitepress-plugin-shiki-twoslash
pnpm add vitepress-plugin-shiki-twoslash
npm i vitepress-plugin-shiki-twoslash
npm i vitepress-plugin-shiki-twoslash
yarn add vitepress-plugin-shiki-twoslash
yarn add vitepress-plugin-shiki-twoslash
WARNING
Until shiki-twoslash
uses the same version of shiki
as VitePress, you must override the following packages' shiki
versions for syntax highlighting to look the same.
{
"pnpm": {
"overrides": {
"remark-shiki-twoslash>shiki": "^0.14.1",
"shiki-twoslash>shiki": "^0.14.1"
}
}
}
{
"pnpm": {
"overrides": {
"remark-shiki-twoslash>shiki": "^0.14.1",
"shiki-twoslash>shiki": "^0.14.1"
}
}
}
Tracked in an upstream issue: https://github.com/shikijs/twoslash/issues/180
Configure
First, wrap your VitePress config file with the withTwoslash
wrapper.
tsTry
// .vitepress/config.[ext]import {defineConfig } from 'vitepress'import {withTwoslash } from 'vitepress-plugin-shiki-twoslash'export defaultwithTwoslash (defineConfig ({// Your VitePress config}),)
tsTry
// .vitepress/config.[ext]import {defineConfig } from 'vitepress'import {withTwoslash } from 'vitepress-plugin-shiki-twoslash'export defaultwithTwoslash (defineConfig ({// Your VitePress config}),)
Then, import vitepress-plugin-shiki-twoslash/styles.css
into your theme.
tsTry
// .vitepress/theme/index.tsimportdefaultTheme from 'vitepress/theme'import 'vitepress-plugin-shiki-twoslash/styles.css'export defaultdefaultTheme
tsTry
// .vitepress/theme/index.tsimportdefaultTheme from 'vitepress/theme'import 'vitepress-plugin-shiki-twoslash/styles.css'export defaultdefaultTheme
TIP
You can configure VitePress Twoslash using the twoslash
property added to defineConfig
.
Add Twoslash
Finally, add the twoslash
attribute to markdown fenced code blocks.
```ts twoslash
// Removes 'readonly' attributes from a type's properties
type CreateMutable<Type> = {
-readonly [Property in keyof Type]: Type[Property]
}
type LockedAccount = {
readonly id: string
readonly name: string
}
type UnlockedAccount = CreateMutable<LockedAccount>
// ^?
```
```ts twoslash
// Removes 'readonly' attributes from a type's properties
type CreateMutable<Type> = {
-readonly [Property in keyof Type]: Type[Property]
}
type LockedAccount = {
readonly id: string
readonly name: string
}
type UnlockedAccount = CreateMutable<LockedAccount>
// ^?
```
And your code blocks will be twoslashified!
tsTry
// Removes 'readonly' attributes from a type's propertiestypeCreateMutable <Type > = {-readonly [Property in keyofType ]:Type [Property ]}typeLockedAccount = {readonlyid : stringreadonlyname : string}typeUnlockedAccount =CreateMutable <LockedAccount >
tsTry
// Removes 'readonly' attributes from a type's propertiestypeCreateMutable <Type > = {-readonly [Property in keyofType ]:Type [Property ]}typeLockedAccount = {readonlyid : stringreadonlyname : string}typeUnlockedAccount =CreateMutable <LockedAccount >