@shikijs/twoslash 3.10.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/core.d.mts CHANGED
@@ -1,4 +1,4 @@
1
- import { CodeToHastOptions, ShikiTransformerContext, ShikiTransformerContextCommon, ShikiTransformer } from '@shikijs/types';
1
+ import { CodeToHastOptions, ShikiTransformerContext, ShikiTransformerContextMeta, ShikiTransformerContextCommon, ShikiTransformer } from '@shikijs/types';
2
2
  import { TwoslashReturn, TwoslashExecuteOptions, TwoslashGenericFunction, TwoslashOptions, NodeError, NodeTag, NodeQuery, NodeCompletion, NodeHover, NodeHighlight } from 'twoslash';
3
3
  import { ElementContent, Element, Text } from 'hast';
4
4
 
@@ -11,6 +11,26 @@ declare module '@shikijs/core' {
11
11
  twoslash?: TwoslashShikiReturn;
12
12
  }
13
13
  }
14
+ interface TwoslashTypesCache {
15
+ /**
16
+ * On initialization
17
+ */
18
+ init?: () => void;
19
+ preprocess?: (code: string, lang?: string, options?: TwoslashExecuteOptions, meta?: ShikiTransformerContextMeta) => string | void;
20
+ /**
21
+ * Read cached result
22
+ *
23
+ * @param code Source code
24
+ */
25
+ read: (code: string, lang?: string, options?: TwoslashExecuteOptions, meta?: ShikiTransformerContextMeta) => TwoslashShikiReturn | null;
26
+ /**
27
+ * Save result to cache
28
+ *
29
+ * @param code Source code
30
+ * @param data Twoslash data
31
+ */
32
+ write: (code: string, data: TwoslashShikiReturn, lang?: string, options?: TwoslashExecuteOptions, meta?: ShikiTransformerContextMeta) => void;
33
+ }
14
34
  interface TransformerTwoslashOptions {
15
35
  /**
16
36
  * Languages to apply this transformer to
@@ -70,6 +90,22 @@ interface TransformerTwoslashOptions {
70
90
  * When specified, `throws` will be ignored
71
91
  */
72
92
  onShikiError?: (error: unknown, code: string, lang: string) => void;
93
+ /**
94
+ * The options for caching resolved types
95
+ *
96
+ * @example
97
+ * ```ts
98
+ * import { transformerTwoslash } from '@shikijs/vitepress-twoslash'
99
+ * import { createFileSystemTypesCache } from '@shikijs/vitepress-twoslash/cache-fs'
100
+ *
101
+ * transformerTwoslash({
102
+ * typesCache: createFileSystemTypesCache({
103
+ * dir: './my-cache-dir'
104
+ * })
105
+ * })
106
+ * ```
107
+ */
108
+ typesCache?: TwoslashTypesCache;
73
109
  }
