@shikijs/vitepress-twoslash 1.22.2 → 1.23.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.
@@ -0,0 +1,17 @@
1
+ import { b as TwoslashTypesCache } from './shared/vitepress-twoslash.dfe2cbbb.mjs';
2
+ import '@shikijs/twoslash/core';
3
+ import 'twoslash';
4
+ import 'twoslash-vue';
5
+ import '@shikijs/twoslash';
6
+
7
+ interface FileSystemTypeResultCacheOptions {
8
+ /**
9
+ * The directory to store the cache files.
10
+ *
11
+ * @default '.vitepress/cache/twoslash'
12
+ */
13
+ dir?: string;
14
+ }
15
+ declare function createFileSystemTypesCache(options?: FileSystemTypeResultCacheOptions): TwoslashTypesCache;
16
+
17
+ export { type FileSystemTypeResultCacheOptions, createFileSystemTypesCache };
@@ -0,0 +1,17 @@
1
+ import { b as TwoslashTypesCache } from './shared/vitepress-twoslash.dfe2cbbb.js';
2
+ import '@shikijs/twoslash/core';
3
+ import 'twoslash';
4
+ import 'twoslash-vue';
5
+ import '@shikijs/twoslash';
6
+
7
+ interface FileSystemTypeResultCacheOptions {
8
+ /**
9
+ * The directory to store the cache files.
10
+ *
11
+ * @default '.vitepress/cache/twoslash'
12
+ */
13
+ dir?: string;
14
+ }
15
+ declare function createFileSystemTypesCache(options?: FileSystemTypeResultCacheOptions): TwoslashTypesCache;
16
+
17
+ export { type FileSystemTypeResultCacheOptions, createFileSystemTypesCache };
@@ -0,0 +1,29 @@
1
+ import { createHash } from 'node:crypto';
2
+ import { mkdirSync, existsSync, readFileSync, writeFileSync } from 'node:fs';
3
+ import { resolve, join } from 'node:path';
4
+ import process from 'node:process';
5
+
6
+ function createFileSystemTypesCache(options = {}) {
7
+ const dir = resolve(process.cwd(), options.dir ?? ".vitepress/cache/twoslash");
8
+ return {
9
+ init() {
10
+ mkdirSync(dir, { recursive: true });
11
+ },
12
+ read(code) {
13
+ const hash = createHash("SHA256").update(code).digest("hex").slice(0, 12);
14
+ const filePath = join(dir, `${hash}.json`);
15
+ if (!existsSync(filePath)) {
16
+ return null;
17
+ }
18
+ return JSON.parse(readFileSync(filePath, { encoding: "utf-8" }));
19
+ },
20
+ write(code, data) {
21
+ const hash = createHash("SHA256").update(code).digest("hex").slice(0, 12);
22
+ const filePath = join(dir, `${hash}.json`);
23
+ const json = JSON.stringify(data);
24
+ writeFileSync(filePath, json, { encoding: "utf-8" });
25
+ }
26
+ };
27
+ }
28
+
29
+ export { createFileSystemTypesCache };
package/dist/index.d.mts CHANGED
@@ -1,36 +1,11 @@
1
- import { TransformerTwoslashOptions } from '@shikijs/twoslash/core';
2
1
  import { ShikiTransformer } from 'shiki';
3
- import { VueSpecificOptions } from 'twoslash-vue';
4
- import { RendererRichOptions, TwoslashRenderer } from '@shikijs/twoslash';
2
+ import { V as VitePressPluginTwoslashOptions } from './shared/vitepress-twoslash.dfe2cbbb.mjs';
3
+ export { T as TwoslashFloatingVueOptions, a as TwoslashFloatingVueRendererOptions, b as TwoslashTypesCache, r as rendererFloatingVue } from './shared/vitepress-twoslash.dfe2cbbb.mjs';
5
4
  export { defaultHoverInfoProcessor } from '@shikijs/twoslash';
5
+ import '@shikijs/twoslash/core';
6
+ import 'twoslash';
7
+ import 'twoslash-vue';
6
8
 
