@shikijs/transformers 3.17.1 → 3.18.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.
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
  }
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.18.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.18.0",
31
+ "@shikijs/types": "3.18.0"
32
32
  },
33
33
  "scripts": {
34
34
  "build": "unbuild",