@shikijs/transformers 2.4.2 → 3.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/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.mjs CHANGED
@@ -1,5 +1,3 @@
1
- import { warnDeprecated } from '@shikijs/core';
2
-
3
1
  const matchers = [
4
2
  [/^(<!--)(.+)(-->)$/, false],
5
3
  [/^(\/\*)(.+)(\*\/)$/, false],
@@ -12,6 +10,42 @@ const matchers = [
12
10
  function parseComments(lines, jsx, matchAlgorithm) {
13
11
  const out = [];
14
12
  for (const line of lines) {
13
+ if (matchAlgorithm === "v3") {
14
+ const splittedElements = line.children.flatMap((element, idx) => {
15
+ if (element.type !== "element")
16
+ return element;
17
+ const token = element.children[0];
18
+ if (token.type !== "text")
19
+ return element;
20
+ const isLast = idx === line.children.length - 1;
21
+ const isComment = matchToken(token.value, isLast);
22
+ if (!isComment)
23
+ return element;
24
+ const rawSplits = token.value.split(/(\s+\/\/)/);
25
+ if (rawSplits.length <= 1)
26
+ return element;
27
+ let splits = [rawSplits[0]];
28
+ for (let i = 1; i < rawSplits.length; i += 2) {
29
+ splits.push(rawSplits[i] + (rawSplits[i + 1] || ""));
30
+ }
31
+ splits = splits.filter(Boolean);
32
+ if (splits.length <= 1)
33
+ return element;
34
+ return splits.map((split) => {
35
+ return {
36
+ ...element,
37
+ children: [
38
+ {
39
+ type: "text",
40
+ value: split
41
+ }
42
+ ]
43
+ };
44
+ });
45
+ });
46
+ if (splittedElements.length !== line.children.length)
47
+ line.children = splittedElements;
48
+ }
15
49
  const elements = line.children;
16
50
  let start = elements.length - 1;
17
51
  if (matchAlgorithm === "v1")
@@ -30,17 +64,20 @@ function parseComments(lines, jsx, matchAlgorithm) {
30
64
  if (!match)
31
65
  continue;
32
66
  if (jsx && !isLast && i !== 0) {
67
+ const isJsxStyle = isValue(elements[i - 1], "{") && isValue(elements[i + 1], "}");
33
68
  out.push({
34
69
  info: match,
35
70
  line,
36
71
  token,
37
- isJsxStyle: isValue(elements[i - 1], "{") && isValue(elements[i + 1], "}")
72
+ isLineCommentOnly: elements.length === 3 && token.children.length === 1,
73
+ isJsxStyle
38
74
  });
39
75
  } else {
40
76
  out.push({
41
77
  info: match,
42
78
  line,
43
79
  token,
80
+ isLineCommentOnly: elements.length === 1 && token.children.length === 1,
44
81
  isJsxStyle: false
45
82
  });
46
83
  }
@@ -75,18 +112,16 @@ function matchToken(text, isLast) {
75
112
  }
76
113
  }
77
114
  function v1ClearEndCommentPrefix(text) {
78
- const regex = /(?:\/\/|["'#]|;{1,2}|%{1,2}|--)(.*)$/;
79
- const result = regex.exec(text);
80
- if (result && result[1].trim().length === 0) {
81
- return text.slice(0, result.index);
115
+ const match = text.match(/(?:\/\/|["'#]|;{1,2}|%{1,2}|--)(\s*)$/);
116
+ if (match && match[1].trim().length === 0) {
117
+ return text.slice(0, match.index);
82
118
  }
83
119
  return text;
84
120
  }
85
121
 
86
122
  function createCommentNotationTransformer(name, regex, onMatch, matchAlgorithm) {
87
123
  if (matchAlgorithm == null) {
88
- matchAlgorithm = "v1";
89
- warnDeprecated('The default `matchAlgorithm: "v1"` is deprecated and will be removed in the future. Please explicitly set `matchAlgorithm: "v3"` in the transformer options.', 3);
124
+ matchAlgorithm = "v3";
90
125
  }
91
126
  return {
92
127
  name,
@@ -100,9 +135,8 @@ function createCommentNotationTransformer(name, regex, onMatch, matchAlgorithm)
100
135
  for (const comment of parsed) {
101
136
  if (comment.info[1].length === 0)
102
137
  continue;
103
- const isLineCommentOnly = comment.line.children.length === (comment.isJsxStyle ? 3 : 1);
104
138
  let lineIdx = lines.indexOf(comment.line);
105
- if (isLineCommentOnly && matchAlgorithm !== "v1")
139
+ if (comment.isLineCommentOnly && matchAlgorithm !== "v1")
106
140
  lineIdx++;
107
141
  let replaced = false;
108
142
  comment.info[1] = comment.info[1].replace(regex, (...match) => {
@@ -114,13 +148,12 @@ function createCommentNotationTransformer(name, regex, onMatch, matchAlgorithm)
114
148
  });
115
149
  if (!replaced)
116
150
  continue;
117
- if (matchAlgorithm === "v1") {
151
+ if (matchAlgorithm === "v1")
118
152
  comment.info[1] = v1ClearEndCommentPrefix(comment.info[1]);
119
- }
120
153
  const isEmpty = comment.info[1].trim().length === 0;
121
154
  if (isEmpty)
122
155
  comment.info[1] = "";
123
- if (isEmpty && isLineCommentOnly) {
156
+ if (isEmpty && comment.isLineCommentOnly) {
124
157
  linesToRemove.push(comment.line);
125
158
  } else if (isEmpty && comment.isJsxStyle) {
126
159
  comment.line.children.splice(comment.line.children.indexOf(comment.token) - 1, 3);
@@ -133,8 +166,14 @@ function createCommentNotationTransformer(name, regex, onMatch, matchAlgorithm)
133
166
  }
134
167
  }
135
168
  }
136
- for (const line of linesToRemove)
137
- code.children.splice(code.children.indexOf(line), 1);
169
+ for (const line of linesToRemove) {
170
+ const index = code.children.indexOf(line);
171
+ const nextLine = code.children[index + 1];
172
+ let removeLength = 1;
173
+ if (nextLine?.type === "text" && nextLine?.value === "\n")
174
+ removeLength = 2;
175
+ code.children.splice(index, removeLength);
176
+ }
138
177
  }
139
178
  };
140
179
  }
@@ -220,9 +259,16 @@ function transformerMetaWordHighlight(options = {}) {
220
259
  }
221
260
  function findAllSubstringIndexes(str, substr) {
222
261
  const indexes = [];
223
- let i = -1;
224
- while ((i = str.indexOf(substr, i + 1)) !== -1)
225
- indexes.push(i);
262
+ let cursor = 0;
263
+ while (true) {
264
+ const index = str.indexOf(substr, cursor);
265
+ if (index === -1 || index >= str.length)
266
+ break;
267
+ if (index < cursor)
268
+ break;
269
+ indexes.push(index);
270
+ cursor = index + substr.length;
271
+ }
226
272
  return indexes;
227
273
  }
228
274
 
@@ -618,4 +664,4 @@ function cyrb53(str, seed = 0) {
618
664
  return (4294967296 * (2097151 & h2) + (h1 >>> 0)).toString(36).slice(0, 6);
619
665
  }
620
666
 
621
- export { createCommentNotationTransformer, parseMetaHighlightString, parseMetaHighlightWords, transformerCompactLineOptions, transformerMetaHighlight, transformerMetaWordHighlight, transformerNotationDiff, transformerNotationErrorLevel, transformerNotationFocus, transformerNotationHighlight, transformerNotationMap, transformerNotationWordHighlight, transformerRemoveLineBreak, transformerRemoveNotationEscape, transformerRenderWhitespace, transformerStyleToClass };
667
+ 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.2",
4
+ "version": "3.0.0",
5
5
  "description": "Collective of common transformers transformers for Shiki",
6
6
  "author": "Anthony Fu <anthonyfu117@hotmail.com>",
7
7
  "license": "MIT",
@@ -18,10 +18,7 @@
18
18
  ],
19
19
  "sideEffects": false,
20
20
  "exports": {
21
- ".": {
22
- "types": "./dist/index.d.mts",
23
- "default": "./dist/index.mjs"
24
- }
21
+ ".": "./dist/index.mjs"
25
22
  },
26
23
  "main": "./dist/index.mjs",
27
24
  "module": "./dist/index.mjs",
@@ -30,8 +27,8 @@
30
27
  "dist"
31
28
  ],
32
29
  "dependencies": {
33
- "@shikijs/core": "2.4.2",
34
- "@shikijs/types": "2.4.2"
30
+ "@shikijs/core": "3.0.0",
31
+ "@shikijs/types": "3.0.0"
35
32
  },
36
33
  "scripts": {
37
34
  "build": "unbuild",
package/dist/index.d.ts DELETED
@@ -1,205 +0,0 @@
1
- import { ShikiTransformerContext, ShikiTransformer } from '@shikijs/core';
2
- import { Element } from 'hast';
3
- import { ShikiTransformer as ShikiTransformer$1 } from '@shikijs/types';
4
-
5
- type MatchAlgorithm = 'v1' | 'v3';
6
- interface MatchAlgorithmOptions {
7
- /**
8
- * Match algorithm to use
9
- *
10
- * @see https://shiki.style/packages/transformers#matching-algorithm
11
- * @default 'v1'
12
- */
13
- matchAlgorithm?: MatchAlgorithm;
14
- }
15
- declare function createCommentNotationTransformer(name: string, regex: RegExp, onMatch: (this: ShikiTransformerContext, match: string[], line: Element, commentNode: Element, lines: Element[], index: number) => boolean, matchAlgorithm: MatchAlgorithm | undefined): ShikiTransformer;
16
-
17
- interface TransformerCompactLineOption {
18
- /**
19
- * 1-based line number.
20
- */
21
- line: number;
22
- classes?: string[];
23
- }
24
- /**
25
- * Transformer for `shiki`'s legacy `lineOptions`
26
- */
27
- declare function transformerCompactLineOptions(lineOptions?: TransformerCompactLineOption[]): ShikiTransformer$1;
28
-
29
- declare function parseMetaHighlightString(meta: string): number[] | null;
30
- interface TransformerMetaHighlightOptions {
31
- /**
32
- * Class for highlighted lines
33
- *
34
- * @default 'highlighted'
35
- */
36
- className?: string;
37
- }
38
- /**
39
- * Allow using `{1,3-5}` in the code snippet meta to mark highlighted lines.
40
- */
41
- declare function transformerMetaHighlight(options?: TransformerMetaHighlightOptions): ShikiTransformer$1;
42
-
43
- declare function parseMetaHighlightWords(meta: string): string[];
44
- interface TransformerMetaWordHighlightOptions {
45
- /**
46
- * Class for highlighted words
47
- *
48
- * @default 'highlighted-word'
49
- */
50
- className?: string;
51
- }
52
- /**
53
- * Allow using `/word/` in the code snippet meta to mark highlighted words.
54
- */
55
- declare function transformerMetaWordHighlight(options?: TransformerMetaWordHighlightOptions): ShikiTransformer$1;
56
-
57
- interface TransformerNotationDiffOptions extends MatchAlgorithmOptions {
58
- /**
59
- * Class for added lines
60
- */
61
- classLineAdd?: string;
62
- /**
63
- * Class for removed lines
64
- */
65
- classLineRemove?: string;
66
- /**
67
- * Class added to the <pre> element when the current code has diff
68
- */
69
- classActivePre?: string;
70
- }
71
- /**
72
- * Use `[!code ++]` and `[!code --]` to mark added and removed lines.
73
- */
74
- declare function transformerNotationDiff(options?: TransformerNotationDiffOptions): ShikiTransformer$1;
75
-
76
- interface TransformerNotationErrorLevelOptions extends MatchAlgorithmOptions {
77
- classMap?: Record<string, string | string[]>;
78
- /**
79
- * Class added to the <pre> element when the current code has diff
80
- */
81
- classActivePre?: string;
82
- }
83
- /**
84
- * Allow using `[!code error]` `[!code warning]` notation in code to mark highlighted lines.
85
- */
86
- declare function transformerNotationErrorLevel(options?: TransformerNotationErrorLevelOptions): ShikiTransformer$1;
87
-
88
- interface TransformerNotationFocusOptions extends MatchAlgorithmOptions {
89
- /**
90
- * Class for focused lines
91
- */
92
- classActiveLine?: string;
93
- /**
94
- * Class added to the root element when the code has focused lines
95
- */
96
- classActivePre?: string;
97
- }
98
- /**
99
- * Allow using `[!code focus]` notation in code to mark focused lines.
100
- */
101
- declare function transformerNotationFocus(options?: TransformerNotationFocusOptions): ShikiTransformer$1;
102
-
103
- interface TransformerNotationHighlightOptions extends MatchAlgorithmOptions {
104
- /**
105
- * Class for highlighted lines
106
- */
107
- classActiveLine?: string;
108
- /**
109
- * Class added to the root element when the code has highlighted lines
110
- */
111
- classActivePre?: string;
112
- }
113
- /**
114
- * Allow using `[!code highlight]` notation in code to mark highlighted lines.
115
- */
116
- declare function transformerNotationHighlight(options?: TransformerNotationHighlightOptions): ShikiTransformer$1;
117
-
118
- interface TransformerNotationWordHighlightOptions extends MatchAlgorithmOptions {
119
- /**
120
- * Class for highlighted words
121
- */
122
- classActiveWord?: string;
123
- /**
124
- * Class added to the root element when the code has highlighted words
125
- */
126
- classActivePre?: string;
127
- }
128
- declare function transformerNotationWordHighlight(options?: TransformerNotationWordHighlightOptions): ShikiTransformer$1;
129
-
130
- interface TransformerNotationMapOptions extends MatchAlgorithmOptions {
131
- classMap?: Record<string, string | string[]>;
132
- /**
133
- * Class added to the <pre> element when the current code has diff
134
- */
135
- classActivePre?: string;
136
- }
137
- declare function transformerNotationMap(options?: TransformerNotationMapOptions, name?: string): ShikiTransformer$1;
138
-
139
- /**
140
- * Remove line breaks between lines.
141
- * Useful when you override `display: block` to `.line` in CSS.
142
- */
143
- declare function transformerRemoveLineBreak(): ShikiTransformer$1;
144
-
145
- /**
146
- * Remove notation escapes.
147
- * Useful when you want to write `// [!code` in markdown.
148
- * If you process `// [\!code ...]` expression, you can get `// [!code ...]` in the output.
149
- */
150
- declare function transformerRemoveNotationEscape(): ShikiTransformer$1;
151
-
152
- interface TransformerRenderWhitespaceOptions {
153
- /**
154
- * Class for tab
155
- *
156
- * @default 'tab'
157
- */
158
- classTab?: string;
159
- /**
160
- * Class for space
161
- *
162
- * @default 'space'
163
- */
164
- classSpace?: string;
165
- /**
166
- * Position of rendered whitespace
167
- * @default all position
168
- */
169
- position?: 'all' | 'boundary' | 'trailing';
170
- }
171
- /**
172
- * Render whitespaces as separate tokens.
173
- * Apply with CSS, it can be used to render tabs and spaces visually.
174
- */
175
- declare function transformerRenderWhitespace(options?: TransformerRenderWhitespaceOptions): ShikiTransformer$1;
176
-
177
- interface TransformerStyleToClassOptions {
178
- /**
179
- * Prefix for class names.
180
- * @default '__shiki_'
181
- */
182
- classPrefix?: string;
183
- /**
184
- * Suffix for class names.
185
- * @default ''
186
- */
187
- classSuffix?: string;
188
- /**
189
- * Callback to replace class names.
190
- * @default (className) => className
191
- */
192
- classReplacer?: (className: string) => string;
193
- }
194
- interface ShikiTransformerStyleToClass extends ShikiTransformer$1 {
195
- getClassRegistry: () => Map<string, Record<string, string> | string>;
196
- getCSS: () => string;
197
- clearRegistry: () => void;
198
- }
199
- /**
200
- * Remove line breaks between lines.
201
- * Useful when you override `display: block` to `.line` in CSS.
202
- */
203
- declare function transformerStyleToClass(options?: TransformerStyleToClassOptions): ShikiTransformerStyleToClass;
204
-
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 };