@shikijs/twoslash 3.23.0 → 4.0.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,308 +1,313 @@
1
- import { CodeToHastOptions, ShikiTransformerContextCommon, ShikiTransformerContext, ShikiTransformerContextMeta, ShikiTransformer } from '@shikijs/types';
2
- import { TwoslashReturn, TwoslashExecuteOptions, TwoslashGenericFunction, TwoslashOptions, NodeError, NodeTag, NodeQuery, NodeCompletion, NodeHover, NodeHighlight } from 'twoslash';
3
- import { ElementContent, Element, Text } from 'hast';
1
+ import { NodeCompletion, NodeError, NodeHighlight, NodeHover, NodeQuery, NodeTag, TwoslashExecuteOptions, TwoslashGenericFunction, TwoslashOptions, TwoslashReturn } from "twoslash";
2
+ import { CodeToHastOptions, ShikiTransformer, ShikiTransformerContext, ShikiTransformerContextCommon, ShikiTransformerContextMeta } from "@shikijs/types";
3
+ import { Element, ElementContent, Text } from "hast";
4
4
 
5
+ //#region src/types.d.ts
5
6
  type TwoslashShikiReturn = Pick<TwoslashReturn, 'nodes' | 'code'> & {
6
- meta?: Partial<Pick<TwoslashReturn['meta'], 'extension'>>;
7
+ meta?: Partial<Pick<TwoslashReturn['meta'], 'extension'>>;
7
8
  };
8
9
  type TwoslashShikiFunction = (code: string, lang?: string, options?: TwoslashExecuteOptions) => TwoslashShikiReturn;
9
10
  declare module '@shikijs/core' {
10
- interface ShikiTransformerContextMeta {
11
- twoslash?: TwoslashShikiReturn;
12
- }
11
+ interface ShikiTransformerContextMeta {
12
+ twoslash?: TwoslashShikiReturn;
13
+ }
13
14
  }
14
15
  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;
16
+ /**
17
+ * On initialization
18
+ */
19
+ init?: () => void;
20
+ preprocess?: (code: string, lang?: string, options?: TwoslashExecuteOptions, meta?: ShikiTransformerContextMeta) => string | void;
21
+ /**
22
+ * Read cached result
23
+ *
24
+ * @param code Source code
25
+ */
26
+ read: (code: string, lang?: string, options?: TwoslashExecuteOptions, meta?: ShikiTransformerContextMeta) => TwoslashShikiReturn | null;
27
+ /**
28
+ * Save result to cache
29
+ *
30
+ * @param code Source code
31
+ * @param data Twoslash data
32
+ */
33
+ write: (code: string, data: TwoslashShikiReturn, lang?: string, options?: TwoslashExecuteOptions, meta?: ShikiTransformerContextMeta) => void;
33
34
  }
