@shikijs/transformers 3.17.1 → 3.19.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
@@ -34,6 +34,12 @@ interface TransformerMetaHighlightOptions {
34
34
  * @default 'highlighted'
35
35
  */
36
36
  className?: string;
37
+ /**
38
+ * Interpret line numbers as 0-indexed
39
+ *
40
+ * @default false
41
+ */
42
+ zeroIndexed?: boolean;
37
43
  }
38
44
  /**
39
45
  * Allow using `{1,3-5}` in the code snippet meta to mark highlighted lines.
@@ -68,6 +74,10 @@ interface TransformerNotationDiffOptions extends MatchAlgorithmOptions {
68
74
  * Class added to the <pre> element when the current code has diff
69
75
  */
70
76
  classActivePre?: string;
77
+ /**
78
+ * Class added to the <code> element when the current code has diff
79
+ */
80
+ classActiveCode?: string;
71
81
  }
72
82
  /**
73
83
  * Use `[!code ++]` and `[!code --]` to mark added and removed lines.
@@ -80,6 +90,10 @@ interface TransformerNotationErrorLevelOptions extends MatchAlgorithmOptions {
80
90
  * Class added to the <pre> element when the current code has diff
81
91
  */
82
92
  classActivePre?: string;
93
+ /**
94
+ * Class added to the <code> element when the current code has diff
95
+ */
96
+ classActiveCode?: string;
83
97
  }
84
98
  /**
85
99
  * Allow using `[!code error]` `[!code warning]` notation in code to mark highlighted lines.
@@ -95,6 +109,10 @@ interface TransformerNotationFocusOptions extends MatchAlgorithmOptions {
95
109
  * Class added to the root element when the code has focused lines
96
110
  */
97
111
  classActivePre?: string;
112
+ /**
113
+ * Class added to the <code> element when the code has focused lines
114
+ */
115
+ classActiveCode?: string;
98
116
  }
99
117
  /**
100
118
  * Allow using `[!code focus]` notation in code to mark focused lines.
@@ -110,6 +128,10 @@ interface TransformerNotationHighlightOptions extends MatchAlgorithmOptions {
110
128
  * Class added to the root element when the code has highlighted lines
111
129
  */
112
130
  classActivePre?: string;
131
+ /**
132
+ * Class added to the <code> element when the code has highlighted lines
133
+ */
134
+ classActiveCode?: string;
113
135
  }
114
136
  /**
115
137
  * Allow using `[!code highlight]` notation in code to mark highlighted lines.
@@ -134,9 +156,25 @@ interface TransformerNotationMapOptions extends MatchAlgorithmOptions {
134
156
  * Class added to the <pre> element when the current code has diff
135
157
  */
136
158
  classActivePre?: string;
159
+ /**
160
+ * Class added to the <code> element when the current code has diff
161
+ */
162
+ classActiveCode?: string;
137
163
  }
138
164
  declare function transformerNotationMap(options?: TransformerNotationMapOptions, name?: string): ShikiTransformer$1;
139
165
 
166
+ interface TransformerRemoveCommentsOptions {
167
+ /**
168
+ * Remove lines that become empty after removing comments.
169
+ * @default true
170
+ */
171
+ removeEmptyLines?: boolean;
172
+ }
173
+ /**
174
+ * Remove comments from the code.
175
+ */
176
+ declare function transformerRemoveComments(options?: TransformerRemoveCommentsOptions): ShikiTransformer$1;
177
+
140
178
  /**
141
179
  * Remove line breaks between lines.
142
180
  * Useful when you override `display: block` to `.line` in CSS.
@@ -212,5 +250,5 @@ interface ShikiTransformerStyleToClass extends ShikiTransformer$1 {
212
250
  */
213
251
  declare function transformerStyleToClass(options?: TransformerStyleToClassOptions): ShikiTransformerStyleToClass;
214
252
 
