Fumadocs

Twoslash

在你的文档中使用 Typescript Twoslash

用法

感谢 Shiki 的 Twoslash 集成,默认代码语法高亮器,只需添加一个转换器即可。

npm install fumadocs-twoslash twoslash

对于 Next.js,你需要外部化以下依赖:

next.config.mjs (Next.js)
const config = {
  reactStrictMode: true,
  serverExternalPackages: ['typescript', 'twoslash'],
};

添加到你的 Shiki 转换器中。

source.config.ts (Fumadocs MDX)
import {  } from 'fumadocs-mdx/config';
import {  } from 'fumadocs-twoslash';
import {  } from 'fumadocs-core/mdx-plugins';

export default ({
  : {
    : {
      : {
        : 'github-light',
        : 'github-dark',
      },
      : [
        ...(. ?? []),
        (),
      ],
    },
  },
});

添加样式,需要 Tailwind CSS v4。

Tailwind CSS
@import 'fumadocs-twoslash/twoslash.css';

添加 MDX 组件。

mdx-components.tsx
import * as Twoslash from 'fumadocs-twoslash/ui';
import defaultComponents from 'fumadocs-ui/mdx';
import type { MDXComponents } from 'mdx/types';

export function getMDXComponents(components?: MDXComponents): MDXComponents {
  return {
    ...defaultComponents,
    ...Twoslash,
    ...components,
  };
}

现在,你可以向代码块添加 twoslash 元字符串。

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

示例

了解更多关于 Twoslash notations

Test
.g
  • group
  • groupCollapsed
  • groupEnd
;
const player: = { : 'Hello World' };
const player: Player
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.

缓存

你可以使用 typesCache 选项启用文件系统缓存:

source.config.ts
import {  } from 'fumadocs-twoslash';
import {  } from 'fumadocs-twoslash/cache-fs';

({
  : (),
});

How is this guide?

Last updated on

On this page