34
35
  interface TransformerTwoslashOptions {
35
- /**
36
- * Languages to apply this transformer to
37
- */
38
- langs?: string[];
39
- /**
40
- * Requires `twoslash` to be presented in the code block meta to apply this transformer
41
- *
42
- * @default false
43
- */
44
- explicitTrigger?: boolean | RegExp;
45
- /**
46
- * Triggers that skip Twoslash transformation on the code block meta
47
- *
48
- * @default ['notwoslash', 'no-twoslash']
49
- */
50
- disableTriggers?: (string | RegExp)[];
51
- /**
52
- * Mapping from language alias to language name
53
- */
54
- langAlias?: Record<string, string>;
55
- /**
56
- * Custom filter function to apply this transformer to
57
- * When specified, `langs`, `explicitTrigger`, and `disableTriggers` will be ignored
58
- */
59
- filter?: (lang: string, code: string, options: CodeToHastOptions, context?: ShikiTransformerContextCommon) => boolean;
60
- /**
61
- * Custom instance of twoslasher function
62
- */
63
- twoslasher?: TwoslashShikiFunction | TwoslashGenericFunction;
64
- /**
65
- * Options to pass to twoslash
66
- */
67
- twoslashOptions?: TwoslashOptions;
68
- /**
69
- * Custom renderers to decide how each info should be rendered
70
- */
71
- renderer?: TwoslashRenderer;
72
- /**
73
- * A map to store code for `@include` directive
74
- * Provide your own instance if you want to clear the map between each transformation
75
- */
76
- includesMap?: Map<string, string>;
77
- /**
78
- * Strictly throw when there is an error
79
- * @default true
80
- */
81
- throws?: boolean;
82
- /**
83
- * Custom error handler for twoslash errors
84
- * When specified, `throws` will be ignored
85
- * Optionally return a string to replace the code
86
- */
87
- onTwoslashError?: (error: unknown, code: string, lang: string, options: CodeToHastOptions) => string | void;
88
- /**
89
- * Custom error handler for Shiki errors
90
- * When specified, `throws` will be ignored
91
- */
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;
36
+ /**
37
+ * Languages to apply this transformer to
38
+ */
39
+ langs?: string[];
40
+ /**
41
+ * Requires `twoslash` to be presented in the code block meta to apply this transformer
42
+ *
43
+ * @default false
44
+ */
45
+ explicitTrigger?: boolean | RegExp;
46
+ /**
47
+ * Triggers that skip Twoslash transformation on the code block meta
48
+ *
49
+ * @default ['notwoslash', 'no-twoslash']
50
+ */
51
+ disableTriggers?: (string | RegExp)[];
52
+ /**
53
+ * Mapping from language alias to language name
54
+ */
55
+ langAlias?: Record<string, string>;
56
+ /**
57
+ * Custom filter function to apply this transformer to
58
+ * When specified, `langs`, `explicitTrigger`, and `disableTriggers` will be ignored
59
+ */
60
+ filter?: (lang: string, code: string, options: CodeToHastOptions, context?: ShikiTransformerContextCommon) => boolean;
61
+ /**
62
+ * Custom instance of twoslasher function
63
+ */
64
+ twoslasher?: TwoslashShikiFunction | TwoslashGenericFunction;
65
+ /**
66
+ * Options to pass to twoslash
67
+ */
68
+ twoslashOptions?: TwoslashOptions;
69
+ /**
70
+ * Custom renderers to decide how each info should be rendered
71
+ */
72
+ renderer?: TwoslashRenderer;
73
+ /**
74
+ * A map to store code for `@include` directive
75
+ * Provide your own instance if you want to clear the map between each transformation
76
+ */
77
+ includesMap?: Map<string, string>;
78
+ /**
79
+ * Strictly throw when there is an error
80
+ * @default true
81
+ */
82
+ throws?: boolean;
83
+ /**
84
+ * Custom error handler for twoslash errors
85
+ * When specified, `throws` will be ignored
86
+ * Optionally return a string to replace the code
87
+ */
88
+ onTwoslashError?: (error: unknown, code: string, lang: string, options: CodeToHastOptions) => string | void;
89
+ /**
90
+ * Custom error handler for Shiki errors
91
+ * When specified, `throws` will be ignored
92
+ */
93
+ onShikiError?: (error: unknown, code: string, lang: string) => void;
94
+ /**
95
+ * The options for caching resolved types
96
+ *
97
+ * @example
98
+ * ```ts
99
+ * import { transformerTwoslash } from '@shikijs/vitepress-twoslash'
100
+ * import { createFileSystemTypesCache } from '@shikijs/vitepress-twoslash/cache-fs'
101
+ *
102
+ * transformerTwoslash({
103
+ * typesCache: createFileSystemTypesCache({
104
+ * dir: './my-cache-dir'
105
+ * })
106
+ * })
107
+ * ```
108
+ */
109
+ typesCache?: TwoslashTypesCache;
109
110
  }
