@shikijs/transformers 3.18.0 → 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
@@ -74,6 +74,10 @@ interface TransformerNotationDiffOptions extends MatchAlgorithmOptions {
74
74
  * Class added to the <pre> element when the current code has diff
75
75
  */
76
76
  classActivePre?: string;
77
+ /**
78
+ * Class added to the <code> element when the current code has diff
79
+ */
80
+ classActiveCode?: string;
77
81
  }
78
82
  /**
79
83
  * Use `[!code ++]` and `[!code --]` to mark added and removed lines.
@@ -86,6 +90,10 @@ interface TransformerNotationErrorLevelOptions extends MatchAlgorithmOptions {
86
90
  * Class added to the <pre> element when the current code has diff
87
91
  */
88
92
  classActivePre?: string;
93
+ /**
94
+ * Class added to the <code> element when the current code has diff
95
+ */
96
+ classActiveCode?: string;
89
97
  }
90
98
  /**
91
99
  * Allow using `[!code error]` `[!code warning]` notation in code to mark highlighted lines.
@@ -101,6 +109,10 @@ interface TransformerNotationFocusOptions extends MatchAlgorithmOptions {
101
109
  * Class added to the root element when the code has focused lines
102
110
  */
103
111
  classActivePre?: string;
112
+ /**
113
+ * Class added to the <code> element when the code has focused lines
114
+ */
115
+ classActiveCode?: string;
104
116
  }
105
117
  /**
106
118
  * Allow using `[!code focus]` notation in code to mark focused lines.
@@ -116,6 +128,10 @@ interface TransformerNotationHighlightOptions extends MatchAlgorithmOptions {
116
128
  * Class added to the root element when the code has highlighted lines
117
129
  */
118
130
  classActivePre?: string;
131
+ /**
132
+ * Class added to the <code> element when the code has highlighted lines
133
+ */
134
+ classActiveCode?: string;
119
135
  }
120
136
  /**
121
137
  * Allow using `[!code highlight]` notation in code to mark highlighted lines.
@@ -140,9 +156,25 @@ interface TransformerNotationMapOptions extends MatchAlgorithmOptions {
140
156
  * Class added to the <pre> element when the current code has diff
141
157
  */
142
158
  classActivePre?: string;
159
+ /**
160
+ * Class added to the <code> element when the current code has diff
161
+ */
162
+ classActiveCode?: string;
143
163
  }
144
164
  declare function transformerNotationMap(options?: TransformerNotationMapOptions, name?: string): ShikiTransformer$1;
145
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
+
146
178
  /**
147
179
  * Remove line breaks between lines.
148
180
  * Useful when you override `display: block` to `.line` in CSS.
@@ -218,5 +250,5 @@ interface ShikiTransformerStyleToClass extends ShikiTransformer$1 {
218
250
  */
219
251
  declare function transformerStyleToClass(options?: TransformerStyleToClassOptions): ShikiTransformerStyleToClass;
220
252
 