215
- export { createCommentNotationTransformer, findAllSubstringIndexes, parseMetaHighlightString, parseMetaHighlightWords, transformerCompactLineOptions, transformerMetaHighlight, transformerMetaWordHighlight, transformerNotationDiff, transformerNotationErrorLevel, transformerNotationFocus, transformerNotationHighlight, transformerNotationMap, transformerNotationWordHighlight, transformerRemoveLineBreak, transformerRemoveNotationEscape, transformerRenderIndentGuides, transformerRenderWhitespace, transformerStyleToClass };
216
- export type { ShikiTransformerStyleToClass, TransformerCompactLineOption, TransformerMetaHighlightOptions, TransformerMetaWordHighlightOptions, TransformerNotationDiffOptions, TransformerNotationErrorLevelOptions, TransformerNotationFocusOptions, TransformerNotationHighlightOptions, TransformerNotationMapOptions, TransformerNotationWordHighlightOptions, TransformerRenderIndentGuidesOptions, TransformerRenderWhitespaceOptions, TransformerStyleToClassOptions };
253
+ export { createCommentNotationTransformer, findAllSubstringIndexes, parseMetaHighlightString, parseMetaHighlightWords, transformerCompactLineOptions, transformerMetaHighlight, transformerMetaWordHighlight, transformerNotationDiff, transformerNotationErrorLevel, transformerNotationFocus, transformerNotationHighlight, transformerNotationMap, transformerNotationWordHighlight, transformerRemoveComments, transformerRemoveLineBreak, transformerRemoveNotationEscape, transformerRenderIndentGuides, transformerRenderWhitespace, transformerStyleToClass };
254
+ export type { ShikiTransformerStyleToClass, TransformerCompactLineOption, TransformerMetaHighlightOptions, TransformerMetaWordHighlightOptions, TransformerNotationDiffOptions, TransformerNotationErrorLevelOptions, TransformerNotationFocusOptions, TransformerNotationHighlightOptions, TransformerNotationMapOptions, TransformerNotationWordHighlightOptions, TransformerRemoveCommentsOptions, TransformerRenderIndentGuidesOptions, TransformerRenderWhitespaceOptions, TransformerStyleToClassOptions };
package/dist/index.mjs CHANGED
@@ -241,28 +241,24 @@ function parseMetaHighlightString(meta) {
241
241
  if (!match)
242
242
  return null;
243
243
  const lines = match[1].split(",").flatMap((v) => {
244
- const num = v.split("-").map((v2) => Number.parseInt(v2, 10));
245
- if (num.length === 1)
246
- return [num[0]];
247
- return Array.from({ length: num[1] - num[0] + 1 }, (_, i) => i + num[0]);
244
+ const range = v.split("-").map((n) => Number.parseInt(n, 10));
245
+ return range.length === 1 ? [range[0]] : Array.from({ length: range[1] - range[0] + 1 }, (_, i) => range[0] + i);
248
246
  });
249
247
  return lines;
250
248
  }
251
249
  const symbol = Symbol("highlighted-lines");
