Skip to content

Commit b1bf129

Browse files
chore: add back global
1 parent 8498138 commit b1bf129

File tree

2 files changed

+27
-12
lines changed

2 files changed

+27
-12
lines changed

packages/mdx/src/plugins/rehype/rehypeSyntaxHighlighting.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import {
2323
getTwoslashOptions,
2424
parseLineComment,
2525
getCdnTwoslashTransformer,
26+
cdnTwoslash,
2627
} from './twoslash/config.js';
2728
import { getLanguage } from './utils.js';
2829

@@ -147,8 +148,10 @@ async function traverseNode({
147148

148149
code = splitCode.join('\n');
149150

151+
await cdnTwoslash.init();
152+
await cdnTwoslash.prepareTypes(code);
150153
const twoslashOptions = getTwoslashOptions({ linkMap });
151-
const cdnTwoslashTransformer = await getCdnTwoslashTransformer(code, twoslashOptions);
154+
const cdnTwoslashTransformer = getCdnTwoslashTransformer(twoslashOptions);
152155

153156
const hast = highlighter.codeToHast(code, {
154157
lang: lang ?? DEFAULT_LANG,

packages/mdx/src/plugins/rehype/twoslash/config.ts

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@ import {
55
} from '@shikijs/twoslash';
66
import type { ElementContent } from 'hast';
77
import type { ShikiTransformer } from 'shiki/types';
8-
import { type TwoslashInstance } from 'twoslash';
8+
import { createTwoslasher, type TwoslashInstance } from 'twoslash';
99
import { createTwoslashFromCDN, TwoslashCdnReturn } from 'twoslash-cdn';
1010
import * as ts from 'typescript';
1111

12+
type TransformerFactory = (options?: TransformerTwoslashOptions) => ShikiTransformer;
13+
1214
const twoslashCompilerOptions: ts.CompilerOptions = {
1315
target: ts.ScriptTarget.ESNext,
1416
lib: ['ESNext', 'DOM', 'esnext', 'dom', 'es2020'],
@@ -18,15 +20,29 @@ const twoslashCompilerOptions: ts.CompilerOptions = {
1820
alwaysStrict: false,
1921
};
2022

23+
const fsMap: Map<string, string> = new Map();
24+
const twoslashStorageMap = new Map();
25+
26+
export const cdnTwoslash = createTwoslashFromCDN({
27+
compilerOptions: twoslashCompilerOptions,
28+
fsMap,
29+
storage: {
30+
getItemRaw(key) {
31+
return twoslashStorageMap.get(key);
32+
},
33+
setItemRaw(key, value) {
34+
twoslashStorageMap.set(key, value);
35+
},
36+
},
37+
});
38+
2139
let cachedInstance: TwoslashCdnReturn | undefined;
2240

23-
export async function getCdnTwoslashTransformer(
24-
code: string,
25-
options: TransformerTwoslashOptions
26-
): Promise<ShikiTransformer> {
27-
const fsMap: Map<string, string> = new Map();
28-
const twoslashStorageMap = new Map();
41+
export const cdnTwoslashTransformer: TransformerFactory = createTransformerFactory(
42+
cdnTwoslash.runSync
43+
);
2944

45+
export function getCdnTwoslashTransformer(options: TransformerTwoslashOptions): ShikiTransformer {
3046
function getInstance() {
3147
cachedInstance ??= createTwoslashFromCDN({
3248
compilerOptions: twoslashCompilerOptions,
@@ -43,10 +59,6 @@ export async function getCdnTwoslashTransformer(
4359
return cachedInstance;
4460
}
4561

46-
const instance = getInstance();
47-
await instance.init();
48-
await instance.prepareTypes(code);
49-
5062
return createTransformerFactory(
5163
// lazy load Twoslash instance so it works on serverless platforms
5264
((...args) => getInstance().runSync(...args)) as TwoslashInstance

0 commit comments

Comments
 (0)