221
- export { createCommentNotationTransformer, findAllSubstringIndexes, parseMetaHighlightString, parseMetaHighlightWords, transformerCompactLineOptions, transformerMetaHighlight, transformerMetaWordHighlight, transformerNotationDiff, transformerNotationErrorLevel, transformerNotationFocus, transformerNotationHighlight, transformerNotationMap, transformerNotationWordHighlight, transformerRemoveLineBreak, transformerRemoveNotationEscape, transformerRenderIndentGuides, transformerRenderWhitespace, transformerStyleToClass };
222
- 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
@@ -318,7 +318,8 @@ function escapeRegExp(str) {
318
318
  function transformerNotationMap(options = {}, name = "@shikijs/transformers:notation-map") {
319
319
  const {
320
320
  classMap = {},
321
- classActivePre = void 0
321
+ classActivePre = void 0,
322
+ classActiveCode = void 0
322
323
  } = options;
323
324
  return createCommentNotationTransformer(
324
325
  name,
@@ -330,6 +331,8 @@ function transformerNotationMap(options = {}, name = "@shikijs/transformers:nota
330
331
  }
331
332
  if (classActivePre)
332
333
  this.addClassToHast(this.pre, classActivePre);
334
+ if (classActiveCode)
335
+ this.addClassToHast(this.code, classActiveCode);
333
336
  return true;
334
337
  },
335
338
  options.matchAlgorithm
@@ -340,7 +343,8 @@ function transformerNotationDiff(options = {}) {
340
343
  const {
341
344
  classLineAdd = "diff add",
342
345
  classLineRemove = "diff remove",
343
- classActivePre = "has-diff"
346
+ classActivePre = "has-diff",
347
+ classActiveCode
344
348
  } = options;
345
349
  return transformerNotationMap(
346
350
  {
@@ -349,6 +353,7 @@ function transformerNotationDiff(options = {}) {
349
353
  "--": classLineRemove
350
354
  },
351
355
  classActivePre,
356
+ classActiveCode,
352
357
  matchAlgorithm: options.matchAlgorithm
353
358
  },
354
359
  "@shikijs/transformers:notation-diff"
@@ -361,12 +366,14 @@ function transformerNotationErrorLevel(options = {}) {
361
366
  error: ["highlighted", "error"],
362
367
  warning: ["highlighted", "warning"]
363
368
  },
364
- classActivePre = "has-highlighted"
369
+ classActivePre = "has-highlighted",
370
+ classActiveCode
365
371
  } = options;
366
372
  return transformerNotationMap(
367
373
  {
368
374
  classMap,
369
375
  classActivePre,
376
+ classActiveCode,
370
377
  matchAlgorithm: options.matchAlgorithm
371
378
  },
372
379
  "@shikijs/transformers:notation-error-level"
@@ -376,7 +383,8 @@ function transformerNotationErrorLevel(options = {}) {
376
383
  function transformerNotationFocus(options = {}) {
377
384
  const {
378
385
  classActiveLine = "focused",
379
- classActivePre = "has-focused"
386
+ classActivePre = "has-focused",
387
+ classActiveCode
380
388
  } = options;
381
389
  return transformerNotationMap(
382
390
  {
@@ -384,6 +392,7 @@ function transformerNotationFocus(options = {}) {
384
392
  focus: classActiveLine
385
393
  },
386
394
  classActivePre,
395
+ classActiveCode,
387
396
  matchAlgorithm: options.matchAlgorithm
388
397
  },
389
398
  "@shikijs/transformers:notation-focus"
@@ -393,7 +402,8 @@ function transformerNotationFocus(options = {}) {
393
402
  function transformerNotationHighlight(options = {}) {
394
403
  const {
395
404
  classActiveLine = "highlighted",
396
- classActivePre = "has-highlighted"
405
+ classActivePre = "has-highlighted",
406
+ classActiveCode
397
407
  } = options;
398
408
  return transformerNotationMap(
399
409
  {
@@ -402,6 +412,7 @@ function transformerNotationHighlight(options = {}) {
402
412
  hl: classActiveLine
403
413
  },
404
414
  classActivePre,
415
+ classActiveCode,
405
416
  matchAlgorithm: options.matchAlgorithm
406
417
  },
407
418
  "@shikijs/transformers:notation-highlight"
@@ -497,6 +508,41 @@ function transformerNotationWordHighlight(options = {}) {
497
508
  );
498
509
  }
499
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
+
500
546
  function transformerRemoveLineBreak() {
501
547
  return {
502
548
  name: "@shikijs/transformers:remove-line-break",
@@ -779,4 +825,4 @@ function cyrb53(str, seed = 0) {
779
825
  return (4294967296 * (2097151 & h2) + (h1 >>> 0)).toString(36).slice(0, 6);
780
826
  }
781
827
 
782
- 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.18.0",
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.18.0",
31
- "@shikijs/types": "3.18.0"
30
+ "@shikijs/core": "3.19.0",
31
+ "@shikijs/types": "3.19.0"
32
32
  },
33
33
  "scripts": {
34
34
  "build": "unbuild",