@shikijs/twoslash 1.0.0-beta.6 → 1.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 +10 -9
- package/dist/core.d.ts +10 -9
- package/dist/core.mjs +72 -50
- package/dist/index.d.mts +1 -1
- package/dist/index.d.ts +1 -1
- package/package.json +3 -3
package/dist/core.d.mts
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
|
-
import { TwoslashReturn,
|
|
1
|
+
import { TwoslashReturn, TwoslashExecuteOptions, TwoslashOptions, NodeError, NodeTag, NodeQuery, NodeCompletion, NodeHover, NodeHighlight } from 'twoslash';
|
|
2
2
|
import { CodeToHastOptions, ShikiTransformerContext, ShikiTransformerContextCommon, ShikiTransformer } from '@shikijs/core';
|
|
3
3
|
import { ElementContent, Element, Text } from 'hast';
|
|
4
4
|
|
|
5
5
|
type TwoslashShikiReturn = Pick<TwoslashReturn, 'nodes' | 'code'> & {
|
|
6
6
|
meta?: Partial<Pick<TwoslashReturn['meta'], 'extension'>>;
|
|
7
7
|
};
|
|
8
|
+
type TwoslashShikiFunction = (code: string, lang?: string, options?: TwoslashExecuteOptions) => TwoslashShikiReturn;
|
|
8
9
|
declare module '@shikijs/core' {
|
|
9
10
|
interface ShikiTransformerContextMeta {
|
|
10
11
|
twoslash?: TwoslashShikiReturn;
|
|
@@ -33,7 +34,7 @@ interface TransformerTwoslashOptions {
|
|
|
33
34
|
/**
|
|
34
35
|
* Custom instance of twoslasher function
|
|
35
36
|
*/
|
|
36
|
-
twoslasher?:
|
|
37
|
+
twoslasher?: TwoslashShikiFunction;
|
|
37
38
|
/**
|
|
38
39
|
* Options to pass to twoslash
|
|
39
40
|
*/
|
|
@@ -57,6 +58,7 @@ interface TwoslashRenderer {
|
|
|
57
58
|
nodeError?: (this: ShikiTransformerContext, error: NodeError, node: Element | Text) => Partial<ElementContent>;
|
|
58
59
|
nodeQuery?: (this: ShikiTransformerContext, query: NodeQuery, node: Element | Text) => Partial<ElementContent>;
|
|
59
60
|
nodeCompletion?: (this: ShikiTransformerContext, query: NodeCompletion, node: Element | Text) => Partial<ElementContent>;
|
|
61
|
+
nodesError?: (this: ShikiTransformerContext, error: NodeError, nodes: ElementContent[]) => ElementContent[];
|
|
60
62
|
nodesHighlight?: (this: ShikiTransformerContext, highlight: NodeHighlight, nodes: ElementContent[]) => ElementContent[];
|
|
61
63
|
}
|
|
62
64
|
|
|
@@ -118,10 +120,6 @@ interface RendererRichOptions {
|
|
|
118
120
|
* @default the language of the code block
|
|
119
121
|
*/
|
|
120
122
|
lang?: string;
|
|
121
|
-
/**
|
|
122
|
-
* @deprecated Use `processHoverInfo` instead.
|
|
123
|
-
*/
|
|
124
|
-
formatInfo?: (info: string) => string;
|
|
125
123
|
/**
|
|
126
124
|
* Custom function to render markdown.
|
|
127
125
|
*
|
|
@@ -158,6 +156,10 @@ interface RendererRichOptions {
|
|
|
158
156
|
* The container of the hover popup.
|
|
159
157
|
*/
|
|
160
158
|
hoverPopup?: HastExtension;
|
|
159
|
+
/**
|
|
160
|
+
* The container of error popup.
|
|
161
|
+
*/
|
|
162
|
+
popupError?: HastExtension;
|
|
161
163
|
/**
|
|
162
164
|
* Custom function to compose the hover token.
|
|
163
165
|
*/
|
|
@@ -253,7 +255,6 @@ declare class ShikiTwoslashError extends Error {
|
|
|
253
255
|
*/
|
|
254
256
|
|
|
255
257
|
declare function defaultTwoslashOptions(): TwoslashExecuteOptions;
|
|
256
|
-
|
|
257
|
-
declare function createTransformerFactory(defaultTwoslasher: TwoslashFunction, defaultRenderer?: TwoslashRenderer): (options?: TransformerTwoslashOptions) => ShikiTransformer;
|
|
258
|
+
declare function createTransformerFactory(defaultTwoslasher: TwoslashShikiFunction, defaultRenderer?: TwoslashRenderer): (options?: TransformerTwoslashOptions) => ShikiTransformer;
|
|
258
259
|
|
|
259
|
-
export { type CompletionItem, type HastExtension, type RendererRichOptions, ShikiTwoslashError, type TransformerTwoslashOptions, type
|
|
260
|
+
export { type CompletionItem, type HastExtension, type RendererRichOptions, ShikiTwoslashError, type TransformerTwoslashOptions, type TwoslashRenderer, type TwoslashShikiFunction, type TwoslashShikiReturn, createTransformerFactory, defaultCompletionIcons, defaultCustomTagIcons, defaultHoverInfoProcessor, defaultTwoslashOptions, rendererClassic, rendererRich };
|
package/dist/core.d.ts
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
|
-
import { TwoslashReturn,
|
|
1
|
+
import { TwoslashReturn, TwoslashExecuteOptions, TwoslashOptions, NodeError, NodeTag, NodeQuery, NodeCompletion, NodeHover, NodeHighlight } from 'twoslash';
|
|
2
2
|
import { CodeToHastOptions, ShikiTransformerContext, ShikiTransformerContextCommon, ShikiTransformer } from '@shikijs/core';
|
|
3
3
|
import { ElementContent, Element, Text } from 'hast';
|
|
4
4
|
|
|
5
5
|
type TwoslashShikiReturn = Pick<TwoslashReturn, 'nodes' | 'code'> & {
|
|
6
6
|
meta?: Partial<Pick<TwoslashReturn['meta'], 'extension'>>;
|
|
7
7
|
};
|
|
8
|
+
type TwoslashShikiFunction = (code: string, lang?: string, options?: TwoslashExecuteOptions) => TwoslashShikiReturn;
|
|
8
9
|
declare module '@shikijs/core' {
|
|
9
10
|
interface ShikiTransformerContextMeta {
|
|
10
11
|
twoslash?: TwoslashShikiReturn;
|
|
@@ -33,7 +34,7 @@ interface TransformerTwoslashOptions {
|
|
|
33
34
|
/**
|
|
34
35
|
* Custom instance of twoslasher function
|
|
35
36
|
*/
|
|
36
|
-
twoslasher?:
|
|
37
|
+
twoslasher?: TwoslashShikiFunction;
|
|
37
38
|
/**
|
|
38
39
|
* Options to pass to twoslash
|
|
39
40
|
*/
|
|
@@ -57,6 +58,7 @@ interface TwoslashRenderer {
|
|
|
57
58
|
nodeError?: (this: ShikiTransformerContext, error: NodeError, node: Element | Text) => Partial<ElementContent>;
|
|
58
59
|
nodeQuery?: (this: ShikiTransformerContext, query: NodeQuery, node: Element | Text) => Partial<ElementContent>;
|
|
59
60
|
nodeCompletion?: (this: ShikiTransformerContext, query: NodeCompletion, node: Element | Text) => Partial<ElementContent>;
|
|
61
|
+
nodesError?: (this: ShikiTransformerContext, error: NodeError, nodes: ElementContent[]) => ElementContent[];
|
|
60
62
|
nodesHighlight?: (this: ShikiTransformerContext, highlight: NodeHighlight, nodes: ElementContent[]) => ElementContent[];
|
|
61
63
|
}
|
|
62
64
|
|
|
@@ -118,10 +120,6 @@ interface RendererRichOptions {
|
|
|
118
120
|
* @default the language of the code block
|
|
119
121
|
*/
|
|
120
122
|
lang?: string;
|
|
121
|
-
/**
|
|
122
|
-
* @deprecated Use `processHoverInfo` instead.
|
|
123
|
-
*/
|
|
124
|
-
formatInfo?: (info: string) => string;
|
|
125
123
|
/**
|
|
126
124
|
* Custom function to render markdown.
|
|
127
125
|
*
|
|
@@ -158,6 +156,10 @@ interface RendererRichOptions {
|
|
|
158
156
|
* The container of the hover popup.
|
|
159
157
|
*/
|
|
160
158
|
hoverPopup?: HastExtension;
|
|
159
|
+
/**
|
|
160
|
+
* The container of error popup.
|
|
161
|
+
*/
|
|
162
|
+
popupError?: HastExtension;
|
|
161
163
|
/**
|
|
162
164
|
* Custom function to compose the hover token.
|
|
163
165
|
*/
|
|
@@ -253,7 +255,6 @@ declare class ShikiTwoslashError extends Error {
|
|
|
253
255
|
*/
|
|
254
256
|
|
|
255
257
|
declare function defaultTwoslashOptions(): TwoslashExecuteOptions;
|
|
256
|
-
|
|
257
|
-
declare function createTransformerFactory(defaultTwoslasher: TwoslashFunction, defaultRenderer?: TwoslashRenderer): (options?: TransformerTwoslashOptions) => ShikiTransformer;
|
|
258
|
+
declare function createTransformerFactory(defaultTwoslasher: TwoslashShikiFunction, defaultRenderer?: TwoslashRenderer): (options?: TransformerTwoslashOptions) => ShikiTransformer;
|
|
258
259
|
|
|
259
|
-
export { type CompletionItem, type HastExtension, type RendererRichOptions, ShikiTwoslashError, type TransformerTwoslashOptions, type
|
|
260
|
+
export { type CompletionItem, type HastExtension, type RendererRichOptions, ShikiTwoslashError, type TransformerTwoslashOptions, type TwoslashRenderer, type TwoslashShikiFunction, type TwoslashShikiReturn, createTransformerFactory, defaultCompletionIcons, defaultCustomTagIcons, defaultHoverInfoProcessor, defaultTwoslashOptions, rendererClassic, rendererRich };
|
package/dist/core.mjs
CHANGED
|
@@ -399,8 +399,7 @@ function rendererRich(options = {}) {
|
|
|
399
399
|
const {
|
|
400
400
|
completionIcons = defaultCompletionIcons,
|
|
401
401
|
customTagIcons = defaultCustomTagIcons,
|
|
402
|
-
|
|
403
|
-
processHoverInfo = formatInfo || defaultHoverInfoProcessor,
|
|
402
|
+
processHoverInfo = defaultHoverInfoProcessor,
|
|
404
403
|
processHoverDocs = (docs) => docs,
|
|
405
404
|
classExtra = "",
|
|
406
405
|
jsdoc = true,
|
|
@@ -647,19 +646,21 @@ function rendererRich(options = {}) {
|
|
|
647
646
|
}
|
|
648
647
|
);
|
|
649
648
|
},
|
|
650
|
-
|
|
649
|
+
nodesError(error, children) {
|
|
651
650
|
if (errorRendering !== "hover") {
|
|
652
|
-
return
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
651
|
+
return [
|
|
652
|
+
extend(
|
|
653
|
+
hast?.errorToken,
|
|
654
|
+
{
|
|
655
|
+
type: "element",
|
|
656
|
+
tagName: "span",
|
|
657
|
+
properties: {
|
|
658
|
+
class: [`twoslash-error`, getErrorLevelClass(error)].filter(Boolean).join(" ")
|
|
659
|
+
},
|
|
660
|
+
children
|
|
661
|
+
}
|
|
662
|
+
)
|
|
663
|
+
];
|
|
663
664
|
}
|
|
664
665
|
const popup = extend(
|
|
665
666
|
hast?.errorPopup,
|
|
@@ -670,28 +671,39 @@ function rendererRich(options = {}) {
|
|
|
670
671
|
class: ["twoslash-popup-container", classExtra].filter(Boolean).join(" ")
|
|
671
672
|
},
|
|
672
673
|
children: [
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
674
|
+
extend(
|
|
675
|
+
hast?.popupError,
|
|
676
|
+
{
|
|
677
|
+
type: "element",
|
|
678
|
+
tagName: "div",
|
|
679
|
+
properties: {
|
|
680
|
+
class: "twoslash-popup-error"
|
|
681
|
+
},
|
|
682
|
+
children: renderMarkdown.call(this, error.text)
|
|
683
|
+
}
|
|
684
|
+
)
|
|
681
685
|
]
|
|
682
686
|
}
|
|
683
687
|
);
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
688
|
+
const token = {
|
|
689
|
+
type: "element",
|
|
690
|
+
tagName: "span",
|
|
691
|
+
children,
|
|
692
|
+
properties: {}
|
|
693
|
+
};
|
|
694
|
+
return [
|
|
695
|
+
extend(
|
|
696
|
+
hast?.errorToken,
|
|
697
|
+
{
|
|
698
|
+
type: "element",
|
|
699
|
+
tagName: "span",
|
|
700
|
+
properties: {
|
|
701
|
+
class: `twoslash-error twoslash-error-hover ${getErrorLevelClass(error)}`
|
|
702
|
+
},
|
|
703
|
+
children: hast?.errorCompose ? hast?.errorCompose({ popup, token }) : [popup, token]
|
|
704
|
+
}
|
|
705
|
+
)
|
|
706
|
+
];
|
|
695
707
|
},
|
|
696
708
|
lineError(error) {
|
|
697
709
|
if (errorRendering !== "line")
|
|
@@ -1056,6 +1068,24 @@ function createTransformerFactory(defaultTwoslasher, defaultRenderer) {
|
|
|
1056
1068
|
continue;
|
|
1057
1069
|
}
|
|
1058
1070
|
const tokens = locateTextTokens(node.line, node.character, node.length);
|
|
1071
|
+
const wrapTokens = (fn) => {
|
|
1072
|
+
const line = this.lines[node.line];
|
|
1073
|
+
let charIndex = 0;
|
|
1074
|
+
let itemStart = line.children.length;
|
|
1075
|
+
let itemEnd = 0;
|
|
1076
|
+
line.children.forEach((token, index) => {
|
|
1077
|
+
if (charIndex >= node.character && index < itemStart)
|
|
1078
|
+
itemStart = index;
|
|
1079
|
+
if (charIndex <= node.character + node.length && index > itemEnd)
|
|
1080
|
+
itemEnd = index;
|
|
1081
|
+
charIndex += getTokenString(token).length;
|
|
1082
|
+
});
|
|
1083
|
+
if (charIndex <= node.character + node.length)
|
|
1084
|
+
itemEnd = line.children.length;
|
|
1085
|
+
const targets = line.children.slice(itemStart, itemEnd);
|
|
1086
|
+
const length = targets.length;
|
|
1087
|
+
line.children.splice(itemStart, length, ...fn(targets));
|
|
1088
|
+
};
|
|
1059
1089
|
switch (node.type) {
|
|
1060
1090
|
case "error": {
|
|
1061
1091
|
if (renderer.nodeError) {
|
|
@@ -1065,6 +1095,14 @@ function createTransformerFactory(defaultTwoslasher, defaultRenderer) {
|
|
|
1065
1095
|
Object.assign(token, renderer.nodeError.call(this, node, clone));
|
|
1066
1096
|
});
|
|
1067
1097
|
}
|
|
1098
|
+
if (renderer.nodesError) {
|
|
1099
|
+
tokens.forEach((token) => {
|
|
1100
|
+
tokensSkipHover.add(token);
|
|
1101
|
+
});
|
|
1102
|
+
actionsHighlights.push(() => {
|
|
1103
|
+
wrapTokens((targets) => renderer.nodesError?.call(this, node, targets) || targets);
|
|
1104
|
+
});
|
|
1105
|
+
}
|
|
1068
1106
|
if (renderer.lineError)
|
|
1069
1107
|
insertAfterLine(node.line, renderer.lineError.call(this, node));
|
|
1070
1108
|
break;
|
|
@@ -1095,23 +1133,7 @@ function createTransformerFactory(defaultTwoslasher, defaultRenderer) {
|
|
|
1095
1133
|
case "highlight": {
|
|
1096
1134
|
if (renderer.nodesHighlight) {
|
|
1097
1135
|
actionsHighlights.push(() => {
|
|
1098
|
-
|
|
1099
|
-
let charIndex = 0;
|
|
1100
|
-
let itemStart = line.children.length;
|
|
1101
|
-
let itemEnd = 0;
|
|
1102
|
-
line.children.forEach((token, index) => {
|
|
1103
|
-
if (charIndex >= node.character && index < itemStart)
|
|
1104
|
-
itemStart = index;
|
|
1105
|
-
if (charIndex <= node.character + node.length && index > itemEnd)
|
|
1106
|
-
itemEnd = index;
|
|
1107
|
-
charIndex += getTokenString(token).length;
|
|
1108
|
-
});
|
|
1109
|
-
if (charIndex <= node.character + node.length)
|
|
1110
|
-
itemEnd = line.children.length;
|
|
1111
|
-
const targets = line.children.slice(itemStart, itemEnd);
|
|
1112
|
-
const length = targets.length;
|
|
1113
|
-
const highlighted = renderer.nodesHighlight?.call(this, node, targets) || targets;
|
|
1114
|
-
line.children.splice(itemStart, length, ...highlighted);
|
|
1136
|
+
wrapTokens((targets) => renderer.nodesHighlight?.call(this, node, targets) || targets);
|
|
1115
1137
|
});
|
|
1116
1138
|
}
|
|
1117
1139
|
break;
|
package/dist/index.d.mts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import * as _shikijs_core_dist_chunk_tokens_mjs from '@shikijs/core/dist/chunk-tokens.mjs';
|
|
2
2
|
import { CreateTwoslashOptions } from 'twoslash';
|
|
3
3
|
import { TransformerTwoslashOptions, RendererRichOptions } from './core.mjs';
|
|
4
|
-
export { CompletionItem, HastExtension, ShikiTwoslashError,
|
|
4
|
+
export { CompletionItem, HastExtension, ShikiTwoslashError, TwoslashRenderer, TwoslashShikiFunction, TwoslashShikiReturn, createTransformerFactory, defaultCompletionIcons, defaultCustomTagIcons, defaultHoverInfoProcessor, defaultTwoslashOptions, rendererClassic, rendererRich } from './core.mjs';
|
|
5
5
|
import '@shikijs/core';
|
|
6
6
|
import 'hast';
|
|
7
7
|
|
package/dist/index.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import * as _shikijs_core_dist_chunk_tokens_mjs from '@shikijs/core/dist/chunk-tokens.mjs';
|
|
2
2
|
import { CreateTwoslashOptions } from 'twoslash';
|
|
3
3
|
import { TransformerTwoslashOptions, RendererRichOptions } from './core.js';
|
|
4
|
-
export { CompletionItem, HastExtension, ShikiTwoslashError,
|
|
4
|
+
export { CompletionItem, HastExtension, ShikiTwoslashError, TwoslashRenderer, TwoslashShikiFunction, TwoslashShikiReturn, createTransformerFactory, defaultCompletionIcons, defaultCustomTagIcons, defaultHoverInfoProcessor, defaultTwoslashOptions, rendererClassic, rendererRich } from './core.js';
|
|
5
5
|
import '@shikijs/core';
|
|
6
6
|
import 'hast';
|
|
7
7
|
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@shikijs/twoslash",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "1.0.0
|
|
4
|
+
"version": "1.0.0",
|
|
5
5
|
"description": "Shiki transformer for twoslash",
|
|
6
6
|
"author": "Anthony Fu <anthonyfu117@hotmail.com>",
|
|
7
7
|
"license": "MIT",
|
|
@@ -49,8 +49,8 @@
|
|
|
49
49
|
"dist"
|
|
50
50
|
],
|
|
51
51
|
"dependencies": {
|
|
52
|
-
"twoslash": "^0.1.
|
|
53
|
-
"@shikijs/core": "1.0.0
|
|
52
|
+
"twoslash": "^0.1.2",
|
|
53
|
+
"@shikijs/core": "1.0.0"
|
|
54
54
|
},
|
|
55
55
|
"devDependencies": {
|
|
56
56
|
"@iconify-json/carbon": "^1.1.28",
|