@shikijs/transformers 3.12.2 → 3.13.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
@@ -150,6 +150,15 @@ declare function transformerRemoveLineBreak(): ShikiTransformer$1;
150
150
  */
151
151
  declare function transformerRemoveNotationEscape(): ShikiTransformer$1;
152
152
 
153
+ interface TransformerRenderIndentGuidesOptions {
154
+ indent?: number;
155
+ }
156
+ /**
157
+ * Render indentations as separate tokens.
158
+ * Apply with CSS, it can be used to render indent guides visually.
159
+ */
160
+ declare function transformerRenderIndentGuides(options?: TransformerRenderIndentGuidesOptions): ShikiTransformer$1;
161
+
153
162
  interface TransformerRenderWhitespaceOptions {
154
163
  /**
155
164
  * Class for tab
@@ -203,5 +212,5 @@ interface ShikiTransformerStyleToClass extends ShikiTransformer$1 {
203
212
  */
204
213
  declare function transformerStyleToClass(options?: TransformerStyleToClassOptions): ShikiTransformerStyleToClass;
205
214
 
206
- export { createCommentNotationTransformer, findAllSubstringIndexes, parseMetaHighlightString, parseMetaHighlightWords, transformerCompactLineOptions, transformerMetaHighlight, transformerMetaWordHighlight, transformerNotationDiff, transformerNotationErrorLevel, transformerNotationFocus, transformerNotationHighlight, transformerNotationMap, transformerNotationWordHighlight, transformerRemoveLineBreak, transformerRemoveNotationEscape, transformerRenderWhitespace, transformerStyleToClass };
207
- export type { ShikiTransformerStyleToClass, TransformerCompactLineOption, TransformerMetaHighlightOptions, TransformerMetaWordHighlightOptions, TransformerNotationDiffOptions, TransformerNotationErrorLevelOptions, TransformerNotationFocusOptions, TransformerNotationHighlightOptions, TransformerNotationMapOptions, TransformerNotationWordHighlightOptions, TransformerRenderWhitespaceOptions, TransformerStyleToClassOptions };
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 };
package/dist/index.mjs CHANGED
@@ -485,6 +485,73 @@ function transformerRemoveNotationEscape() {
485
485
  };
486
486
  }
487
487
 
488
+ function transformerRenderIndentGuides(options = {}) {
489
+ return {
490
+ name: "@shikijs/transformers:render-indent-guides",
491
+ code(hast) {
492
+ let { indent = 2 } = options;
493
+ const match = this.options.meta?.__raw?.match(/\{indent:(\d+|false)\}/);
494
+ if (match) {
495
+ if (match[1] === "false") {
496
+ return hast;
497
+ }
498
+ indent = Number(match[1]);
499
+ }
500
+ const indentRegex = new RegExp(` {${indent}}| {0,${indent - 1}} | {1,}$`, "g");
501
+ const emptyLines = [];
502
+ let level = 0;
503
+ for (const line of hast.children) {
504
+ if (line.type !== "element") {
505
+ continue;
506
+ }
507
+ const first = line.children[0];
508
+ if (first?.type !== "element" || first?.children[0]?.type !== "text") {
509
+ emptyLines.push([line, level]);
510
+ continue;
511
+ }
512
+ const text = first.children[0];
513
+ const blanks = text.value.split(/[^ \t]/, 1)[0];
514
+ const ranges = [];
515
+ for (const match2 of blanks.matchAll(indentRegex)) {
516
+ const start = match2.index;
517
+ const end = start + match2[0].length;
518
+ ranges.push([start, end]);
519
+ }
520
+ for (const [line2, level2] of emptyLines) {
521
+ line2.children.unshift(...Array.from({ length: Math.min(ranges.length, level2 + 1) }, (_, i) => ({
522
+ type: "element",
523
+ tagName: "span",
524
+ properties: {
525
+ class: "indent",
526
+ style: `--indent-offset: ${i * indent}ch;`
527
+ },
528
+ children: []
529
+ })));
530
+ }
531
+ emptyLines.length = 0;
532
+ level = ranges.length;
533
+ if (ranges.length) {
534
+ line.children.unshift(
535
+ ...ranges.map(([start, end]) => ({
536
+ type: "element",
537
+ tagName: "span",
538
+ properties: {
539
+ class: "indent"
540
+ },
541
+ children: [{
542
+ type: "text",
543
+ value: text.value.slice(start, end)
544
+ }]
545
+ }))
546
+ );
547
+ text.value = text.value.slice(ranges.at(-1)[1]);
548
+ }
549
+ }
550
+ return hast;
551
+ }
552
+ };
553
+ }
554
+
488
555
  function isTab(part) {
489
556
  return part === " ";
490
557
  }
@@ -674,4 +741,4 @@ function cyrb53(str, seed = 0) {
674
741
  return (4294967296 * (2097151 & h2) + (h1 >>> 0)).toString(36).slice(0, 6);
675
742
  }
676
743
 
677
- export { createCommentNotationTransformer, findAllSubstringIndexes, parseMetaHighlightString, parseMetaHighlightWords, transformerCompactLineOptions, transformerMetaHighlight, transformerMetaWordHighlight, transformerNotationDiff, transformerNotationErrorLevel, transformerNotationFocus, transformerNotationHighlight, transformerNotationMap, transformerNotationWordHighlight, transformerRemoveLineBreak, transformerRemoveNotationEscape, transformerRenderWhitespace, transformerStyleToClass };
744
+ export { createCommentNotationTransformer, findAllSubstringIndexes, parseMetaHighlightString, parseMetaHighlightWords, transformerCompactLineOptions, transformerMetaHighlight, transformerMetaWordHighlight, transformerNotationDiff, transformerNotationErrorLevel, transformerNotationFocus, transformerNotationHighlight, transformerNotationMap, transformerNotationWordHighlight, 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.12.2",
4
+ "version": "3.13.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/types": "3.12.2",
31
- "@shikijs/core": "3.12.2"
30
+ "@shikijs/core": "3.13.0",
31
+ "@shikijs/types": "3.13.0"
32
32
  },
33
33
  "scripts": {
34
34
  "build": "unbuild",