7
- interface TwoslashFloatingVueOptions {
8
- classCopyIgnore?: string;
9
- classFloatingPanel?: string;
10
- classCode?: string;
11
- classMarkdown?: string;
12
- floatingVueTheme?: string;
13
- floatingVueThemeQuery?: string;
14
- floatingVueThemeCompletion?: string;
15
- }
16
- interface TwoslashFloatingVueRendererOptions extends RendererRichOptions {
17
- /**
18
- * Class and themes for floating-vue specific nodes
19
- */
20
- floatingVue?: TwoslashFloatingVueOptions;
21
- }
22
- declare function rendererFloatingVue(options?: TwoslashFloatingVueRendererOptions): TwoslashRenderer;
23
-
24
- interface TransformerTwoslashVueOptions extends TransformerTwoslashOptions {
25
- twoslashOptions?: TransformerTwoslashOptions['twoslashOptions'] & VueSpecificOptions;
26
- }
27
- interface VitePressPluginTwoslashOptions extends TransformerTwoslashVueOptions, TwoslashFloatingVueRendererOptions {
28
- /**
29
- * Requires adding `twoslash` to the code block explicitly to run twoslash
30
- * @default true
31
- */
32
- explicitTrigger?: TransformerTwoslashOptions['explicitTrigger'];
33
- }
34
9
  /**
35
10
  * Create a Shiki transformer for VitePress to enable twoslash integration
36
11
  *
@@ -38,4 +13,4 @@ interface VitePressPluginTwoslashOptions extends TransformerTwoslashVueOptions,
38
13
  */
39
14
  declare function transformerTwoslash(options?: VitePressPluginTwoslashOptions): ShikiTransformer;
40
15
 
41
- export { type TwoslashFloatingVueOptions, type TwoslashFloatingVueRendererOptions, type VitePressPluginTwoslashOptions, rendererFloatingVue, transformerTwoslash };
16
+ export { VitePressPluginTwoslashOptions, transformerTwoslash };
package/dist/index.d.ts CHANGED
@@ -1,36 +1,11 @@
1
- import { TransformerTwoslashOptions } from '@shikijs/twoslash/core';
2
1
  import { ShikiTransformer } from 'shiki';
3
- import { VueSpecificOptions } from 'twoslash-vue';
4
- import { RendererRichOptions, TwoslashRenderer } from '@shikijs/twoslash';
2
+ import { V as VitePressPluginTwoslashOptions } from './shared/vitepress-twoslash.dfe2cbbb.js';
3
+ export { T as TwoslashFloatingVueOptions, a as TwoslashFloatingVueRendererOptions, b as TwoslashTypesCache, r as rendererFloatingVue } from './shared/vitepress-twoslash.dfe2cbbb.js';
5
4
  export { defaultHoverInfoProcessor } from '@shikijs/twoslash';
5
+ import '@shikijs/twoslash/core';
6
+ import 'twoslash';
7
+ import 'twoslash-vue';
6
8
 
7
- interface TwoslashFloatingVueOptions {
8
- classCopyIgnore?: string;
9
- classFloatingPanel?: string;
10
- classCode?: string;
11
- classMarkdown?: string;
12
- floatingVueTheme?: string;
13
- floatingVueThemeQuery?: string;
14
- floatingVueThemeCompletion?: string;
15
- }
16
- interface TwoslashFloatingVueRendererOptions extends RendererRichOptions {
17
- /**
18
- * Class and themes for floating-vue specific nodes
19
- */
20
- floatingVue?: TwoslashFloatingVueOptions;
21
- }
22
- declare function rendererFloatingVue(options?: TwoslashFloatingVueRendererOptions): TwoslashRenderer;
23
-
24
- interface TransformerTwoslashVueOptions extends TransformerTwoslashOptions {
25
- twoslashOptions?: TransformerTwoslashOptions['twoslashOptions'] & VueSpecificOptions;
26
- }
27
- interface VitePressPluginTwoslashOptions extends TransformerTwoslashVueOptions, TwoslashFloatingVueRendererOptions {
28
- /**
29
- * Requires adding `twoslash` to the code block explicitly to run twoslash
30
- * @default true
31
- */
32
- explicitTrigger?: TransformerTwoslashOptions['explicitTrigger'];
33
- }
34
9
  /**
35
10
  * Create a Shiki transformer for VitePress to enable twoslash integration
36
11
  *
@@ -38,4 +13,4 @@ interface VitePressPluginTwoslashOptions extends TransformerTwoslashVueOptions,
38
13
  */
39
14
  declare function transformerTwoslash(options?: VitePressPluginTwoslashOptions): ShikiTransformer;
40
15
 
