@shikijs/twoslash 1.1.0 → 1.1.2
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 +11 -0
- package/dist/core.d.ts +11 -0
- package/dist/core.mjs +23 -11
- package/package.json +4 -4
package/dist/core.d.mts
CHANGED
|
@@ -48,6 +48,17 @@ interface TransformerTwoslashOptions {
|
|
|
48
48
|
* @default true
|
|
49
49
|
*/
|
|
50
50
|
throws?: boolean;
|
|
51
|
+
/**
|
|
52
|
+
* Custom error handler for twoslash errors
|
|
53
|
+
* When specified, `throws` will be ignored
|
|
54
|
+
* Optionally return a string to replace the code
|
|
55
|
+
*/
|
|
56
|
+
onTwoslashError?: (error: unknown, code: string, lang: string, options: CodeToHastOptions) => string | void;
|
|
57
|
+
/**
|
|
58
|
+
* Custom error handler for Shiki errors
|
|
59
|
+
* When specified, `throws` will be ignored
|
|
60
|
+
*/
|
|
61
|
+
onShikiError?: (error: unknown, code: string, lang: string) => void;
|
|
51
62
|
}
|
|
52
63
|
interface TwoslashRenderer {
|
|
53
64
|
lineError?: (this: ShikiTransformerContext, error: NodeError) => ElementContent[];
|
package/dist/core.d.ts
CHANGED
|
@@ -48,6 +48,17 @@ interface TransformerTwoslashOptions {
|
|
|
48
48
|
* @default true
|
|
49
49
|
*/
|
|
50
50
|
throws?: boolean;
|
|
51
|
+
/**
|
|
52
|
+
* Custom error handler for twoslash errors
|
|
53
|
+
* When specified, `throws` will be ignored
|
|
54
|
+
* Optionally return a string to replace the code
|
|
55
|
+
*/
|
|
56
|
+
onTwoslashError?: (error: unknown, code: string, lang: string, options: CodeToHastOptions) => string | void;
|
|
57
|
+
/**
|
|
58
|
+
* Custom error handler for Shiki errors
|
|
59
|
+
* When specified, `throws` will be ignored
|
|
60
|
+
*/
|
|
61
|
+
onShikiError?: (error: unknown, code: string, lang: string) => void;
|
|
51
62
|
}
|
|
52
63
|
interface TwoslashRenderer {
|
|
53
64
|
lineError?: (this: ShikiTransformerContext, error: NodeError) => ElementContent[];
|
package/dist/core.mjs
CHANGED
|
@@ -990,6 +990,12 @@ function createTransformerFactory(defaultTwoslasher, defaultRenderer) {
|
|
|
990
990
|
renderer = defaultRenderer,
|
|
991
991
|
throws = true
|
|
992
992
|
} = options;
|
|
993
|
+
const onTwoslashError = options.onTwoslashError || (throws ? (error) => {
|
|
994
|
+
throw error;
|
|
995
|
+
} : () => false);
|
|
996
|
+
const onShikiError = options.onShikiError || (throws ? (error) => {
|
|
997
|
+
throw error;
|
|
998
|
+
} : () => false);
|
|
993
999
|
const trigger = explicitTrigger instanceof RegExp ? explicitTrigger : /\btwoslash\b/;
|
|
994
1000
|
if (!renderer)
|
|
995
1001
|
throw new ShikiTwoslashError("Missing renderer");
|
|
@@ -1001,11 +1007,17 @@ function createTransformerFactory(defaultTwoslasher, defaultRenderer) {
|
|
|
1001
1007
|
if (lang in langAlias)
|
|
1002
1008
|
lang = langAlias[this.options.lang];
|
|
1003
1009
|
if (filter(lang, code, this.options)) {
|
|
1004
|
-
|
|
1005
|
-
|
|
1006
|
-
|
|
1007
|
-
|
|
1008
|
-
|
|
1010
|
+
try {
|
|
1011
|
+
const twoslash = twoslasher(code, lang, twoslashOptions);
|
|
1012
|
+
map.set(this.meta, twoslash);
|
|
1013
|
+
this.meta.twoslash = twoslash;
|
|
1014
|
+
this.options.lang = twoslash.meta?.extension || lang;
|
|
1015
|
+
return twoslash.code;
|
|
1016
|
+
} catch (error) {
|
|
1017
|
+
const result = onTwoslashError(error, code, lang, this.options);
|
|
1018
|
+
if (typeof result === "string")
|
|
1019
|
+
return code;
|
|
1020
|
+
}
|
|
1009
1021
|
}
|
|
1010
1022
|
},
|
|
1011
1023
|
tokens(tokens) {
|
|
@@ -1039,8 +1051,7 @@ function createTransformerFactory(defaultTwoslasher, defaultRenderer) {
|
|
|
1039
1051
|
const lineEl = this.lines[line];
|
|
1040
1052
|
index = codeEl.children.indexOf(lineEl);
|
|
1041
1053
|
if (index === -1) {
|
|
1042
|
-
|
|
1043
|
-
throw new ShikiTwoslashError(`Cannot find line ${line} in code element`);
|
|
1054
|
+
onShikiError(new ShikiTwoslashError(`Cannot find line ${line} in code element`), this.source, this.options.lang);
|
|
1044
1055
|
return;
|
|
1045
1056
|
}
|
|
1046
1057
|
}
|
|
@@ -1077,8 +1088,10 @@ function createTransformerFactory(defaultTwoslasher, defaultRenderer) {
|
|
|
1077
1088
|
continue;
|
|
1078
1089
|
}
|
|
1079
1090
|
const tokens = locateTextTokens(node.line, node.character, node.length);
|
|
1080
|
-
if (!tokens.length)
|
|
1081
|
-
|
|
1091
|
+
if (!tokens.length && !(node.type === "error" && renderer.nodesError)) {
|
|
1092
|
+
onShikiError(new ShikiTwoslashError(`Cannot find tokens for node: ${JSON.stringify(node)}`), this.source, this.options.lang);
|
|
1093
|
+
continue;
|
|
1094
|
+
}
|
|
1082
1095
|
const wrapTokens = (fn) => {
|
|
1083
1096
|
const line = this.lines[node.line];
|
|
1084
1097
|
let charIndex = 0;
|
|
@@ -1164,8 +1177,7 @@ function createTransformerFactory(defaultTwoslasher, defaultRenderer) {
|
|
|
1164
1177
|
break;
|
|
1165
1178
|
}
|
|
1166
1179
|
default: {
|
|
1167
|
-
|
|
1168
|
-
throw new ShikiTwoslashError(`Unknown node type: ${node.type}`);
|
|
1180
|
+
onShikiError(new ShikiTwoslashError(`Unknown node type: ${node?.type}`), this.source, this.options.lang);
|
|
1169
1181
|
}
|
|
1170
1182
|
}
|
|
1171
1183
|
}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@shikijs/twoslash",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "1.1.
|
|
4
|
+
"version": "1.1.2",
|
|
5
5
|
"description": "Shiki transformer for twoslash",
|
|
6
6
|
"author": "Anthony Fu <anthonyfu117@hotmail.com>",
|
|
7
7
|
"license": "MIT",
|
|
@@ -49,11 +49,11 @@
|
|
|
49
49
|
"dist"
|
|
50
50
|
],
|
|
51
51
|
"dependencies": {
|
|
52
|
-
"twoslash": "^0.2.
|
|
53
|
-
"@shikijs/core": "1.1.
|
|
52
|
+
"twoslash": "^0.2.1",
|
|
53
|
+
"@shikijs/core": "1.1.2"
|
|
54
54
|
},
|
|
55
55
|
"devDependencies": {
|
|
56
|
-
"@iconify-json/carbon": "^1.1.
|
|
56
|
+
"@iconify-json/carbon": "^1.1.30",
|
|
57
57
|
"@iconify-json/codicon": "^1.1.41",
|
|
58
58
|
"@shikijs/twoslash": "^3.1.2",
|
|
59
59
|
"hast-util-from-html": "^2.0.1",
|