Open tylersayshi opened 1 day ago
twoslash
meta string to codeblock. Like```ts twoslash
console.log("I love Arktype")
2. It requires a Popup component to display the generated popups, you can import them from the package:
```ts
import { Popup, PopupContent, PopupTrigger } from 'fumadocs-twoslash/ui';
And pass it to components option.
I would suggest to provide the details of runtime error too if you have other questions, it helps me to locate the problem faster.
Sorry for late, I've just made the Twoslash section docs up-to-date.
Hope it clarifies the problems: https://fumadocs.vercel.app/docs/ui/twoslash
Thanks for the quick responses!
Is the code for customizing the config file referring to the next.config.js
?
from the docs:
import { defineConfig } from 'fumadocs-mdx/config';
import { transformerTwoslash } from 'fumadocs-twoslash';
import { rehypeCodeDefaultOptions } from 'fumadocs-core/mdx-plugins';
export default defineConfig({
mdxOptions: {
rehypeCodeOptions: {
transformers: [
...(rehypeCodeDefaultOptions.transformers ?? []),
transformerTwoslash(),
],
},
},
});
I am probably just configuring this wrong somehow.
Oh it's the source.config.ts
- sorry for my confusion there. #1 is now resolved for me and was not a bug at all. 🎉
re: the runtime error.
minimal reproduction: https://github.com/tylersayshi/fumadocs-twoslash-bug
link to related code block component
This error I find confusion because it mentions that the error happens on the server where path
should definitely be defined.
I checked and this happens on node 20, 22, and 23
I get the same error when doing this with
import { transformerTwoslash } from "@shikijs/twoslash";
So maybe this is upstream somehow?
took some time for me, it's a bundler issue because typescript
has dynamic require of modules including fs
and path
.
You can add them to serverExternalPackages
.
import * as Base from "fumadocs-ui/components/codeblock";
import { highlight } from "fumadocs-core/server";
import { transformerTwoslash } from "fumadocs-twoslash";
import { Popup, PopupContent, PopupTrigger } from "fumadocs-twoslash/ui";
export interface CodeBlockProps {
code: string;
wrapper?: Base.CodeBlockProps;
lang: string;
}
export async function CodeBlock({
code,
lang,
wrapper,
}: CodeBlockProps): Promise<React.ReactElement> {
const rendered = await highlight(code, {
lang,
meta: {
__raw: 'twoslash'
},
themes: {
light: "github-light",
dark: "vesper",
},
transformers: [transformerTwoslash()],
components: {
// @ts-expect-error -- JSX component
pre: Base.Pre,
Popup,
PopupContent,
PopupTrigger
},
});
return <Base.CodeBlock {...wrapper}>{rendered}</Base.CodeBlock>;
}
import { createMDX } from "fumadocs-mdx/next";
const withMDX = createMDX();
/** @type {import('next').NextConfig} */
const config = {
reactStrictMode: true,
serverExternalPackages: ['twoslash', 'typescript'],
};
export default withMDX(config);
and install them to your project: pnpm i twoslash
(you already have typescript
installed)
wow! Thanks so much for the help! It all works super nicely now :)
Oh wait one last question, it looks like the twoslash snippets are parsing strangely. Is the meta.__raw: 'twoslash'
supposed to fix this?
You probably forgot to add components option to JSX renderer or MDX
component, the fumadocs-twoslash/ui
exported all required components.
The purpose of meta is to trigger twoslash, without it twoslash won't be enabled
So the weird output above was because I was importing from @shikijs/twoslash
for transformerTwoslash
still by accident.
I am now seeing this error 😅
Error: Language `js` not found, you may need to load it first
this issue seems relevant but maybe different? https://github.com/vuejs/vitepress/issues/4334
the popups are added here btw: https://github.com/tylersayshi/fuma-move/blob/main/components/code-snippets/Code.tsx#L27-L32
Maybe I need to mark another package as external? https://github.com/tylersayshi/fuma-move/blob/main/next.config.js#L8
Yes it's relevant, codeblocks in popups may have a different language than the parent codeblock, but only the language of parent codeblock e.g. ts
will be pre-loaded.
It's indeed a problem of Shiki, it doesn't support async markdown renderer so there's no official solution unless I make a PR to shiki.
for user-land workaround, you can pre-load all possible languages first (ideally bundledLanguages
).
import {
getSingletonHighlighter,
bundledLanguages
} from 'shiki';
// before highlight call
await getSingletonHighlighter({
langs: Object.keys(bundledLanguages)
})
Or you can just drop the highlight
function and go with Shiki + your custom config, so you can have full control on this.
To Reproduce
Issue Reproduction: https://github.com/tylersayshi/fumadocs-twoslash-bug
Current vs. Expected behavior
Bugs:
// ^?
and other twoslash comments are not being transformed on the docs pages after adding the plugin following the docs: https://fumadocs.vercel.app/docs/ui/twoslashtwoslash
on the server incomponents/code-block.tsx
for the home pageProvide environment information
Which area(s) are affected? (Select all that apply)
Content Sources (e.g. Fumadocs MDX, Content Collections), Integrations (e.g OpenAPI, Typescript DocsGen)
Additional context
the project i am working on this for is here: https://github.com/tylersayshi/fuma-move
This work is to move arktype.io to fumadocs: https://github.com/arktypeio/arktype/issues/1111