110
111
  interface TwoslashRenderer {
111
- lineError?: (this: ShikiTransformerContext, error: NodeError) => ElementContent[];
112
- lineCustomTag?: (this: ShikiTransformerContext, tag: NodeTag) => ElementContent[];
113
- lineQuery?: (this: ShikiTransformerContext, query: NodeQuery, targetNode?: Element | Text) => ElementContent[];
114
- lineCompletion?: (this: ShikiTransformerContext, query: NodeCompletion) => ElementContent[];
115
- nodeStaticInfo: (this: ShikiTransformerContext, info: NodeHover, node: Element | Text) => Partial<ElementContent>;
116
- nodeError?: (this: ShikiTransformerContext, error: NodeError, node: Element | Text) => Partial<ElementContent>;
117
- nodeQuery?: (this: ShikiTransformerContext, query: NodeQuery, node: Element | Text) => Partial<ElementContent>;
118
- nodeCompletion?: (this: ShikiTransformerContext, query: NodeCompletion, node: Element | Text) => Partial<ElementContent>;
119
- nodesError?: (this: ShikiTransformerContext, error: NodeError, nodes: ElementContent[]) => ElementContent[];
120
- nodesHighlight?: (this: ShikiTransformerContext, highlight: NodeHighlight, nodes: ElementContent[]) => ElementContent[];
112
+ lineError?: (this: ShikiTransformerContext, error: NodeError) => ElementContent[];
113
+ lineCustomTag?: (this: ShikiTransformerContext, tag: NodeTag) => ElementContent[];
114
+ lineQuery?: (this: ShikiTransformerContext, query: NodeQuery, targetNode?: Element | Text) => ElementContent[];
115
+ lineCompletion?: (this: ShikiTransformerContext, query: NodeCompletion) => ElementContent[];
116
+ nodeStaticInfo: (this: ShikiTransformerContext, info: NodeHover, node: Element | Text) => Partial<ElementContent>;
117
+ nodeError?: (this: ShikiTransformerContext, error: NodeError, node: Element | Text) => Partial<ElementContent>;
118
+ nodeQuery?: (this: ShikiTransformerContext, query: NodeQuery, node: Element | Text) => Partial<ElementContent>;
119
+ nodeCompletion?: (this: ShikiTransformerContext, query: NodeCompletion, node: Element | Text) => Partial<ElementContent>;
120
+ nodesError?: (this: ShikiTransformerContext, error: NodeError, nodes: ElementContent[]) => ElementContent[];
121
+ nodesHighlight?: (this: ShikiTransformerContext, highlight: NodeHighlight, nodes: ElementContent[]) => ElementContent[];
121
122
  }
122
-
123
+ //#endregion
124
+ //#region src/error.d.ts
123
125
  declare class ShikiTwoslashError extends Error {
124
- constructor(message: string);
126
+ constructor(message: string);
125
127
  }
126
-
128
+ //#endregion
129
+ //#region src/icons.d.ts
127
130
  type CompletionItem = NonNullable<NodeCompletion['completions']>[number];
128
131
  declare const defaultCompletionIcons: Record<string, Element | undefined>;
129
132
  declare const defaultCustomTagIcons: Record<string, Element | undefined>;
130
-
133
+ //#endregion
134
+ //#region src/renderer-classic.d.ts
131
135
  /**
132
136
  * The default renderer aligning with the original `@shikijs/twoslash` output.
133
137
  */
134
138
  declare function rendererClassic(): TwoslashRenderer;