252
250
  function transformerMetaHighlight(options = {}) {
253
- const {
254
- className = "highlighted"
255
- } = options;
251
+ const { className = "highlighted", zeroIndexed = false } = options;
256
252
  return {
257
253
  name: "@shikijs/transformers:meta-highlight",
258
- line(node, line) {
259
- if (!this.options.meta?.__raw) {
254
+ line(node, lineNumber) {
255
+ if (!this.options.meta?.__raw)
260
256
  return;
261
- }
262
257
  const meta = this.meta;
263
258
  meta[symbol] ??= parseMetaHighlightString(this.options.meta.__raw);
264
- const lines = meta[symbol] ?? [];
265
- if (lines.includes(line))
259
+ const highlightedLines = meta[symbol] ?? [];
260
+ const effectiveLine = zeroIndexed ? lineNumber - 1 : lineNumber;
261
+ if (highlightedLines.includes(effectiveLine))
266
262
  this.addClassToHast(node, className);
267
263
  return node;
268
264
  }
@@ -322,7 +318,8 @@ function escapeRegExp(str) {
322
318
  function transformerNotationMap(options = {}, name = "@shikijs/transformers:notation-map") {
323
319
  const {
324
320
  classMap = {},
325
- classActivePre = void 0
321
+ classActivePre = void 0,
322
+ classActiveCode = void 0
326
323
  } = options;
327
324
  return createCommentNotationTransformer(
328
325
  name,
@@ -334,6 +331,8 @@ function transformerNotationMap(options = {}, name = "@shikijs/transformers:nota
334
331
  }
335
332
  if (classActivePre)
336
333
  this.addClassToHast(this.pre, classActivePre);
334
+ if (classActiveCode)
335
+ this.addClassToHast(this.code, classActiveCode);
337
336
  return true;
338
337
  },
339
338
  options.matchAlgorithm
@@ -344,7 +343,8 @@ function transformerNotationDiff(options = {}) {
344
343
  const {
345
344
  classLineAdd = "diff add",
346
345
  classLineRemove = "diff remove",
347
- classActivePre = "has-diff"
346
+ classActivePre = "has-diff",
347
+ classActiveCode
348
348
  } = options;
349
349
  return transformerNotationMap(
350
350
  {
@@ -353,6 +353,7 @@ function transformerNotationDiff(options = {}) {
353
353
  "--": classLineRemove
354
354
  },
355
355
  classActivePre,
356
+ classActiveCode,
356
357
  matchAlgorithm: options.matchAlgorithm
357
358
  },
358
359
  "@shikijs/transformers:notation-diff"
@@ -365,12 +366,14 @@ function transformerNotationErrorLevel(options = {}) {
365
366
  error: ["highlighted", "error"],
366
367
  warning: ["highlighted", "warning"]
367
368
  },
368
- classActivePre = "has-highlighted"
369
+ classActivePre = "has-highlighted",
370
+ classActiveCode
369
371
  } = options;
370
372
  return transformerNotationMap(
371
373
  {
372
374
  classMap,
373
375
  classActivePre,
376
+ classActiveCode,
374
377
  matchAlgorithm: options.matchAlgorithm
375
378
  },
376
379
  "@shikijs/transformers:notation-error-level"
@@ -380,7 +383,8 @@ function transformerNotationErrorLevel(options = {}) {
380
383
  function transformerNotationFocus(options = {}) {
381
384
  const {
382
385
  classActiveLine = "focused",
383
- classActivePre = "has-focused"
386
+ classActivePre = "has-focused",
387
+ classActiveCode
384
388
  } = options;
385
389
  return transformerNotationMap(
386
390
  {
@@ -388,6 +392,7 @@ function transformerNotationFocus(options = {}) {
388
392
  focus: classActiveLine
389
393
  },
390
394
  classActivePre,
395
+ classActiveCode,
391
396
  matchAlgorithm: options.matchAlgorithm
392
397
  },
393
398
  "@shikijs/transformers:notation-focus"
@@ -397,7 +402,8 @@ function transformerNotationFocus(options = {}) {
397
402
  function transformerNotationHighlight(options = {}) {
398
403
  const {
399
404
  classActiveLine = "highlighted",
400
- classActivePre = "has-highlighted"
405
+ classActivePre = "has-highlighted",
406
+ classActiveCode
401
407
  } = options;
402
408
  return transformerNotationMap(
403
409
  {
@@ -406,6 +412,7 @@ function transformerNotationHighlight(options = {}) {
406
412
  hl: classActiveLine
407
413
  },
408
414
  classActivePre,
415
+ classActiveCode,
409
416
  matchAlgorithm: options.matchAlgorithm
410
417
  },
411
418
  "@shikijs/transformers:notation-highlight"
@@ -501,6 +508,41 @@ function transformerNotationWordHighlight(options = {}) {
501
508
  );
502
509
  }
503
510
 
511
+ function transformerRemoveComments(options = {}) {
512
+ const { removeEmptyLines = true } = options;
513
+ return {
514
+ name: "@shikijs/transformers:remove-comments",
515
+ preprocess(_code, options2) {
516
+ if (options2.includeExplanation !== true && options2.includeExplanation !== "scopeName")
517
+ throw new Error("`transformerRemoveComments` requires `includeExplanation` to be set to `true` or `'scopeName'`");
518
+ },
519
+ tokens(tokens) {
520
+ const result = [];
521
+ for (const line of tokens) {
522
+ const filteredLine = [];
523
+ let hasComment = false;
524
+ for (const token of line) {
525
+ const isComment = token.explanation?.some(
526
+ (exp) => exp.scopes.some((s) => s.scopeName.startsWith("comment"))
527
+ );
528
+ if (isComment) {
529
+ hasComment = true;
530
+ } else {
531
+ filteredLine.push(token);
532
+ }
533
+ }
534
+ if (removeEmptyLines && hasComment) {
535
+ const isAllWhitespace = filteredLine.every((token) => !token.content.trim());
536
+ if (isAllWhitespace)
537
+ continue;
538
+ }
539
+ result.push(filteredLine);
540
+ }
541
+ return result;
542
+ }
543
+ };
544
+ }
545
+
504
546
  function transformerRemoveLineBreak() {
505
547
  return {
506
548
  name: "@shikijs/transformers:remove-line-break",
@@ -783,4 +825,4 @@ function cyrb53(str, seed = 0) {
783
825
  return (4294967296 * (2097151 & h2) + (h1 >>> 0)).toString(36).slice(0, 6);
784
826
  }
785
827
 
786
- export { createCommentNotationTransformer, findAllSubstringIndexes, parseMetaHighlightString, parseMetaHighlightWords, transformerCompactLineOptions, transformerMetaHighlight, transformerMetaWordHighlight, transformerNotationDiff, transformerNotationErrorLevel, transformerNotationFocus, transformerNotationHighlight, transformerNotationMap, transformerNotationWordHighlight, transformerRemoveLineBreak, transformerRemoveNotationEscape, transformerRenderIndentGuides, transformerRenderWhitespace, transformerStyleToClass };
828
+ export { createCommentNotationTransformer, findAllSubstringIndexes, parseMetaHighlightString, parseMetaHighlightWords, transformerCompactLineOptions, transformerMetaHighlight, transformerMetaWordHighlight, transformerNotationDiff, transformerNotationErrorLevel, transformerNotationFocus, transformerNotationHighlight, transformerNotationMap, transformerNotationWordHighlight, transformerRemoveComments, transformerRemoveLineBreak, transformerRemoveNotationEscape, transformerRenderIndentGuides, transformerRenderWhitespace, transformerStyleToClass };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@shikijs/transformers",
3
3
  "type": "module",
4
- "version": "3.17.1",
4
+ "version": "3.19.0",
5
5
  "description": "Collective of common transformers transformers for Shiki",
6
6
  "author": "Anthony Fu <anthonyfu117@hotmail.com>",
7
7
  "license": "MIT",
@@ -27,8 +27,8 @@
27
27
  "dist"
28
28
  ],
29
29
  "dependencies": {
30
- "@shikijs/core": "3.17.1",
31
- "@shikijs/types": "3.17.1"
30
+ "@shikijs/core": "3.19.0",
31
+ "@shikijs/types": "3.19.0"
32
32
  },
33
33
  "scripts": {
34
34
  "build": "unbuild",