Fumadocs
Integrations

Twoslash

Use Typescript Twoslash in your docs

Usage

Thanks to the Twoslash integration of Shiki, the default code syntax highlighter, it is as simple as adding a transformer.

npm install fumadocs-twoslash

Add to your Shiki transformers.

Example for Fumadocs MDX

source.config.ts
import {  } from 'fumadocs-mdx/config';
import {  } from 'fumadocs-twoslash';
import {  } from 'fumadocs-core/mdx-plugins';
 
export default ({
  : {
    : {
      : [
        ...(. ?? []),
        (),
      ],
    },
  },
});

Add required styles and components, note that you have to configure Tailwind CSS and the official Tailwind CSS plugin first.

layout.tsx
import 'fumadocs-twoslash/twoslash.css';
page.tsx
import { Popup, PopupContent, PopupTrigger } from 'fumadocs-twoslash/ui';
import defaultMdxComponents from 'fumadocs-ui/mdx';
 
<MDX
  components={{
    ...defaultMdxComponents,
    Popup,
    PopupContent,
    PopupTrigger,
  }}
/>;

Now you can add twoslash meta string to codeblocks.

```ts twoslash
console.log('Hello World');
```

Example

Learn more about Twoslash notations.

Test
.g
  • group
  • groupCollapsed
  • groupEnd
;
const
const player: Player
player
: = { : 'Hello World' };
const  = '123';
 
.();
 
import {  } from 'fumadocs-openapi';
 
void ({
  : ['./museum.yaml'],
  : './content/docs/ui',
});
const  = '123';
 
a = 132;
Cannot assign to 'a' because it is a constant.

Using on Runtime

To use it on Next.js runtime, add typescript and twoslash to serverExternalPackages:

next.config.mjs
import { createMDX } from 'fumadocs-mdx/next';
 
const withMDX = createMDX();
 
/** @type {import('next').NextConfig} */
const config = {
  reactStrictMode: true,
  serverExternalPackages: ['twoslash', 'typescript'],
};
 
export default withMDX(config);

Install the external dependencies:

npm install typescript twoslash

Pass the transformer to Shiki, see https://shiki.style/packages/next for details.

import React from 'react';
import { ,  } from 'fumadocs-ui/components/codeblock';
import {  } from 'fumadocs-core/server';
import {  } from 'fumadocs-twoslash';
import { , ,  } from 'fumadocs-twoslash/ui';
import { ,  } from 'shiki';
import {  } from 'hast-util-to-jsx-runtime';
import { , ,  } from 'react/jsx-runtime';
 
export async function ({
  ,
   = 'ts',
}: {
  ?: string;
  : string;
}) {
  const  = await ({
    : .(),
    : ['vesper'],
  });
 
  const  = .(, {
    ,
    : {
      : 'vesper',
      : 'vesper',
    },
    : false,
    : [
      (),
      ({
        : false,
      }),
    ],
  });
 
  const  = (, {
    ,
    ,
    ,
    : false,
    : {
      : () => (
        < {...}>
          <>{.children}</>
        </>
      ),
      ,
      ,
      ,
    },
  });
 
  return ;
}
Edit on GitHub

Last updated on

On this page