135
-
139
+ //#endregion
140
+ //#region src/renderer-rich.d.ts
136
141
  interface RendererRichOptions {
142
+ /**
143
+ * Render JSDoc comments in hover popup.
144
+ *
145
+ * @default true
146
+ */
147
+ jsdoc?: boolean;
148
+ /**
149
+ * Custom icons for completion items.
150
+ * A map from completion item kind to a HAST node.
151
+ *
152
+ * If `false`, no icons will be rendered.
153
+ * @default defaultCompletionIcons
154
+ */
155
+ completionIcons?: Partial<Record<string, ElementContent>> | false;
156
+ /**
157
+ * Custom icons for custom tags lines.
158
+ * A map from tag name to a HAST node.
159
+ *
160
+ * If `false`, no icons will be rendered.
161
+ * @default defaultCustomTagIcons
162
+ */
163
+ customTagIcons?: Partial<Record<string, ElementContent>> | false;
164
+ /**
165
+ * Custom formatter for the type info text.
166
+ * Note that it might not be valid TypeScript syntax.
167
+ *
168
+ * @default defaultHoverInfoProcessor
169
+ */
170
+ processHoverInfo?: (info: string) => string;
171
+ /**
172
+ * Custom formatter for the docs text (can be markdown).
173
+ *
174
+ * @default undefined
175
+ */
176
+ processHoverDocs?: (docs: string) => string;
177
+ /**
178
+ * The way errors should be rendered.
179
+ *
180
+ * - `'line'`: Render the error line after the line of code
181
+ * - `'hover'`: Render the error in the hover popup
182
+ *
183
+ * @default 'line'
184
+ */
185
+ errorRendering?: 'line' | 'hover';
186
+ /**
187
+ * Classes added to injected elements
188
+ */
189
+ classExtra?: string;
190
+ /**
191
+ * Language for syntax highlight.
192
+ * @default the language of the code block
193
+ */
194
+ lang?: string;
195
+ /**
196
+ * Custom function to render markdown.
197
+ *
198
+ * By default it pass-through the markdown.
199
+ */
200
+ renderMarkdown?: (this: ShikiTransformerContextCommon, markdown: string) => ElementContent[];
201
+ /**
202
+ * Custom function to render inline markdown.
203
+ *
204
+ * By default it pass-through the markdown.
205
+ */
206
+ renderMarkdownInline?: (this: ShikiTransformerContextCommon, markdown: string, context: string) => ElementContent[];
207
+ /**
208
+ * The way query should be rendered.
209
+ * - `'popup'`: Render the query in the absolute popup
210
+ * - `'line'`: Render the query line after the line of code
211
+ * @default 'popup'
212
+ */
213
+ queryRendering?: 'popup' | 'line';
214
+ /**
215
+ * Extensions for the generated HAST tree.
216
+ */
217
+ hast?: {
218
+ /**
219
+ * The <code> block for in the hover popup.
220
+ */
221
+ popupTypes?: HastExtension;
222
+ /**
223
+ * The documentation block in the hover popup. Can be markdown rendered if `renderMarkdown` is provided.
224
+ */
225
+ popupDocs?: HastExtension;
137
226
  /**
138
- * Render JSDoc comments in hover popup.
139
- *
140
- * @default true
227
+ * The container of jsdoc tags in the hover popup.
141
228
  */
142
- jsdoc?: boolean;
229
+ popupDocsTags?: HastExtension;
143
230
  /**
144
- * Custom icons for completion items.
145
- * A map from completion item kind to a HAST node.
146
- *
147
- * If `false`, no icons will be rendered.
148
- * @default defaultCompletionIcons
231
+ * The token for the hover informaton.
149
232
  */
150
- completionIcons?: Partial<Record<string, ElementContent>> | false;
233
+ hoverToken?: HastExtension;
151
234
  /**
152
- * Custom icons for custom tags lines.
153
- * A map from tag name to a HAST node.
154
- *
155
- * If `false`, no icons will be rendered.
156
- * @default defaultCustomTagIcons
235
+ * The container of the hover popup.
157
236
  */
158
- customTagIcons?: Partial<Record<string, ElementContent>> | false;
237
+ hoverPopup?: HastExtension;
159
238
  /**
160
- * Custom formatter for the type info text.
161
- * Note that it might not be valid TypeScript syntax.
162
- *
163
- * @default defaultHoverInfoProcessor
239
+ * The container of error popup.
164
240
  */
165
- processHoverInfo?: (info: string) => string;
241
+ popupError?: HastExtension;
166
242
  /**
167
- * Custom formatter for the docs text (can be markdown).
168
- *
169
- * @default undefined
243
+ * Custom function to compose the hover token.
170
244
  */
171
- processHoverDocs?: (docs: string) => string;
245
+ hoverCompose?: (parts: {
246
+ popup: Element;
247
+ token: Text | Element;
248
+ }) => ElementContent[];
172
249
  /**
173
- * The way errors should be rendered.
174
- *
175
- * - `'line'`: Render the error line after the line of code
176
- * - `'hover'`: Render the error in the hover popup
177
- *
178
- * @default 'line'
250
+ * The token for the query informaton.
179
251
  */
180
- errorRendering?: 'line' | 'hover';
252
+ queryToken?: HastExtension;
181
253
  /**
182
- * Classes added to injected elements
254
+ * The container of the query popup.
183
255
  */
184
- classExtra?: string;
256
+ queryPopup?: HastExtension;
185
257
  /**
186
- * Language for syntax highlight.
187
- * @default the language of the code block
258
+ * Custom function to compose the hover token.
188
259
  */
189
- lang?: string;
260
+ queryCompose?: (parts: {
261
+ popup: Element;
262
+ token: Text | Element;
263
+ }) => ElementContent[];
190
264
  /**
191
- * Custom function to render markdown.
192
- *
193
- * By default it pass-through the markdown.
265
+ * The token for the completion informaton.
194
266
  */
195
- renderMarkdown?: (this: ShikiTransformerContextCommon, markdown: string) => ElementContent[];
267
+ completionToken?: HastExtension;
196
268
  /**
197
- * Custom function to render inline markdown.
198
- *
199
- * By default it pass-through the markdown.
269
+ * The cursor element in the completion popup.
200
270
  */
201
- renderMarkdownInline?: (this: ShikiTransformerContextCommon, markdown: string, context: string) => ElementContent[];
271
+ completionCursor?: HastExtension;
202
272
  /**
203
- * The way query should be rendered.
204
- * - `'popup'`: Render the query in the absolute popup
205
- * - `'line'`: Render the query line after the line of code
206
- * @default 'popup'
273
+ * The container of the completion popup.
207
274
  */
208
- queryRendering?: 'popup' | 'line';
275
+ completionPopup?: HastExtension;
209
276
  /**
210
- * Extensions for the generated HAST tree.
277
+ * Custom function to compose the completion token.
211
278
  */
212
- hast?: {
213
- /**
214
- * The <code> block for in the hover popup.
215
- */
216
- popupTypes?: HastExtension;
217
- /**
218
- * The documentation block in the hover popup. Can be markdown rendered if `renderMarkdown` is provided.
219
- */
220
- popupDocs?: HastExtension;
221
- /**
222
- * The container of jsdoc tags in the hover popup.
223
- */
224
- popupDocsTags?: HastExtension;
225
- /**
226
- * The token for the hover informaton.
227
- */
228
- hoverToken?: HastExtension;
229
- /**
230
- * The container of the hover popup.
231
- */
232
- hoverPopup?: HastExtension;
233
- /**
234
- * The container of error popup.
235
- */
236
- popupError?: HastExtension;
237
- /**
238
- * Custom function to compose the hover token.
239
- */
240
- hoverCompose?: (parts: {
241
- popup: Element;
242
- token: Text | Element;
243
- }) => ElementContent[];
244
- /**
245
- * The token for the query informaton.
246
- */
247
- queryToken?: HastExtension;
248
- /**
249
- * The container of the query popup.
250
- */
251
- queryPopup?: HastExtension;
252
- /**
253
- * Custom function to compose the hover token.
254
- */
255
- queryCompose?: (parts: {
256
- popup: Element;
257
- token: Text | Element;
258
- }) => ElementContent[];
259
- /**
260
- * The token for the completion informaton.
261
- */
262
- completionToken?: HastExtension;
263
- /**
264
- * The cursor element in the completion popup.
265
- */
266
- completionCursor?: HastExtension;
267
- /**
268
- * The container of the completion popup.
269
- */
270
- completionPopup?: HastExtension;
271
- /**
272
- * Custom function to compose the completion token.
273
- */
274
- completionCompose?: (parts: {
275
- popup: Element;
276
- cursor: Element;
277
- }) => ElementContent[];
278
- /**
279
- * The token for the error informaton.
280
- */
281
- errorToken?: HastExtension;
282
- /**
283
- * The container of the error popup.
284
- * Only used when `errorRendering` is set to `'hover'`.
285
- */
286
- errorPopup?: HastExtension;
287
- /**
288
- * Custom function to compose the error token.
289
- * Only used when `errorRendering` is set to `'hover'`.
290
- */
291
- errorCompose?: (parts: {
292
- popup: Element;
293
- token: Text | Element;
294
- }) => ElementContent[];
295
- /**
296
- * The wrapper for the highlighted nodes.
297
- */
298
- nodesHighlight?: HastExtension;
299
- };
279
+ completionCompose?: (parts: {
280
+ popup: Element;
281
+ cursor: Element;
282
+ }) => ElementContent[];
283
+ /**
284
+ * The token for the error informaton.
285
+ */
286
+ errorToken?: HastExtension;
287
+ /**
288
+ * The container of the error popup.
289
+ * Only used when `errorRendering` is set to `'hover'`.
290
+ */
291
+ errorPopup?: HastExtension;
292
+ /**
293
+ * Custom function to compose the error token.
294
+ * Only used when `errorRendering` is set to `'hover'`.
295
+ */
296
+ errorCompose?: (parts: {
297
+ popup: Element;
298
+ token: Text | Element;
299
+ }) => ElementContent[];
300
+ /**
301
+ * The wrapper for the highlighted nodes.
302
+ */
303
+ nodesHighlight?: HastExtension;
304
+ };
300
305
  }
