Skip to content

Commit 8498138

Browse files
Remove globals
1 parent 3e8d2b2 commit 8498138

File tree

2 files changed

+13
-27
lines changed

2 files changed

+13
-27
lines changed

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import {
2323
getTwoslashOptions,
2424
parseLineComment,
2525
getCdnTwoslashTransformer,
26-
cdnTwoslash,
2726
} from './twoslash/config.js';
2827
import { getLanguage } from './utils.js';
2928

@@ -148,9 +147,8 @@ async function traverseNode({
148147

149148
code = splitCode.join('\n');
150149

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

155153
const hast = highlighter.codeToHast(code, {
156154
lang: lang ?? DEFAULT_LANG,
@@ -165,7 +163,7 @@ async function traverseNode({
165163
colorReplacements: shikiColorReplacements,
166164
tabindex: false,
167165
tokenizeMaxLineLength: 1000,
168-
transformers: [...SHIKI_TRANSFORMERS, getCdnTwoslashTransformer(twoslashOptions)],
166+
transformers: [...SHIKI_TRANSFORMERS, cdnTwoslashTransformer],
169167
});
170168

171169
const codeElement = hast.children[0] as Element;

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

Lines changed: 11 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,10 @@ import {
55
} from '@shikijs/twoslash';
66
import type { ElementContent } from 'hast';
77
import type { ShikiTransformer } from 'shiki/types';
8-
import { createTwoslasher, type TwoslashInstance } from 'twoslash';
8+
import { 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-
1412
const twoslashCompilerOptions: ts.CompilerOptions = {
1513
target: ts.ScriptTarget.ESNext,
1614
lib: ['ESNext', 'DOM', 'esnext', 'dom', 'es2020'],
@@ -20,29 +18,15 @@ const twoslashCompilerOptions: ts.CompilerOptions = {
2018
alwaysStrict: false,
2119
};
2220

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-
3921
let cachedInstance: TwoslashCdnReturn | undefined;
4022

41-
export const cdnTwoslashTransformer: TransformerFactory = createTransformerFactory(
42-
cdnTwoslash.runSync
43-
);
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();
4429

45-
export function getCdnTwoslashTransformer(options: TransformerTwoslashOptions): ShikiTransformer {
4630
function getInstance() {
4731
cachedInstance ??= createTwoslashFromCDN({
4832
compilerOptions: twoslashCompilerOptions,
@@ -59,6 +43,10 @@ export function getCdnTwoslashTransformer(options: TransformerTwoslashOptions):
5943
return cachedInstance;
6044
}
6145

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

0 commit comments

Comments
 (0)