@shikijs/transformers 2.4.2 → 2.5.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/index.d.mts +2 -1
- package/dist/index.d.ts +2 -1
- package/dist/index.mjs +65 -16
- package/package.json +3 -3
package/dist/index.d.mts
CHANGED
|
@@ -53,6 +53,7 @@ interface TransformerMetaWordHighlightOptions {
|
|
|
53
53
|
* Allow using `/word/` in the code snippet meta to mark highlighted words.
|
|
54
54
|
*/
|
|
55
55
|
declare function transformerMetaWordHighlight(options?: TransformerMetaWordHighlightOptions): ShikiTransformer$1;
|
|
56
|
+
declare function findAllSubstringIndexes(str: string, substr: string): number[];
|
|
56
57
|
|
|
57
58
|
interface TransformerNotationDiffOptions extends MatchAlgorithmOptions {
|
|
58
59
|
/**
|
|
@@ -202,4 +203,4 @@ interface ShikiTransformerStyleToClass extends ShikiTransformer$1 {
|
|
|
202
203
|
*/
|
|
203
204
|
declare function transformerStyleToClass(options?: TransformerStyleToClassOptions): ShikiTransformerStyleToClass;
|
|
204
205
|
|
|
205
|
-
export { type ShikiTransformerStyleToClass, type TransformerCompactLineOption, type TransformerMetaHighlightOptions, type TransformerMetaWordHighlightOptions, type TransformerNotationDiffOptions, type TransformerNotationErrorLevelOptions, type TransformerNotationFocusOptions, type TransformerNotationHighlightOptions, type TransformerNotationMapOptions, type TransformerNotationWordHighlightOptions, type TransformerRenderWhitespaceOptions, type TransformerStyleToClassOptions, createCommentNotationTransformer, parseMetaHighlightString, parseMetaHighlightWords, transformerCompactLineOptions, transformerMetaHighlight, transformerMetaWordHighlight, transformerNotationDiff, transformerNotationErrorLevel, transformerNotationFocus, transformerNotationHighlight, transformerNotationMap, transformerNotationWordHighlight, transformerRemoveLineBreak, transformerRemoveNotationEscape, transformerRenderWhitespace, transformerStyleToClass };
|
|
206
|
+
export { type ShikiTransformerStyleToClass, type TransformerCompactLineOption, type TransformerMetaHighlightOptions, type TransformerMetaWordHighlightOptions, type TransformerNotationDiffOptions, type TransformerNotationErrorLevelOptions, type TransformerNotationFocusOptions, type TransformerNotationHighlightOptions, type TransformerNotationMapOptions, type TransformerNotationWordHighlightOptions, type TransformerRenderWhitespaceOptions, type TransformerStyleToClassOptions, createCommentNotationTransformer, findAllSubstringIndexes, parseMetaHighlightString, parseMetaHighlightWords, transformerCompactLineOptions, transformerMetaHighlight, transformerMetaWordHighlight, transformerNotationDiff, transformerNotationErrorLevel, transformerNotationFocus, transformerNotationHighlight, transformerNotationMap, transformerNotationWordHighlight, transformerRemoveLineBreak, transformerRemoveNotationEscape, transformerRenderWhitespace, transformerStyleToClass };
|
package/dist/index.d.ts
CHANGED
|
@@ -53,6 +53,7 @@ interface TransformerMetaWordHighlightOptions {
|
|
|
53
53
|
* Allow using `/word/` in the code snippet meta to mark highlighted words.
|
|
54
54
|
*/
|
|
55
55
|
declare function transformerMetaWordHighlight(options?: TransformerMetaWordHighlightOptions): ShikiTransformer$1;
|
|
56
|
+
declare function findAllSubstringIndexes(str: string, substr: string): number[];
|
|
56
57
|
|
|
57
58
|
interface TransformerNotationDiffOptions extends MatchAlgorithmOptions {
|
|
58
59
|
/**
|
|
@@ -202,4 +203,4 @@ interface ShikiTransformerStyleToClass extends ShikiTransformer$1 {
|
|
|
202
203
|
*/
|
|
203
204
|
declare function transformerStyleToClass(options?: TransformerStyleToClassOptions): ShikiTransformerStyleToClass;
|
|
204
205
|
|
|
205
|
-
export { type ShikiTransformerStyleToClass, type TransformerCompactLineOption, type TransformerMetaHighlightOptions, type TransformerMetaWordHighlightOptions, type TransformerNotationDiffOptions, type TransformerNotationErrorLevelOptions, type TransformerNotationFocusOptions, type TransformerNotationHighlightOptions, type TransformerNotationMapOptions, type TransformerNotationWordHighlightOptions, type TransformerRenderWhitespaceOptions, type TransformerStyleToClassOptions, createCommentNotationTransformer, parseMetaHighlightString, parseMetaHighlightWords, transformerCompactLineOptions, transformerMetaHighlight, transformerMetaWordHighlight, transformerNotationDiff, transformerNotationErrorLevel, transformerNotationFocus, transformerNotationHighlight, transformerNotationMap, transformerNotationWordHighlight, transformerRemoveLineBreak, transformerRemoveNotationEscape, transformerRenderWhitespace, transformerStyleToClass };
|
|
206
|
+
export { type ShikiTransformerStyleToClass, type TransformerCompactLineOption, type TransformerMetaHighlightOptions, type TransformerMetaWordHighlightOptions, type TransformerNotationDiffOptions, type TransformerNotationErrorLevelOptions, type TransformerNotationFocusOptions, type TransformerNotationHighlightOptions, type TransformerNotationMapOptions, type TransformerNotationWordHighlightOptions, type TransformerRenderWhitespaceOptions, type TransformerStyleToClassOptions, createCommentNotationTransformer, findAllSubstringIndexes, parseMetaHighlightString, parseMetaHighlightWords, transformerCompactLineOptions, transformerMetaHighlight, transformerMetaWordHighlight, transformerNotationDiff, transformerNotationErrorLevel, transformerNotationFocus, transformerNotationHighlight, transformerNotationMap, transformerNotationWordHighlight, transformerRemoveLineBreak, transformerRemoveNotationEscape, transformerRenderWhitespace, transformerStyleToClass };
|
package/dist/index.mjs
CHANGED
|
@@ -12,6 +12,42 @@ const matchers = [
|
|
|
12
12
|
function parseComments(lines, jsx, matchAlgorithm) {
|
|
13
13
|
const out = [];
|
|
14
14
|
for (const line of lines) {
|
|
15
|
+
if (matchAlgorithm === "v3") {
|
|
16
|
+
const splittedElements = line.children.flatMap((element, idx) => {
|
|
17
|
+
if (element.type !== "element")
|
|
18
|
+
return element;
|
|
19
|
+
const token = element.children[0];
|
|
20
|
+
if (token.type !== "text")
|
|
21
|
+
return element;
|
|
22
|
+
const isLast = idx === line.children.length - 1;
|
|
23
|
+
const isComment = matchToken(token.value, isLast);
|
|
24
|
+
if (!isComment)
|
|
25
|
+
return element;
|
|
26
|
+
const rawSplits = token.value.split(/(\s+\/\/)/);
|
|
27
|
+
if (rawSplits.length <= 1)
|
|
28
|
+
return element;
|
|
29
|
+
let splits = [rawSplits[0]];
|
|
30
|
+
for (let i = 1; i < rawSplits.length; i += 2) {
|
|
31
|
+
splits.push(rawSplits[i] + (rawSplits[i + 1] || ""));
|
|
32
|
+
}
|
|
33
|
+
splits = splits.filter(Boolean);
|
|
34
|
+
if (splits.length <= 1)
|
|
35
|
+
return element;
|
|
36
|
+
return splits.map((split) => {
|
|
37
|
+
return {
|
|
38
|
+
...element,
|
|
39
|
+
children: [
|
|
40
|
+
{
|
|
41
|
+
type: "text",
|
|
42
|
+
value: split
|
|
43
|
+
}
|
|
44
|
+
]
|
|
45
|
+
};
|
|
46
|
+
});
|
|
47
|
+
});
|
|
48
|
+
if (splittedElements.length !== line.children.length)
|
|
49
|
+
line.children = splittedElements;
|
|
50
|
+
}
|
|
15
51
|
const elements = line.children;
|
|
16
52
|
let start = elements.length - 1;
|
|
17
53
|
if (matchAlgorithm === "v1")
|
|
@@ -30,17 +66,20 @@ function parseComments(lines, jsx, matchAlgorithm) {
|
|
|
30
66
|
if (!match)
|
|
31
67
|
continue;
|
|
32
68
|
if (jsx && !isLast && i !== 0) {
|
|
69
|
+
const isJsxStyle = isValue(elements[i - 1], "{") && isValue(elements[i + 1], "}");
|
|
33
70
|
out.push({
|
|
34
71
|
info: match,
|
|
35
72
|
line,
|
|
36
73
|
token,
|
|
37
|
-
|
|
74
|
+
isLineCommentOnly: elements.length === 3 && token.children.length === 1,
|
|
75
|
+
isJsxStyle
|
|
38
76
|
});
|
|
39
77
|
} else {
|
|
40
78
|
out.push({
|
|
41
79
|
info: match,
|
|
42
80
|
line,
|
|
43
81
|
token,
|
|
82
|
+
isLineCommentOnly: elements.length === 1 && token.children.length === 1,
|
|
44
83
|
isJsxStyle: false
|
|
45
84
|
});
|
|
46
85
|
}
|
|
@@ -75,10 +114,9 @@ function matchToken(text, isLast) {
|
|
|
75
114
|
}
|
|
76
115
|
}
|
|
77
116
|
function v1ClearEndCommentPrefix(text) {
|
|
78
|
-
const
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
return text.slice(0, result.index);
|
|
117
|
+
const match = text.match(/(?:\/\/|["'#]|;{1,2}|%{1,2}|--)(\s*)$/);
|
|
118
|
+
if (match && match[1].trim().length === 0) {
|
|
119
|
+
return text.slice(0, match.index);
|
|
82
120
|
}
|
|
83
121
|
return text;
|
|
84
122
|
}
|
|
@@ -100,9 +138,8 @@ function createCommentNotationTransformer(name, regex, onMatch, matchAlgorithm)
|
|
|
100
138
|
for (const comment of parsed) {
|
|
101
139
|
if (comment.info[1].length === 0)
|
|
102
140
|
continue;
|
|
103
|
-
const isLineCommentOnly = comment.line.children.length === (comment.isJsxStyle ? 3 : 1);
|
|
104
141
|
let lineIdx = lines.indexOf(comment.line);
|
|
105
|
-
if (isLineCommentOnly && matchAlgorithm !== "v1")
|
|
142
|
+
if (comment.isLineCommentOnly && matchAlgorithm !== "v1")
|
|
106
143
|
lineIdx++;
|
|
107
144
|
let replaced = false;
|
|
108
145
|
comment.info[1] = comment.info[1].replace(regex, (...match) => {
|
|
@@ -114,13 +151,12 @@ function createCommentNotationTransformer(name, regex, onMatch, matchAlgorithm)
|
|
|
114
151
|
});
|
|
115
152
|
if (!replaced)
|
|
116
153
|
continue;
|
|
117
|
-
if (matchAlgorithm === "v1")
|
|
154
|
+
if (matchAlgorithm === "v1")
|
|
118
155
|
comment.info[1] = v1ClearEndCommentPrefix(comment.info[1]);
|
|
119
|
-
}
|
|
120
156
|
const isEmpty = comment.info[1].trim().length === 0;
|
|
121
157
|
if (isEmpty)
|
|
122
158
|
comment.info[1] = "";
|
|
123
|
-
if (isEmpty && isLineCommentOnly) {
|
|
159
|
+
if (isEmpty && comment.isLineCommentOnly) {
|
|
124
160
|
linesToRemove.push(comment.line);
|
|
125
161
|
} else if (isEmpty && comment.isJsxStyle) {
|
|
126
162
|
comment.line.children.splice(comment.line.children.indexOf(comment.token) - 1, 3);
|
|
@@ -133,8 +169,14 @@ function createCommentNotationTransformer(name, regex, onMatch, matchAlgorithm)
|
|
|
133
169
|
}
|
|
134
170
|
}
|
|
135
171
|
}
|
|
136
|
-
for (const line of linesToRemove)
|
|
137
|
-
code.children.
|
|
172
|
+
for (const line of linesToRemove) {
|
|
173
|
+
const index = code.children.indexOf(line);
|
|
174
|
+
const nextLine = code.children[index + 1];
|
|
175
|
+
let removeLength = 1;
|
|
176
|
+
if (nextLine?.type === "text" && nextLine?.value === "\n")
|
|
177
|
+
removeLength = 2;
|
|
178
|
+
code.children.splice(index, removeLength);
|
|
179
|
+
}
|
|
138
180
|
}
|
|
139
181
|
};
|
|
140
182
|
}
|
|
@@ -220,9 +262,16 @@ function transformerMetaWordHighlight(options = {}) {
|
|
|
220
262
|
}
|
|
221
263
|
function findAllSubstringIndexes(str, substr) {
|
|
222
264
|
const indexes = [];
|
|
223
|
-
let
|
|
224
|
-
while (
|
|
225
|
-
|
|
265
|
+
let cursor = 0;
|
|
266
|
+
while (true) {
|
|
267
|
+
const index = str.indexOf(substr, cursor);
|
|
268
|
+
if (index === -1 || index >= str.length)
|
|
269
|
+
break;
|
|
270
|
+
if (index < cursor)
|
|
271
|
+
break;
|
|
272
|
+
indexes.push(index);
|
|
273
|
+
cursor = index + substr.length;
|
|
274
|
+
}
|
|
226
275
|
return indexes;
|
|
227
276
|
}
|
|
228
277
|
|
|
@@ -618,4 +667,4 @@ function cyrb53(str, seed = 0) {
|
|
|
618
667
|
return (4294967296 * (2097151 & h2) + (h1 >>> 0)).toString(36).slice(0, 6);
|
|
619
668
|
}
|
|
620
669
|
|
|
621
|
-
export { createCommentNotationTransformer, parseMetaHighlightString, parseMetaHighlightWords, transformerCompactLineOptions, transformerMetaHighlight, transformerMetaWordHighlight, transformerNotationDiff, transformerNotationErrorLevel, transformerNotationFocus, transformerNotationHighlight, transformerNotationMap, transformerNotationWordHighlight, transformerRemoveLineBreak, transformerRemoveNotationEscape, transformerRenderWhitespace, transformerStyleToClass };
|
|
670
|
+
export { createCommentNotationTransformer, findAllSubstringIndexes, parseMetaHighlightString, parseMetaHighlightWords, transformerCompactLineOptions, transformerMetaHighlight, transformerMetaWordHighlight, transformerNotationDiff, transformerNotationErrorLevel, transformerNotationFocus, transformerNotationHighlight, transformerNotationMap, transformerNotationWordHighlight, transformerRemoveLineBreak, transformerRemoveNotationEscape, transformerRenderWhitespace, transformerStyleToClass };
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@shikijs/transformers",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "2.
|
|
4
|
+
"version": "2.5.0",
|
|
5
5
|
"description": "Collective of common transformers transformers for Shiki",
|
|
6
6
|
"author": "Anthony Fu <anthonyfu117@hotmail.com>",
|
|
7
7
|
"license": "MIT",
|
|
@@ -30,8 +30,8 @@
|
|
|
30
30
|
"dist"
|
|
31
31
|
],
|
|
32
32
|
"dependencies": {
|
|
33
|
-
"@shikijs/core": "2.
|
|
34
|
-
"@shikijs/types": "2.
|
|
33
|
+
"@shikijs/core": "2.5.0",
|
|
34
|
+
"@shikijs/types": "2.5.0"
|
|
35
35
|
},
|
|
36
36
|
"scripts": {
|
|
37
37
|
"build": "unbuild",
|