41
- export { type TwoslashFloatingVueOptions, type TwoslashFloatingVueRendererOptions, type VitePressPluginTwoslashOptions, rendererFloatingVue, transformerTwoslash };
16
+ export { VitePressPluginTwoslashOptions, transformerTwoslash };
package/dist/index.mjs CHANGED
@@ -167,7 +167,8 @@ function compose(parts) {
167
167
 
168
168
  function transformerTwoslash(options = {}) {
169
169
  const {
170
- explicitTrigger = true
170
+ explicitTrigger = true,
171
+ typesCache
171
172
  } = options;
172
173
  const onError = (error, code) => {
173
174
  const isCI = typeof process !== "undefined" && process?.env?.CI;
@@ -187,9 +188,20 @@ ${code.split(/\n/g).slice(0, 15).join("\n").trim()}
187
188
  console.error(error);
188
189
  removeTwoslashNotations(code);
189
190
  };
190
- const twoslash = createTransformerFactory(
191
- createTwoslasher(options.twoslashOptions)
192
- )({
191
+ const defaultTwoslasher = createTwoslasher(options.twoslashOptions);
192
+ let twoslasher = defaultTwoslasher;
193
+ if (typesCache) {
194
+ twoslasher = (code, extension, options2) => {
195
+ const cached = typesCache.read(code);
196
+ if (cached)
197
+ return cached;
198
+ const twoslashResult = defaultTwoslasher(code, extension, options2);
199
+ typesCache.write(code, twoslashResult);
200
+ return twoslashResult;
201
+ };
202
+ twoslasher.getCacheMap = defaultTwoslasher.getCacheMap;
203
+ }
204
+ const twoslash = createTransformerFactory(twoslasher)({
193
205
  langs: ["ts", "tsx", "js", "jsx", "json", "vue"],
194
206
  renderer: rendererFloatingVue(options),
195
207
  onTwoslashError: onError,
@@ -198,6 +210,7 @@ ${code.split(/\n/g).slice(0, 15).join("\n").trim()}
198
210
  explicitTrigger
199
211
  });
200
212
  const trigger = explicitTrigger instanceof RegExp ? explicitTrigger : /\btwoslash\b/;
213
+ typesCache?.init?.();
201
214
  return {
202
215
  ...twoslash,
203
216
  name: "@shikijs/vitepress-twoslash",
@@ -0,0 +1,69 @@
1
+ import { TransformerTwoslashOptions } from '@shikijs/twoslash/core';
2
+ import { TwoslashReturn } from 'twoslash';
3
+ import { VueSpecificOptions } from 'twoslash-vue';
4
+ import { RendererRichOptions, TwoslashRenderer } from '@shikijs/twoslash';
5
+
6
+ interface TwoslashFloatingVueOptions {
7
+ classCopyIgnore?: string;
8
+ classFloatingPanel?: string;
9
+ classCode?: string;
10
+ classMarkdown?: string;
11
+ floatingVueTheme?: string;
12
+ floatingVueThemeQuery?: string;
13
+ floatingVueThemeCompletion?: string;
14
+ }
15
+ interface TwoslashFloatingVueRendererOptions extends RendererRichOptions {
16
+ /**
17
+ * Class and themes for floating-vue specific nodes
18
+ */
19
+ floatingVue?: TwoslashFloatingVueOptions;
20
+ }
21
+ declare function rendererFloatingVue(options?: TwoslashFloatingVueRendererOptions): TwoslashRenderer;
22
+
23
+ interface TransformerTwoslashVueOptions extends TransformerTwoslashOptions {
24
+ twoslashOptions?: TransformerTwoslashOptions['twoslashOptions'] & VueSpecificOptions;
25
+ }
26
+ interface VitePressPluginTwoslashOptions extends TransformerTwoslashVueOptions, TwoslashFloatingVueRendererOptions {
27
+ /**
28
+ * Requires adding `twoslash` to the code block explicitly to run twoslash
29
+ * @default true
30
+ */
31
+ explicitTrigger?: TransformerTwoslashOptions['explicitTrigger'];
32
+ /**
33
+ * The options for caching resolved types
34
+ *
35
+ * @example
36
+ * ```ts
37
+ * import { transformerTwoslash } from '@shikijs/vitepress-twoslash'
38
+ * import { createFileSystemTypesCache } from '@shikijs/vitepress-twoslash/cache-fs'
39
+ *
40
+ * transformerTwoslash({
41
+ * typesCache: createFileSystemTypesCache({
42
+ * dir: './my-cache-dir'
43
+ * })
44
+ * })
45
+ * ```
46
+ */
47
+ typesCache?: TwoslashTypesCache;
48
+ }
49
+ interface TwoslashTypesCache {
50
+ /**
51
+ * Read cached result
52
+ *
53
+ * @param code Source code
54
+ */
55
+ read: (code: string) => TwoslashReturn | null;
56
+ /**
57
+ * Save result to cache
58
+ *
59
+ * @param code Source code
60
+ * @param data Twoslash data
61
+ */
62
+ write: (code: string, data: TwoslashReturn) => void;
63
+ /**
64
+ * On initialization
65
+ */
66
+ init?: () => void;
67
+ }
68
+
69
+ export { type TwoslashFloatingVueOptions as T, type VitePressPluginTwoslashOptions as V, type TwoslashFloatingVueRendererOptions as a, type TwoslashTypesCache as b, rendererFloatingVue as r };
@@ -0,0 +1,69 @@
1
+ import { TransformerTwoslashOptions } from '@shikijs/twoslash/core';
2
+ import { TwoslashReturn } from 'twoslash';
3
+ import { VueSpecificOptions } from 'twoslash-vue';
4
+ import { RendererRichOptions, TwoslashRenderer } from '@shikijs/twoslash';
5
+
6
+ interface TwoslashFloatingVueOptions {
7
+ classCopyIgnore?: string;
8
+ classFloatingPanel?: string;
9
+ classCode?: string;
10
+ classMarkdown?: string;
11
+ floatingVueTheme?: string;
12
+ floatingVueThemeQuery?: string;
13
+ floatingVueThemeCompletion?: string;
14
+ }
15
+ interface TwoslashFloatingVueRendererOptions extends RendererRichOptions {
16
+ /**
17
+ * Class and themes for floating-vue specific nodes
18
+ */
19
+ floatingVue?: TwoslashFloatingVueOptions;
20
+ }
21
+ declare function rendererFloatingVue(options?: TwoslashFloatingVueRendererOptions): TwoslashRenderer;
22
+
23
+ interface TransformerTwoslashVueOptions extends TransformerTwoslashOptions {
24
+ twoslashOptions?: TransformerTwoslashOptions['twoslashOptions'] & VueSpecificOptions;
25
+ }
26
+ interface VitePressPluginTwoslashOptions extends TransformerTwoslashVueOptions, TwoslashFloatingVueRendererOptions {
27
+ /**
28
+ * Requires adding `twoslash` to the code block explicitly to run twoslash
29
+ * @default true
30
+ */
31
+ explicitTrigger?: TransformerTwoslashOptions['explicitTrigger'];
32
+ /**
33
+ * The options for caching resolved types
34
+ *
35
+ * @example
36
+ * ```ts
37
+ * import { transformerTwoslash } from '@shikijs/vitepress-twoslash'
38
+ * import { createFileSystemTypesCache } from '@shikijs/vitepress-twoslash/cache-fs'
39
+ *
40
+ * transformerTwoslash({
41
+ * typesCache: createFileSystemTypesCache({
42
+ * dir: './my-cache-dir'
43
+ * })
44
+ * })
45
+ * ```
46
+ */
47
+ typesCache?: TwoslashTypesCache;
48
+ }
49
+ interface TwoslashTypesCache {
50
+ /**
51
+ * Read cached result
52
+ *
53
+ * @param code Source code
54
+ */
55
+ read: (code: string) => TwoslashReturn | null;
56
+ /**
57
+ * Save result to cache
58
+ *
59
+ * @param code Source code
60
+ * @param data Twoslash data
61
+ */
62
+ write: (code: string, data: TwoslashReturn) => void;
63
+ /**
64
+ * On initialization
65
+ */
66
+ init?: () => void;
67
+ }
68
+
69
+ export { type TwoslashFloatingVueOptions as T, type VitePressPluginTwoslashOptions as V, type TwoslashFloatingVueRendererOptions as a, type TwoslashTypesCache as b, rendererFloatingVue as r };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@shikijs/vitepress-twoslash",
3
3
  "type": "module",
4
- "version": "1.22.2",
4
+ "version": "1.23.0",
5
5
  "description": "Enable Twoslash support in VitePress",
6
6
  "author": "Anthony Fu <anthonyfu117@hotmail.com>",
7
7
  "license": "MIT",
@@ -27,6 +27,10 @@
27
27
  "types": "./dist/client.d.mts",
28
28
  "default": "./dist/client.mjs"
29
29
  },
30
+ "./cache-fs": {
31
+ "types": "./dist/cache-fs.d.mts",
32
+ "default": "./dist/cache-fs.mjs"
33
+ },
30
34
  "./style.css": "./style.css",
31
35
  "./style-core.css": "./style-core.css"
32
36
  },
@@ -56,7 +60,7 @@
56
60
  "twoslash": "^0.2.12",
57
61
  "twoslash-vue": "^0.2.12",
58
62
  "vue": "^3.5.12",
59
- "shiki": "1.22.2"
63
+ "shiki": "1.23.0"
60
64
  },
61
65
  "scripts": {
62
66
  "build": "unbuild",