docs/integrations/twoslash.md
@unocss/twoslash provides twoslash integration for UnoCSS, annotating code blocks with generated CSS output, useful in documentation sites powered by VitePress.
import { defineConfig } from 'unocss'
export default defineConfig({
rules: [
['m-1', { margin: '1px' }],
],
})
npm add @unocss/twoslash
In your .vitepress/config.ts:
import { transformerTwoslash } from '@shikijs/vitepress-twoslash'
import { createTwoslasher } from '@unocss/twoslash'
import { defineConfig } from 'vitepress'
export default defineConfig({
markdown: {
codeTransformers: [
transformerTwoslash({
langs: ['vue', 'html'],
twoslasher: createTwoslasher(),
}),
],
},
})
Then use twoslash in your fenced code blocks:
<div class="p-4 text-red"></div>
configPathPath to your UnoCSS config file. If not provided, it will search up the directory tree automatically.
createTwoslasher({
configPath: './my-uno.config.ts',
})
preprocessCustom code transform before sending to UnoCSS for generation. This does not affect the rendered code.
createTwoslasher({
preprocess: code => code.replace(/\/\/.*$/gm, ''),
})