74
110
  interface TwoslashRenderer {
75
111
  lineError?: (this: ShikiTransformerContext, error: NodeError) => ElementContent[];
@@ -287,4 +323,4 @@ declare function defaultTwoslashOptions(): TwoslashExecuteOptions;
287
323
  declare function createTransformerFactory(defaultTwoslasher: TwoslashShikiFunction | TwoslashGenericFunction, defaultRenderer?: TwoslashRenderer): (options?: TransformerTwoslashOptions) => ShikiTransformer;
288
324
 
289
325
  export { ShikiTwoslashError, createTransformerFactory, defaultCompletionIcons, defaultCustomTagIcons, defaultHoverInfoProcessor, defaultTwoslashOptions, rendererClassic, rendererRich };
290
- export type { CompletionItem, HastExtension, RendererRichOptions, TransformerTwoslashOptions, TwoslashRenderer, TwoslashShikiFunction, TwoslashShikiReturn };
326
+ export type { CompletionItem, HastExtension, RendererRichOptions, TransformerTwoslashOptions, TwoslashRenderer, TwoslashShikiFunction, TwoslashShikiReturn, TwoslashTypesCache };
package/dist/core.mjs CHANGED
@@ -1103,7 +1103,8 @@ function createTransformerFactory(defaultTwoslasher, defaultRenderer) {
1103
1103
  disableTriggers = ["notwoslash", "no-twoslash"],
1104
1104
  renderer = defaultRenderer,
1105
1105
  throws = true,
1106
- includesMap = /* @__PURE__ */ new Map()
1106
+ includesMap = /* @__PURE__ */ new Map(),
1107
+ typesCache
1107
1108
  } = options;
1108
1109
  const onTwoslashError = options.onTwoslashError || (throws ? (error) => {
1109
1110
  throw error;
@@ -1121,6 +1122,21 @@ function createTransformerFactory(defaultTwoslasher, defaultRenderer) {
1121
1122
  }
1122
1123
  } = options;
1123
1124
  const includes = new TwoslashIncludesManager(includesMap);
1125
+ let _twoslasher = twoslasher;
1126
+ if (typesCache) {
1127
+ _twoslasher = ((code, lang, options2, meta) => {
1128
+ const preprocessed = typesCache?.preprocess?.(code, lang, options2, meta);
1129
+ if (preprocessed !== void 0)
1130
+ code = preprocessed;
1131
+ let twoslash = typesCache?.read(code, lang, options2, meta);
1132
+ if (!twoslash) {
1133
+ twoslash = twoslasher(code, lang, options2);
1134
+ typesCache?.write(code, twoslash, lang, options2, meta);
1135
+ }
1136
+ return twoslash;
1137
+ });
1138
+ typesCache.init?.();
1139
+ }
1124
1140
  return {
1125
1141
  preprocess(code) {
1126
1142
  let lang = this.options.lang;
@@ -1132,7 +1148,7 @@ function createTransformerFactory(defaultTwoslasher, defaultRenderer) {
1132
1148
  const include = parseIncludeMeta(this.options.meta?.__raw);
1133
1149
  if (include)
1134
1150
  includes.add(include, codeWithIncludes);
1135
- const twoslash = twoslasher(codeWithIncludes, lang, twoslashOptions);
1151
+ const twoslash = _twoslasher(codeWithIncludes, lang, twoslashOptions, this.meta);
1136
1152
  map.set(this.meta, twoslash);
1137
1153
  this.meta.twoslash = twoslash;
1138
1154
  this.options.lang = twoslash.meta?.extension || lang;
package/dist/index.d.mts CHANGED
@@ -1,7 +1,7 @@
1
1
  import { ShikiTransformer } from '@shikijs/types';
2
2
  import { CreateTwoslashOptions } from 'twoslash';
3
3
  import { TransformerTwoslashOptions, RendererRichOptions } from './core.mjs';
4
- export { CompletionItem, HastExtension, ShikiTwoslashError, TwoslashRenderer, TwoslashShikiFunction, TwoslashShikiReturn, createTransformerFactory, defaultCompletionIcons, defaultCustomTagIcons, defaultHoverInfoProcessor, defaultTwoslashOptions, rendererClassic, rendererRich } from './core.mjs';
4
+ export { CompletionItem, HastExtension, ShikiTwoslashError, TwoslashRenderer, TwoslashShikiFunction, TwoslashShikiReturn, TwoslashTypesCache, createTransformerFactory, defaultCompletionIcons, defaultCustomTagIcons, defaultHoverInfoProcessor, defaultTwoslashOptions, rendererClassic, rendererRich } from './core.mjs';
5
5
  import 'hast';
6
6
 
7
7
  interface TransformerTwoslashIndexOptions extends TransformerTwoslashOptions, Pick<CreateTwoslashOptions, 'cache'> {
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@shikijs/twoslash",
3
3
  "type": "module",
4
- "version": "3.10.0",
4
+ "version": "3.12.0",
5
5
  "description": "Shiki transformer for twoslash",
6
6
  "author": "Anthony Fu <anthonyfu117@hotmail.com>",
7
7
  "license": "MIT",
@@ -36,15 +36,15 @@
36
36
  },
37
37
  "dependencies": {
38
38
  "twoslash": "^0.3.4",
39
- "@shikijs/core": "3.10.0",
40
- "@shikijs/types": "3.10.0"
39
+ "@shikijs/core": "3.12.0",
40
+ "@shikijs/types": "3.12.0"
41
41
  },
42
42
  "devDependencies": {
43
43
  "@iconify-json/carbon": "^1.2.13",
44
- "@iconify-json/codicon": "^1.2.25",
44
+ "@iconify-json/codicon": "^1.2.26",
45
45
  "hast-util-from-html": "^2.0.3",
46
46
  "typescript": "^5.9.2",
47
- "@shikijs/twoslash": "3.10.0"
47
+ "@shikijs/twoslash": "3.12.0"
48
48
  },
49
49
  "scripts": {
50
50
  "build": "unbuild",