@shikijs/twoslash 1.0.0-beta.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/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2021 Pine Wu
4
+ Copyright (c) 2023 Anthony Fu <https://github.com/antfu>
5
+
6
+ Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ of this software and associated documentation files (the "Software"), to deal
8
+ in the Software without restriction, including without limitation the rights
9
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ copies of the Software, and to permit persons to whom the Software is
11
+ furnished to do so, subject to the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be included in all
14
+ copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,9 @@
1
+ # @shikijs/twoslash
2
+
3
+ A [shiki](https://github.com/shikijs/shiki) transformer for [TypeScript Twoslash](https://www.typescriptlang.org/dev/twoslash/).
4
+
5
+ [Documentations](https://shiki.style/packages/twoslash)
6
+
7
+ ## License
8
+
9
+ MIT
@@ -0,0 +1,230 @@
1
+ import { TwoslashReturn, twoslasher, TwoslashOptions, NodeError, NodeTag, NodeQuery, NodeCompletion, NodeHover, NodeHighlight, TwoslashExecuteOptions } from 'twoslash';
2
+ import { CodeToHastOptions, ShikiTransformerContext, ShikiTransformerContextCommon, ShikiTransformer } from '@shikijs/core';
3
+ import { ElementContent, Element, Text } from 'hast';
4
+
5
+ declare module '@shikijs/core' {
6
+ interface ShikiTransformerContextMeta {
7
+ twoslash?: TwoslashReturn;
8
+ }
9
+ }
10
+ interface TransformerTwoslashOptions {
11
+ /**
12
+ * Languages to apply this transformer to
13
+ */
14
+ langs?: string[];
15
+ /**
16
+ * Requires `twoslash` to be presented in the code block meta to apply this transformer
17
+ *
18
+ * @default false
19
+ */
20
+ explicitTrigger?: boolean;
21
+ /**
22
+ * Mapping from language alias to language name
23
+ */
24
+ langAlias?: Record<string, string>;
25
+ /**
26
+ * Custom filter function to apply this transformer to
27
+ * When specified, `langs` and `explicitTrigger` will be ignored
28
+ */
29
+ filter?: (lang: string, code: string, options: CodeToHastOptions) => boolean;
30
+ /**
31
+ * Custom instance of twoslasher function
32
+ */
33
+ twoslasher?: typeof twoslasher;
34
+ /**
35
+ * Options to pass to twoslash
36
+ */
37
+ twoslashOptions?: TwoslashOptions;
38
+ /**
39
+ * Custom renderers to decide how each info should be rendered
40
+ */
41
+ renderer?: TwoslashRenderer;
42
+ /**
43
+ * Strictly throw when there is an error
44
+ * @default true
45
+ */
46
+ throws?: boolean;
47
+ }
48
+ interface TwoslashRenderer {
49
+ lineError?(this: ShikiTransformerContext, error: NodeError): ElementContent[];
50
+ lineCustomTag?(this: ShikiTransformerContext, tag: NodeTag): ElementContent[];
51
+ lineQuery?(this: ShikiTransformerContext, query: NodeQuery, targetNode?: Element | Text): ElementContent[];
52
+ lineCompletion?(this: ShikiTransformerContext, query: NodeCompletion): ElementContent[];
53
+ nodeStaticInfo(this: ShikiTransformerContext, info: NodeHover, node: Element | Text): Partial<ElementContent>;
54
+ nodeError?(this: ShikiTransformerContext, error: NodeError, node: Element | Text): Partial<ElementContent>;
55
+ nodeQuery?(this: ShikiTransformerContext, query: NodeQuery, node: Element | Text): Partial<ElementContent>;
56
+ nodeCompletion?(this: ShikiTransformerContext, query: NodeCompletion, node: Element | Text): Partial<ElementContent>;
57
+ nodesHighlight?(this: ShikiTransformerContext, highlight: NodeHighlight, nodes: ElementContent[]): ElementContent[];
58
+ }
59
+
60
+ type CompletionItem = NonNullable<NodeCompletion['completions']>[number];
61
+ declare const defaultCompletionIcons: Record<CompletionItem['kind'], Element | undefined>;
62
+ declare const defaultCustomTagIcons: Record<string, Element | undefined>;
63
+
64
+ interface RendererRichOptions {
65
+ /**
66
+ * Render JSDoc comments in hover popup.
67
+ *
68
+ * @default true
69
+ */
70
+ jsdoc?: boolean;
71
+ /**
72
+ * Custom icons for completion items.
73
+ * A map from completion item kind to a HAST node.
74
+ *
75
+ * If `false`, no icons will be rendered.
76
+ * @default defaultCompletionIcons
77
+ */
78
+ completionIcons?: Partial<Record<CompletionItem['kind'], ElementContent>> | false;
79
+ /**
80
+ * Custom icons for custom tags lines.
81
+ * A map from tag name to a HAST node.
82
+ *
83
+ * If `false`, no icons will be rendered.
84
+ * @default defaultCustomTagIcons
85
+ */
86
+ customTagIcons?: Partial<Record<string, ElementContent>> | false;
87
+ /**
88
+ * Custom formatter for the type info text.
89
+ * Note that it might not be valid TypeScript syntax.
90
+ *
91
+ * @default defaultHoverInfoProcessor
92
+ */
93
+ processHoverInfo?: (info: string) => string;
94
+ /**
95
+ * Custom formatter for the docs text (can be markdown).
96
+ *
97
+ * @default undefined
98
+ */
99
+ processHoverDocs?: (docs: string) => string;
100
+ /**
101
+ * Classes added to injected elements
102
+ */
103
+ classExtra?: string;
104
+ /**
105
+ * Language for syntax highlight.
106
+ * @default the language of the code block
107
+ */
108
+ lang?: string;
109
+ /**
110
+ * @deprecated Use `processHoverInfo` instead.
111
+ */
112
+ formatInfo?(info: string): string;
113
+ /**
114
+ * Custom function to render markdown.
115
+ *
116
+ * By default it pass-through the markdown.
117
+ */
118
+ renderMarkdown?(this: ShikiTransformerContextCommon, markdown: string): ElementContent[];
119
+ /**
120
+ * Custom function to render inline markdown.
121
+ *
122
+ * By default it pass-through the markdown.
123
+ */
124
+ renderMarkdownInline?(this: ShikiTransformerContextCommon, markdown: string, context: string): ElementContent[];
125
+ /**
126
+ * Extensions for the genreated HAST tree.
127
+ */
128
+ hast?: {
129
+ /**
130
+ * The <code> block for in the hover popup.
131
+ */
132
+ popupTypes?: HastExtension;
133
+ /**
134
+ * The documentation block in the hover popup. Can be markdown rendered if `renderMarkdown` is provided.
135
+ */
136
+ popupDocs?: HastExtension;
137
+ /**
138
+ * The container of jsdoc tags in the hover popup.
139
+ */
140
+ popupDocsTags?: HastExtension;
141
+ /**
142
+ * The token for the hover informaton.
143
+ */
144
+ hoverToken?: HastExtension;
145
+ /**
146
+ * The container of the hover popup.
147
+ */
148
+ hoverPopup?: HastExtension;
149
+ /**
150
+ * Custom function to compose the hover token.
151
+ */
152
+ hoverCompose?: (parts: {
153
+ popup: Element;
154
+ token: Text | Element;
155
+ }) => ElementContent[];
156
+ /**
157
+ * The token for the query informaton.
158
+ */
159
+ queryToken?: HastExtension;
160
+ /**
161
+ * The container of the query popup.
162
+ */
163
+ queryPopup?: HastExtension;
164
+ /**
165
+ * Custom function to compose the hover token.
166
+ */
167
+ queryCompose?: (parts: {
168
+ popup: Element;
169
+ token: Text | Element;
170
+ }) => ElementContent[];
171
+ /**
172
+ * The token for the completion informaton.
173
+ */
174
+ completionToken?: HastExtension;
175
+ /**
176
+ * The cursor element in the completion popup.
177
+ */
178
+ completionCursor?: HastExtension;
179
+ /**
180
+ * The container of the completion popup.
181
+ */
182
+ completionPopup?: HastExtension;
183
+ /**
184
+ * Custom function to compose the completion token.
185
+ */
186
+ completionCompose?: (parts: {
187
+ popup: Element;
188
+ cursor: Element;
189
+ }) => ElementContent[];
190
+ /**
191
+ * The token for the error informaton.
192
+ */
193
+ errorToken?: HastExtension;
194
+ /**
195
+ * The wrapper for the highlighted nodes.
196
+ */
197
+ nodesHighlight?: HastExtension;
198
+ };
199
+ }
200
+ interface HastExtension {
201
+ tagName?: string;
202
+ properties?: Element['properties'];
203
+ class?: string;
204
+ children?: (input: ElementContent[]) => ElementContent[];
205
+ }
206
+ /**
207
+ * An alternative renderer that providers better prefixed class names,
208
+ * with syntax highlight for the info text.
209
+ */
210
+ declare function rendererRich(options?: RendererRichOptions): TwoslashRenderer;
211
+ /**
212
+ * The default hover info processor, which will do some basic cleanup
213
+ */
214
+ declare function defaultHoverInfoProcessor(type: string): string;
215
+
216
+ /**
217
+ * The default renderer aligning with the original `@shikijs/twoslash` output.
218
+ */
219
+ declare function rendererClassic(): TwoslashRenderer;
220
+
221
+ /**
222
+ * This file is the core of the @shikijs/twoslash package,
223
+ * Decoupled from twoslash's implementation and allowing to introduce custom implementation or cache system.
224
+ */
225
+
226
+ declare function defaultTwoslashOptions(): TwoslashExecuteOptions;
227
+ type TwoslashFunction = (code: string, lang?: string, options?: TwoslashExecuteOptions) => TwoslashReturn;
228
+ declare function createTransformerFactory(defaultTwoslasher: TwoslashFunction, defaultRenderer?: TwoslashRenderer): (options?: TransformerTwoslashOptions) => ShikiTransformer;
229
+
230
+ export { type CompletionItem, type HastExtension, type RendererRichOptions, type TransformerTwoslashOptions, type TwoslashRenderer, createTransformerFactory, defaultCompletionIcons, defaultCustomTagIcons, defaultHoverInfoProcessor, defaultTwoslashOptions, rendererClassic, rendererRich };
package/dist/core.d.ts ADDED
@@ -0,0 +1,230 @@
1
+ import { TwoslashReturn, twoslasher, TwoslashOptions, NodeError, NodeTag, NodeQuery, NodeCompletion, NodeHover, NodeHighlight, TwoslashExecuteOptions } from 'twoslash';
2
+ import { CodeToHastOptions, ShikiTransformerContext, ShikiTransformerContextCommon, ShikiTransformer } from '@shikijs/core';
3
+ import { ElementContent, Element, Text } from 'hast';
4
+
5
+ declare module '@shikijs/core' {
6
+ interface ShikiTransformerContextMeta {
7
+ twoslash?: TwoslashReturn;
8
+ }
9
+ }
10
+ interface TransformerTwoslashOptions {
11
+ /**
12
+ * Languages to apply this transformer to
13
+ */
14
+ langs?: string[];
15
+ /**
16
+ * Requires `twoslash` to be presented in the code block meta to apply this transformer
17
+ *
18
+ * @default false
19
+ */
20
+ explicitTrigger?: boolean;
21
+ /**
22
+ * Mapping from language alias to language name
23
+ */
24
+ langAlias?: Record<string, string>;
25
+ /**
26
+ * Custom filter function to apply this transformer to
27
+ * When specified, `langs` and `explicitTrigger` will be ignored
28
+ */
29
+ filter?: (lang: string, code: string, options: CodeToHastOptions) => boolean;
30
+ /**
31
+ * Custom instance of twoslasher function
32
+ */
33
+ twoslasher?: typeof twoslasher;
34
+ /**
35
+ * Options to pass to twoslash
36
+ */
37
+ twoslashOptions?: TwoslashOptions;
38
+ /**
39
+ * Custom renderers to decide how each info should be rendered
40
+ */
41
+ renderer?: TwoslashRenderer;
42
+ /**
43
+ * Strictly throw when there is an error
44
+ * @default true
45
+ */
46
+ throws?: boolean;
47
+ }
48
+ interface TwoslashRenderer {
49
+ lineError?(this: ShikiTransformerContext, error: NodeError): ElementContent[];
50
+ lineCustomTag?(this: ShikiTransformerContext, tag: NodeTag): ElementContent[];
51
+ lineQuery?(this: ShikiTransformerContext, query: NodeQuery, targetNode?: Element | Text): ElementContent[];
52
+ lineCompletion?(this: ShikiTransformerContext, query: NodeCompletion): ElementContent[];
53
+ nodeStaticInfo(this: ShikiTransformerContext, info: NodeHover, node: Element | Text): Partial<ElementContent>;
54
+ nodeError?(this: ShikiTransformerContext, error: NodeError, node: Element | Text): Partial<ElementContent>;
55
+ nodeQuery?(this: ShikiTransformerContext, query: NodeQuery, node: Element | Text): Partial<ElementContent>;
56
+ nodeCompletion?(this: ShikiTransformerContext, query: NodeCompletion, node: Element | Text): Partial<ElementContent>;
57
+ nodesHighlight?(this: ShikiTransformerContext, highlight: NodeHighlight, nodes: ElementContent[]): ElementContent[];
58
+ }
59
+
60
+ type CompletionItem = NonNullable<NodeCompletion['completions']>[number];
61
+ declare const defaultCompletionIcons: Record<CompletionItem['kind'], Element | undefined>;
62
+ declare const defaultCustomTagIcons: Record<string, Element | undefined>;
63
+
64
+ interface RendererRichOptions {
65
+ /**
66
+ * Render JSDoc comments in hover popup.
67
+ *
68
+ * @default true
69
+ */
70
+ jsdoc?: boolean;
71
+ /**
72
+ * Custom icons for completion items.
73
+ * A map from completion item kind to a HAST node.
74
+ *
75
+ * If `false`, no icons will be rendered.
76
+ * @default defaultCompletionIcons
77
+ */
78
+ completionIcons?: Partial<Record<CompletionItem['kind'], ElementContent>> | false;
79
+ /**
80
+ * Custom icons for custom tags lines.
81
+ * A map from tag name to a HAST node.
82
+ *
83
+ * If `false`, no icons will be rendered.
84
+ * @default defaultCustomTagIcons
85
+ */
86
+ customTagIcons?: Partial<Record<string, ElementContent>> | false;
87
+ /**
88
+ * Custom formatter for the type info text.
89
+ * Note that it might not be valid TypeScript syntax.
90
+ *
91
+ * @default defaultHoverInfoProcessor
92
+ */
93
+ processHoverInfo?: (info: string) => string;
94
+ /**
95
+ * Custom formatter for the docs text (can be markdown).
96
+ *
97
+ * @default undefined
98
+ */
99
+ processHoverDocs?: (docs: string) => string;
100
+ /**
101
+ * Classes added to injected elements
102
+ */
103
+ classExtra?: string;
104
+ /**
105
+ * Language for syntax highlight.
106
+ * @default the language of the code block
107
+ */
108
+ lang?: string;
109
+ /**
110
+ * @deprecated Use `processHoverInfo` instead.
111
+ */
112
+ formatInfo?(info: string): string;
113
+ /**
114
+ * Custom function to render markdown.
115
+ *
116
+ * By default it pass-through the markdown.
117
+ */
118
+ renderMarkdown?(this: ShikiTransformerContextCommon, markdown: string): ElementContent[];
119
+ /**
120
+ * Custom function to render inline markdown.
121
+ *
122
+ * By default it pass-through the markdown.
123
+ */
124
+ renderMarkdownInline?(this: ShikiTransformerContextCommon, markdown: string, context: string): ElementContent[];
125
+ /**
126
+ * Extensions for the genreated HAST tree.
127
+ */
128
+ hast?: {
129
+ /**
130
+ * The <code> block for in the hover popup.
131
+ */
132
+ popupTypes?: HastExtension;
133
+ /**
134
+ * The documentation block in the hover popup. Can be markdown rendered if `renderMarkdown` is provided.
135
+ */
136
+ popupDocs?: HastExtension;
137
+ /**
138
+ * The container of jsdoc tags in the hover popup.
139
+ */
140
+ popupDocsTags?: HastExtension;
141
+ /**
142
+ * The token for the hover informaton.
143
+ */
144
+ hoverToken?: HastExtension;
145
+ /**
146
+ * The container of the hover popup.
147
+ */
148
+ hoverPopup?: HastExtension;
149
+ /**
150
+ * Custom function to compose the hover token.
151
+ */
152
+ hoverCompose?: (parts: {
153
+ popup: Element;
154
+ token: Text | Element;
155
+ }) => ElementContent[];
156
+ /**
157
+ * The token for the query informaton.
158
+ */
159
+ queryToken?: HastExtension;
160
+ /**
161
+ * The container of the query popup.
162
+ */
163
+ queryPopup?: HastExtension;
164
+ /**
165
+ * Custom function to compose the hover token.
166
+ */
167
+ queryCompose?: (parts: {
168
+ popup: Element;
169
+ token: Text | Element;
170
+ }) => ElementContent[];
171
+ /**
172
+ * The token for the completion informaton.
173
+ */
174
+ completionToken?: HastExtension;
175
+ /**
176
+ * The cursor element in the completion popup.
177
+ */
178
+ completionCursor?: HastExtension;
179
+ /**
180
+ * The container of the completion popup.
181
+ */
182
+ completionPopup?: HastExtension;
183
+ /**
184
+ * Custom function to compose the completion token.
185
+ */
186
+ completionCompose?: (parts: {
187
+ popup: Element;
188
+ cursor: Element;
189
+ }) => ElementContent[];
190
+ /**
191
+ * The token for the error informaton.
192
+ */
193
+ errorToken?: HastExtension;
194
+ /**
195
+ * The wrapper for the highlighted nodes.
196
+ */
197
+ nodesHighlight?: HastExtension;
198
+ };
199
+ }
200
+ interface HastExtension {
201
+ tagName?: string;
202
+ properties?: Element['properties'];
203
+ class?: string;
204
+ children?: (input: ElementContent[]) => ElementContent[];
205
+ }
206
+ /**
207
+ * An alternative renderer that providers better prefixed class names,
208
+ * with syntax highlight for the info text.
209
+ */
210
+ declare function rendererRich(options?: RendererRichOptions): TwoslashRenderer;
211
+ /**
212
+ * The default hover info processor, which will do some basic cleanup
213
+ */
214
+ declare function defaultHoverInfoProcessor(type: string): string;
215
+
216
+ /**
217
+ * The default renderer aligning with the original `@shikijs/twoslash` output.
218
+ */
219
+ declare function rendererClassic(): TwoslashRenderer;
220
+
221
+ /**
222
+ * This file is the core of the @shikijs/twoslash package,
223
+ * Decoupled from twoslash's implementation and allowing to introduce custom implementation or cache system.
224
+ */
225
+
226
+ declare function defaultTwoslashOptions(): TwoslashExecuteOptions;
227
+ type TwoslashFunction = (code: string, lang?: string, options?: TwoslashExecuteOptions) => TwoslashReturn;
228
+ declare function createTransformerFactory(defaultTwoslasher: TwoslashFunction, defaultRenderer?: TwoslashRenderer): (options?: TransformerTwoslashOptions) => ShikiTransformer;
229
+
230
+ export { type CompletionItem, type HastExtension, type RendererRichOptions, type TransformerTwoslashOptions, type TwoslashRenderer, createTransformerFactory, defaultCompletionIcons, defaultCustomTagIcons, defaultHoverInfoProcessor, defaultTwoslashOptions, rendererClassic, rendererRich };