301
306
  interface HastExtension {
302
- tagName?: string;
303
- properties?: Element['properties'];
304
- class?: string;
305
- children?: (input: ElementContent[]) => ElementContent[];
307
+ tagName?: string;
308
+ properties?: Element['properties'];
309
+ class?: string;
310
+ children?: (input: ElementContent[]) => ElementContent[];
306
311
  }
307
312
  /**
308
313
  * An alternative renderer that providers better prefixed class names,
@@ -313,14 +318,9 @@ declare function rendererRich(options?: RendererRichOptions): TwoslashRenderer;
313
318
  * The default hover info processor, which will do some basic cleanup
314
319
  */
315
320
  declare function defaultHoverInfoProcessor(type: string): string;
316
-
317
- /**
318
- * This file is the core of the @shikijs/twoslash package,
319
- * Decoupled from twoslash's implementation and allowing to introduce custom implementation or cache system.
320
- */
321
-
321
+ //#endregion
322
+ //#region src/core.d.ts
322
323
  declare function defaultTwoslashOptions(): TwoslashExecuteOptions;
323
324
  declare function createTransformerFactory(defaultTwoslasher: TwoslashShikiFunction | TwoslashGenericFunction, defaultRenderer?: TwoslashRenderer): (options?: TransformerTwoslashOptions) => ShikiTransformer;
324
-
325
- export { ShikiTwoslashError, createTransformerFactory, defaultCompletionIcons, defaultCustomTagIcons, defaultHoverInfoProcessor, defaultTwoslashOptions, rendererClassic, rendererRich };
326
- export type { CompletionItem, HastExtension, RendererRichOptions, TransformerTwoslashOptions, TwoslashRenderer, TwoslashShikiFunction, TwoslashShikiReturn, TwoslashTypesCache };
325
+ //#endregion
326
+ export { CompletionItem, HastExtension, RendererRichOptions, ShikiTwoslashError, TransformerTwoslashOptions, TwoslashRenderer, TwoslashShikiFunction, TwoslashShikiReturn, TwoslashTypesCache, createTransformerFactory, defaultCompletionIcons, defaultCustomTagIcons, defaultHoverInfoProcessor, defaultTwoslashOptions, rendererClassic, rendererRich };