@shikijs/vitepress-twoslash 3.11.0 → 3.12.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cache-fs.d.mts +1 -5
- package/dist/cache-inline/index.d.mts +48 -0
- package/dist/cache-inline/index.mjs +341 -0
- package/dist/client.mjs +8 -3
- package/dist/index.d.mts +2 -3
- package/dist/index.mjs +4 -17
- package/dist/shared/{vitepress-twoslash.D_PyQzvF.d.mts → vitepress-twoslash.CBxCru0j.d.mts} +1 -37
- package/package.json +8 -3
package/dist/cache-fs.d.mts
CHANGED
|
@@ -1,8 +1,4 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import '@shikijs/twoslash/core';
|
|
3
|
-
import 'twoslash';
|
|
4
|
-
import 'twoslash-vue';
|
|
5
|
-
import '@shikijs/twoslash';
|
|
1
|
+
import { TwoslashTypesCache } from '@shikijs/twoslash';
|
|
6
2
|
|
|
7
3
|
interface FileSystemTypeResultCacheOptions {
|
|
8
4
|
/**
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { UserConfig } from 'vitepress';
|
|
2
|
+
import { V as VitePressPluginTwoslashOptions } from '../shared/vitepress-twoslash.CBxCru0j.mjs';
|
|
3
|
+
import '@shikijs/twoslash/core';
|
|
4
|
+
import 'twoslash-vue';
|
|
5
|
+
import '@shikijs/twoslash';
|
|
6
|
+
|
|
7
|
+
interface MarkdownFenceSourceMap {
|
|
8
|
+
path: string;
|
|
9
|
+
from: number;
|
|
10
|
+
to: number;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Maps markdown code to its source map positions.
|
|
14
|
+
* @param code markdown source
|
|
15
|
+
* @param path markdown file path
|
|
16
|
+
* @returns map of inject positions to source maps
|
|
17
|
+
*/
|
|
18
|
+
type MarkdownFencesSourceMapper = (code: string, path: string) => Map<number, MarkdownFenceSourceMap>;
|
|
19
|
+
interface MarkdownFenceSourceMapCodec {
|
|
20
|
+
/**
|
|
21
|
+
* Injects source map data into the markdown source.
|
|
22
|
+
* @param code markdown source
|
|
23
|
+
* @param injects map of inject positions to source maps
|
|
24
|
+
* @returns markdown source with injected source maps
|
|
25
|
+
*/
|
|
26
|
+
injectToMarkdown: (code: string, path: string) => string;
|
|
27
|
+
/**
|
|
28
|
+
* Extract source map from fence code snippet.
|
|
29
|
+
* @param code fence code snippet
|
|
30
|
+
* @returns extracted code and source map
|
|
31
|
+
*/
|
|
32
|
+
extractFromFence: (code: string) => {
|
|
33
|
+
code: string;
|
|
34
|
+
sourceMap: MarkdownFenceSourceMap | null;
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
interface TwoslashInlineCacheOptions {
|
|
39
|
+
sourceMapper?: MarkdownFencesSourceMapper;
|
|
40
|
+
sourceMapCodec?: MarkdownFenceSourceMapCodec;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* @experimental This API is experimental and may be changed in the future.
|
|
44
|
+
*/
|
|
45
|
+
declare function createTwoslashWithInlineCache(twoslashOptions?: VitePressPluginTwoslashOptions, { sourceMapper, sourceMapCodec, }?: TwoslashInlineCacheOptions): (config: UserConfig) => UserConfig;
|
|
46
|
+
|
|
47
|
+
export { createTwoslashWithInlineCache };
|
|
48
|
+
export type { TwoslashInlineCacheOptions };
|
|
@@ -0,0 +1,341 @@
|
|
|
1
|
+
import { existsSync, readFileSync, writeFileSync } from 'node:fs';
|
|
2
|
+
import { transformerTwoslash } from '../index.mjs';
|
|
3
|
+
import { createHash } from 'node:crypto';
|
|
4
|
+
import LZString from 'lz-string';
|
|
5
|
+
import { hash } from 'ohash';
|
|
6
|
+
import MagicString from 'magic-string';
|
|
7
|
+
import process from 'node:process';
|
|
8
|
+
import MarkdownIt from 'markdown-it';
|
|
9
|
+
import '@shikijs/twoslash/core';
|
|
10
|
+
import 'twoslash';
|
|
11
|
+
import 'twoslash-vue';
|
|
12
|
+
import '@shikijs/twoslash';
|
|
13
|
+
import 'mdast-util-from-markdown';
|
|
14
|
+
import 'mdast-util-gfm';
|
|
15
|
+
import 'mdast-util-to-hast';
|
|
16
|
+
|
|
17
|
+
class FilePatcher {
|
|
18
|
+
files = /* @__PURE__ */ new Map();
|
|
19
|
+
static key(from, to) {
|
|
20
|
+
return `${from}${to ? `:${to}` : ""}`;
|
|
21
|
+
}
|
|
22
|
+
load(path) {
|
|
23
|
+
let file = this.files.get(path);
|
|
24
|
+
if (file === void 0) {
|
|
25
|
+
if (existsSync(path)) {
|
|
26
|
+
const content = readFileSync(path, { encoding: "utf-8" });
|
|
27
|
+
file = { content, patches: /* @__PURE__ */ new Map() };
|
|
28
|
+
} else {
|
|
29
|
+
file = null;
|
|
30
|
+
}
|
|
31
|
+
this.files.set(path, file);
|
|
32
|
+
}
|
|
33
|
+
return file;
|
|
34
|
+
}
|
|
35
|
+
patch(path) {
|
|
36
|
+
const file = this.files.get(path);
|
|
37
|
+
if (file) {
|
|
38
|
+
if (file.patches.size) {
|
|
39
|
+
const s = new MagicString(file.content);
|
|
40
|
+
for (const [key, value] of file.patches) {
|
|
41
|
+
const [from, to] = key.split(":").map((s2) => s2 !== "" ? Number(s2) : void 0);
|
|
42
|
+
if (from === void 0)
|
|
43
|
+
continue;
|
|
44
|
+
if (to !== void 0) {
|
|
45
|
+
s.update(from, to, value);
|
|
46
|
+
} else {
|
|
47
|
+
s.appendRight(from, value);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
const content = s.toString();
|
|
51
|
+
writeFileSync(path, content, { encoding: "utf-8" });
|
|
52
|
+
}
|
|
53
|
+
this.files.delete(path);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
const CODE_INLINE_CACHE_KEY = "@twoslash-cache";
|
|
59
|
+
const CODE_INLINE_CACHE_REGEX = new RegExp(`// ${CODE_INLINE_CACHE_KEY}: (.*)(?:
|
|
60
|
+
|$)`, "g");
|
|
61
|
+
function createInlineTypesCache({ remove, ignoreCache } = {}) {
|
|
62
|
+
const patcher = new FilePatcher();
|
|
63
|
+
const optionsHashCache = /* @__PURE__ */ new WeakMap();
|
|
64
|
+
function getOptionsHash(options = {}) {
|
|
65
|
+
let hash$1 = optionsHashCache.get(options);
|
|
66
|
+
if (!hash$1) {
|
|
67
|
+
hash$1 = hash(options);
|
|
68
|
+
optionsHashCache.set(options, hash$1);
|
|
69
|
+
}
|
|
70
|
+
return hash$1;
|
|
71
|
+
}
|
|
72
|
+
function cacheHash(code, lang, options) {
|
|
73
|
+
return sha256Hash(`${getOptionsHash(options)}:${lang ?? ""}:${code}`);
|
|
74
|
+
}
|
|
75
|
+
function stringifyCachePayload(data, code, lang, options) {
|
|
76
|
+
const hash = cacheHash(code, lang, options);
|
|
77
|
+
const payload = {
|
|
78
|
+
v: 1,
|
|
79
|
+
hash,
|
|
80
|
+
data: LZString.compressToBase64(JSON.stringify(data))
|
|
81
|
+
};
|
|
82
|
+
return JSON.stringify(payload);
|
|
83
|
+
}
|
|
84
|
+
function resolveCachePayload(cache) {
|
|
85
|
+
if (!cache)
|
|
86
|
+
return null;
|
|
87
|
+
try {
|
|
88
|
+
const payload = JSON.parse(cache);
|
|
89
|
+
if (payload.v === 1) {
|
|
90
|
+
return {
|
|
91
|
+
payload,
|
|
92
|
+
twoslash: () => {
|
|
93
|
+
try {
|
|
94
|
+
return JSON.parse(LZString.decompressFromBase64(payload.data));
|
|
95
|
+
} catch {
|
|
96
|
+
return null;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
} catch {
|
|
102
|
+
}
|
|
103
|
+
return null;
|
|
104
|
+
}
|
|
105
|
+
function resolveSourcePatcher(source, search) {
|
|
106
|
+
const file = patcher.load(source.path);
|
|
107
|
+
if (file === null)
|
|
108
|
+
return void 0;
|
|
109
|
+
const range = { from: source.from };
|
|
110
|
+
let linebreak = true;
|
|
111
|
+
if (search) {
|
|
112
|
+
const cachePos = file.content.indexOf(search, source.from);
|
|
113
|
+
if (cachePos !== -1 && cachePos < source.to) {
|
|
114
|
+
range.from = cachePos;
|
|
115
|
+
range.to = cachePos + search.length;
|
|
116
|
+
linebreak = search.endsWith("\n");
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
const patchKey = FilePatcher.key(range.from, range.to);
|
|
120
|
+
return (newCache) => {
|
|
121
|
+
if (newCache === "") {
|
|
122
|
+
if (range.to !== void 0)
|
|
123
|
+
file.patches.set(patchKey, "");
|
|
124
|
+
return;
|
|
125
|
+
}
|
|
126
|
+
file.patches.set(patchKey, newCache + (linebreak ? "\n" : ""));
|
|
127
|
+
};
|
|
128
|
+
}
|
|
129
|
+
const typesCache = {
|
|
130
|
+
preprocess(code, lang, options, meta) {
|
|
131
|
+
if (!meta)
|
|
132
|
+
return;
|
|
133
|
+
let rawCache = "";
|
|
134
|
+
let cacheString = "";
|
|
135
|
+
code = code.replaceAll(CODE_INLINE_CACHE_REGEX, (full, p1) => {
|
|
136
|
+
if (!rawCache.length) {
|
|
137
|
+
cacheString = p1;
|
|
138
|
+
rawCache = full;
|
|
139
|
+
}
|
|
140
|
+
return "";
|
|
141
|
+
});
|
|
142
|
+
const shouldLoadCache = !ignoreCache && !remove;
|
|
143
|
+
if (shouldLoadCache) {
|
|
144
|
+
const cache = resolveCachePayload(cacheString);
|
|
145
|
+
if (cache?.payload.hash === cacheHash(code, lang, options)) {
|
|
146
|
+
const twoslash = cache.twoslash();
|
|
147
|
+
if (twoslash)
|
|
148
|
+
meta.__cache = twoslash;
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
if (meta.sourceMap)
|
|
152
|
+
meta.__patch = resolveSourcePatcher(meta.sourceMap, rawCache);
|
|
153
|
+
return code;
|
|
154
|
+
},
|
|
155
|
+
read(code, lang, options, meta) {
|
|
156
|
+
return meta?.__cache ?? null;
|
|
157
|
+
},
|
|
158
|
+
write(code, data, lang, options, meta) {
|
|
159
|
+
if (remove) {
|
|
160
|
+
meta?.__patch?.("");
|
|
161
|
+
return;
|
|
162
|
+
}
|
|
163
|
+
const twoslashShiki = simplifyTwoslashReturn(data);
|
|
164
|
+
const cacheStr = `// ${CODE_INLINE_CACHE_KEY}: ${stringifyCachePayload(twoslashShiki, code, lang, options)}`;
|
|
165
|
+
meta?.__patch?.(cacheStr);
|
|
166
|
+
}
|
|
167
|
+
};
|
|
168
|
+
return { typesCache, patcher };
|
|
169
|
+
}
|
|
170
|
+
function sha256Hash(str) {
|
|
171
|
+
return createHash("SHA256").update(str).digest("hex");
|
|
172
|
+
}
|
|
173
|
+
function simplifyTwoslashReturn(ret) {
|
|
174
|
+
return {
|
|
175
|
+
nodes: ret.nodes,
|
|
176
|
+
code: ret.code,
|
|
177
|
+
meta: ret.meta ? { extension: ret.meta.extension } : void 0
|
|
178
|
+
};
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
function isEnabledEnv(key) {
|
|
182
|
+
const val = process.env?.[key]?.toLowerCase();
|
|
183
|
+
if (val) {
|
|
184
|
+
return {
|
|
185
|
+
true: true,
|
|
186
|
+
false: false,
|
|
187
|
+
1: true,
|
|
188
|
+
0: false,
|
|
189
|
+
yes: true,
|
|
190
|
+
no: false,
|
|
191
|
+
y: true,
|
|
192
|
+
n: false
|
|
193
|
+
}[val] || null;
|
|
194
|
+
}
|
|
195
|
+
return null;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
function createMarkdownFenceSourceCodec(mapper) {
|
|
199
|
+
const FENCE_SOURCE_WRAP = `<fsm-${Math.random().toString(36).slice(2)}>`;
|
|
200
|
+
const FENCE_SOURCE_REGEX = new RegExp(`// ${FENCE_SOURCE_WRAP}(.+?)${FENCE_SOURCE_WRAP}\\n`);
|
|
201
|
+
function stringifyFenceSourceMap(sourceMap) {
|
|
202
|
+
const data = JSON.stringify(sourceMap);
|
|
203
|
+
return `// ${FENCE_SOURCE_WRAP}${data}${FENCE_SOURCE_WRAP}
|
|
204
|
+
`;
|
|
205
|
+
}
|
|
206
|
+
function injectToMarkdown(code, path) {
|
|
207
|
+
const injects = mapper(code, path);
|
|
208
|
+
let newCode = code;
|
|
209
|
+
const injectAts = Array.from(injects.keys()).sort((a, b) => b - a);
|
|
210
|
+
for (const at of injectAts) {
|
|
211
|
+
const sourceMap = injects.get(at);
|
|
212
|
+
const data = stringifyFenceSourceMap(sourceMap);
|
|
213
|
+
newCode = newCode.slice(0, at) + data + newCode.slice(at);
|
|
214
|
+
}
|
|
215
|
+
return newCode;
|
|
216
|
+
}
|
|
217
|
+
function extractFromFence(code) {
|
|
218
|
+
let sourceMap = null;
|
|
219
|
+
try {
|
|
220
|
+
code = code.replace(FENCE_SOURCE_REGEX, (_, p1) => {
|
|
221
|
+
sourceMap = JSON.parse(p1);
|
|
222
|
+
return "";
|
|
223
|
+
});
|
|
224
|
+
} catch {
|
|
225
|
+
}
|
|
226
|
+
return { code, sourceMap };
|
|
227
|
+
}
|
|
228
|
+
return {
|
|
229
|
+
injectToMarkdown,
|
|
230
|
+
extractFromFence
|
|
231
|
+
};
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
const markdownItMapper = function(code, path) {
|
|
235
|
+
const md = new MarkdownIt();
|
|
236
|
+
const result = md.parse(code, {});
|
|
237
|
+
const pos = getLineStartPositions(code);
|
|
238
|
+
const injects = /* @__PURE__ */ new Map();
|
|
239
|
+
for (const token of result) {
|
|
240
|
+
if (token.type === "fence") {
|
|
241
|
+
if (!token.map)
|
|
242
|
+
continue;
|
|
243
|
+
if (token.map[0] + 1 >= pos.length)
|
|
244
|
+
continue;
|
|
245
|
+
const codeStart = pos[token.map[0] + 1].from;
|
|
246
|
+
const codeEnd = pos[token.map[1] - 1].from;
|
|
247
|
+
injects.set(codeStart, { path, from: codeStart, to: codeEnd });
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
return injects;
|
|
251
|
+
};
|
|
252
|
+
function getLineStartPositions(text) {
|
|
253
|
+
const positions = [];
|
|
254
|
+
let pos = 0;
|
|
255
|
+
while (true) {
|
|
256
|
+
const [idx, len] = findNextNewLine(text, pos);
|
|
257
|
+
if (idx === -1) {
|
|
258
|
+
positions.push({ from: pos, to: text.length });
|
|
259
|
+
break;
|
|
260
|
+
}
|
|
261
|
+
positions.push({ from: pos, to: idx });
|
|
262
|
+
pos = idx + len;
|
|
263
|
+
}
|
|
264
|
+
return positions;
|
|
265
|
+
}
|
|
266
|
+
function findNextNewLine(str, position) {
|
|
267
|
+
const nIdx = str.indexOf("\n", position);
|
|
268
|
+
const rIdx = str.indexOf("\r", position);
|
|
269
|
+
if (nIdx === -1 && rIdx === -1)
|
|
270
|
+
return [-1, 0];
|
|
271
|
+
let idx;
|
|
272
|
+
if (nIdx === -1)
|
|
273
|
+
idx = rIdx;
|
|
274
|
+
else if (rIdx === -1)
|
|
275
|
+
idx = nIdx;
|
|
276
|
+
else idx = nIdx < rIdx ? nIdx : rIdx;
|
|
277
|
+
if (str.charCodeAt(idx) === 13 && str.charCodeAt(idx + 1) === 10) {
|
|
278
|
+
return [idx, 2];
|
|
279
|
+
}
|
|
280
|
+
return [idx, 1];
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
function createTwoslashWithInlineCache(twoslashOptions = {}, {
|
|
284
|
+
sourceMapper = markdownItMapper,
|
|
285
|
+
sourceMapCodec = createMarkdownFenceSourceCodec(sourceMapper)
|
|
286
|
+
} = {}) {
|
|
287
|
+
return function(config) {
|
|
288
|
+
if (isEnabledEnv("TWOSLASH_INLINE_CACHE") === false)
|
|
289
|
+
return config;
|
|
290
|
+
const { typesCache, patcher } = createInlineTypesCache({
|
|
291
|
+
remove: isEnabledEnv("TWOSLASH_INLINE_CACHE_REMOVE") === true,
|
|
292
|
+
ignoreCache: isEnabledEnv("TWOSLASH_INLINE_CACHE_IGNORE") === true
|
|
293
|
+
});
|
|
294
|
+
const transformer = transformerTwoslash({ ...twoslashOptions, typesCache });
|
|
295
|
+
const PatchPlugin = {
|
|
296
|
+
name: "vitepress-twoslash:patch",
|
|
297
|
+
enforce: "post",
|
|
298
|
+
transform(code, id) {
|
|
299
|
+
if (id.endsWith(".md")) {
|
|
300
|
+
patcher.patch(id);
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
};
|
|
304
|
+
config = withFenceSourceMap(config, sourceMapCodec);
|
|
305
|
+
const codeTransformers = (config.markdown ??= {}).codeTransformers ??= [];
|
|
306
|
+
codeTransformers.push(transformer);
|
|
307
|
+
const plugins = (config.vite ??= {}).plugins ??= [];
|
|
308
|
+
plugins.push(PatchPlugin);
|
|
309
|
+
return config;
|
|
310
|
+
};
|
|
311
|
+
}
|
|
312
|
+
function withFenceSourceMap(config, codec) {
|
|
313
|
+
const InjectPlugin = {
|
|
314
|
+
name: "vitepress-twoslash:inject-fence-source-map",
|
|
315
|
+
enforce: "pre",
|
|
316
|
+
load(id) {
|
|
317
|
+
if (id.endsWith(".md")) {
|
|
318
|
+
const code = readFileSync(id, "utf-8");
|
|
319
|
+
return {
|
|
320
|
+
code: codec.injectToMarkdown(code, id)
|
|
321
|
+
};
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
};
|
|
325
|
+
const transformer = {
|
|
326
|
+
name: "vitepress-twoslash:extract-fence-source-map",
|
|
327
|
+
enforce: "pre",
|
|
328
|
+
preprocess(code) {
|
|
329
|
+
const { code: transformedCode, sourceMap } = codec.extractFromFence(code);
|
|
330
|
+
this.meta.sourceMap = sourceMap;
|
|
331
|
+
return transformedCode;
|
|
332
|
+
}
|
|
333
|
+
};
|
|
334
|
+
const codeTransformers = (config.markdown ??= {}).codeTransformers ??= [];
|
|
335
|
+
codeTransformers.unshift(transformer);
|
|
336
|
+
const plugins = (config.vite ??= {}).plugins ??= [];
|
|
337
|
+
plugins.push(InjectPlugin);
|
|
338
|
+
return config;
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
export { createTwoslashWithInlineCache };
|
package/dist/client.mjs
CHANGED
|
@@ -13,6 +13,7 @@ const TwoslashFloatingVue = {
|
|
|
13
13
|
app.use(FloatingVue, {
|
|
14
14
|
...options,
|
|
15
15
|
themes: {
|
|
16
|
+
...options.themes,
|
|
16
17
|
"twoslash": {
|
|
17
18
|
$extend: "dropdown",
|
|
18
19
|
triggers: isMobile ? ["touch"] : ["hover", "touch"],
|
|
@@ -25,13 +26,15 @@ const TwoslashFloatingVue = {
|
|
|
25
26
|
instantMove: true,
|
|
26
27
|
flip: false,
|
|
27
28
|
arrowPadding: 8,
|
|
28
|
-
autoBoundaryMaxSize: true
|
|
29
|
+
autoBoundaryMaxSize: true,
|
|
30
|
+
...options.themes?.twoslash ?? {}
|
|
29
31
|
},
|
|
30
32
|
"twoslash-query": {
|
|
31
33
|
$extend: "twoslash",
|
|
32
34
|
triggers: ["click"],
|
|
33
35
|
popperTriggers: ["click"],
|
|
34
|
-
autoHide: false
|
|
36
|
+
autoHide: false,
|
|
37
|
+
...options.themes?.["twoslash-query"] ?? {}
|
|
35
38
|
},
|
|
36
39
|
"twoslash-completion": {
|
|
37
40
|
$extend: "twoslash-query",
|
|
@@ -39,8 +42,10 @@ const TwoslashFloatingVue = {
|
|
|
39
42
|
popperTriggers: ["click"],
|
|
40
43
|
autoHide: false,
|
|
41
44
|
distance: 0,
|
|
42
|
-
arrowOverflow: true
|
|
45
|
+
arrowOverflow: true,
|
|
46
|
+
...options.themes?.["twoslash-completion"] ?? {}
|
|
43
47
|
},
|
|
48
|
+
/** @deprecated */
|
|
44
49
|
...options.theme
|
|
45
50
|
}
|
|
46
51
|
});
|
package/dist/index.d.mts
CHANGED
|
@@ -1,9 +1,8 @@
|
|
|
1
1
|
import { ShikiTransformer } from 'shiki';
|
|
2
|
-
import { V as VitePressPluginTwoslashOptions } from './shared/vitepress-twoslash.
|
|
3
|
-
export { T as TwoslashFloatingVueOptions, a as TwoslashFloatingVueRendererOptions,
|
|
2
|
+
import { V as VitePressPluginTwoslashOptions } from './shared/vitepress-twoslash.CBxCru0j.mjs';
|
|
3
|
+
export { T as TwoslashFloatingVueOptions, a as TwoslashFloatingVueRendererOptions, r as rendererFloatingVue } from './shared/vitepress-twoslash.CBxCru0j.mjs';
|
|
4
4
|
export { defaultHoverInfoProcessor } from '@shikijs/twoslash';
|
|
5
5
|
import '@shikijs/twoslash/core';
|
|
6
|
-
import 'twoslash';
|
|
7
6
|
import 'twoslash-vue';
|
|
8
7
|
|
|
9
8
|
/**
|
package/dist/index.mjs
CHANGED
|
@@ -170,8 +170,7 @@ function compose(parts) {
|
|
|
170
170
|
|
|
171
171
|
function transformerTwoslash(options = {}) {
|
|
172
172
|
const {
|
|
173
|
-
explicitTrigger = true
|
|
174
|
-
typesCache
|
|
173
|
+
explicitTrigger = true
|
|
175
174
|
} = options;
|
|
176
175
|
const onError = (error, code) => {
|
|
177
176
|
const isCI = typeof process !== "undefined" && process?.env?.CI;
|
|
@@ -191,20 +190,9 @@ ${code.split(/\n/g).slice(0, 15).join("\n").trim()}
|
|
|
191
190
|
console.error(error);
|
|
192
191
|
removeTwoslashNotations(code);
|
|
193
192
|
};
|
|
194
|
-
const
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
twoslasher = ((code, extension, options2) => {
|
|
198
|
-
const cached = typesCache.read(code);
|
|
199
|
-
if (cached)
|
|
200
|
-
return cached;
|
|
201
|
-
const twoslashResult = defaultTwoslasher(code, extension, options2);
|
|
202
|
-
typesCache.write(code, twoslashResult);
|
|
203
|
-
return twoslashResult;
|
|
204
|
-
});
|
|
205
|
-
twoslasher.getCacheMap = defaultTwoslasher.getCacheMap;
|
|
206
|
-
}
|
|
207
|
-
const twoslash = createTransformerFactory(twoslasher)({
|
|
193
|
+
const twoslash = createTransformerFactory(
|
|
194
|
+
createTwoslasher(options.twoslashOptions)
|
|
195
|
+
)({
|
|
208
196
|
langs: ["ts", "tsx", "js", "jsx", "json", "vue"],
|
|
209
197
|
renderer: rendererFloatingVue(options),
|
|
210
198
|
onTwoslashError: onError,
|
|
@@ -213,7 +201,6 @@ ${code.split(/\n/g).slice(0, 15).join("\n").trim()}
|
|
|
213
201
|
explicitTrigger
|
|
214
202
|
});
|
|
215
203
|
const trigger = explicitTrigger instanceof RegExp ? explicitTrigger : /\btwoslash\b/;
|
|
216
|
-
typesCache?.init?.();
|
|
217
204
|
return {
|
|
218
205
|
...twoslash,
|
|
219
206
|
name: "@shikijs/vitepress-twoslash",
|
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import { TransformerTwoslashOptions } from '@shikijs/twoslash/core';
|
|
2
|
-
import { TwoslashReturn } from 'twoslash';
|
|
3
2
|
import { VueSpecificOptions } from 'twoslash-vue';
|
|
4
3
|
import { RendererRichOptions, TwoslashRenderer } from '@shikijs/twoslash';
|
|
5
4
|
|
|
@@ -28,42 +27,7 @@ interface VitePressPluginTwoslashOptions extends TransformerTwoslashVueOptions,
|
|
|
28
27
|
* @default true
|
|
29
28
|
*/
|
|
30
29
|
explicitTrigger?: TransformerTwoslashOptions['explicitTrigger'];
|
|
31
|
-
/**
|
|
32
|
-
* The options for caching resolved types
|
|
33
|
-
*
|
|
34
|
-
* @example
|
|
35
|
-
* ```ts
|
|
36
|
-
* import { transformerTwoslash } from '@shikijs/vitepress-twoslash'
|
|
37
|
-
* import { createFileSystemTypesCache } from '@shikijs/vitepress-twoslash/cache-fs'
|
|
38
|
-
*
|
|
39
|
-
* transformerTwoslash({
|
|
40
|
-
* typesCache: createFileSystemTypesCache({
|
|
41
|
-
* dir: './my-cache-dir'
|
|
42
|
-
* })
|
|
43
|
-
* })
|
|
44
|
-
* ```
|
|
45
|
-
*/
|
|
46
|
-
typesCache?: TwoslashTypesCache;
|
|
47
|
-
}
|
|
48
|
-
interface TwoslashTypesCache {
|
|
49
|
-
/**
|
|
50
|
-
* Read cached result
|
|
51
|
-
*
|
|
52
|
-
* @param code Source code
|
|
53
|
-
*/
|
|
54
|
-
read: (code: string) => TwoslashReturn | null;
|
|
55
|
-
/**
|
|
56
|
-
* Save result to cache
|
|
57
|
-
*
|
|
58
|
-
* @param code Source code
|
|
59
|
-
* @param data Twoslash data
|
|
60
|
-
*/
|
|
61
|
-
write: (code: string, data: TwoslashReturn) => void;
|
|
62
|
-
/**
|
|
63
|
-
* On initialization
|
|
64
|
-
*/
|
|
65
|
-
init?: () => void;
|
|
66
30
|
}
|
|
67
31
|
|
|
68
32
|
export { rendererFloatingVue as r };
|
|
69
|
-
export type { TwoslashFloatingVueOptions as T, VitePressPluginTwoslashOptions as V, TwoslashFloatingVueRendererOptions as a
|
|
33
|
+
export type { TwoslashFloatingVueOptions as T, VitePressPluginTwoslashOptions as V, TwoslashFloatingVueRendererOptions as a };
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@shikijs/vitepress-twoslash",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "3.
|
|
4
|
+
"version": "3.12.0",
|
|
5
5
|
"description": "Enable Twoslash support in VitePress",
|
|
6
6
|
"author": "Anthony Fu <anthonyfu117@hotmail.com>",
|
|
7
7
|
"license": "MIT",
|
|
@@ -22,6 +22,7 @@
|
|
|
22
22
|
".": "./dist/index.mjs",
|
|
23
23
|
"./client": "./dist/client.mjs",
|
|
24
24
|
"./cache-fs": "./dist/cache-fs.mjs",
|
|
25
|
+
"./cache-inline": "./dist/cache-inline/index.mjs",
|
|
25
26
|
"./style.css": "./style.css",
|
|
26
27
|
"./style-core.css": "./style-core.css"
|
|
27
28
|
},
|
|
@@ -35,13 +36,17 @@
|
|
|
35
36
|
"dependencies": {
|
|
36
37
|
"@shikijs/twoslash": "",
|
|
37
38
|
"floating-vue": "^5.2.2",
|
|
39
|
+
"lz-string": "^1.5.0",
|
|
40
|
+
"magic-string": "^0.30.17",
|
|
41
|
+
"markdown-it": "^14.1.0",
|
|
38
42
|
"mdast-util-from-markdown": "^2.0.2",
|
|
39
43
|
"mdast-util-gfm": "^3.1.0",
|
|
40
44
|
"mdast-util-to-hast": "^13.2.0",
|
|
45
|
+
"ohash": "^2.0.11",
|
|
41
46
|
"twoslash": "^0.3.4",
|
|
42
47
|
"twoslash-vue": "^0.3.4",
|
|
43
|
-
"vue": "^3.5.
|
|
44
|
-
"shiki": "3.
|
|
48
|
+
"vue": "^3.5.20",
|
|
49
|
+
"shiki": "3.12.0"
|
|
45
50
|
},
|
|
46
51
|
"scripts": {
|
|
47
52
|
"build": "unbuild",
|