Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

@shikijs/transformers: extend matchAlgorithm with a start index #921

Open
4 tasks done
Fro-Q opened this issue Feb 7, 2025 · 1 comment
Open
4 tasks done

@shikijs/transformers: extend matchAlgorithm with a start index #921

Fro-Q opened this issue Feb 7, 2025 · 1 comment

Comments

@Fro-Q
Copy link

Fro-Q commented Feb 7, 2025

Clear and concise description of the problem

When attempting to add a transformer notation within an HTML snippet, it is currently impossible to highlight specific lines, such as lines 2–3, in cases like this:

<div
  class="test"
  style="color: white"
>
  test case
</div>

Suggested solution

Introduce a matchAlgorithm that supports matching lines with both a start and end index. Currently, the match must begin from the line containing the comment. For example, // [!code highlight:5] on line 3 results in a match of lines 3–7. Instead, we could allow specifying a range, such as // [!code highlight:2-5], which would match lines 4–7.

If the start index is not explicitly provided, the default value remains 1, ensuring no breaking changes.

Alternative

No response

Additional context

No response

Validations

Contributes

  • If this feature request is accepted, I am willing to submit a PR to fix this issue
@Fro-Q
Copy link
Author

Fro-Q commented Feb 10, 2025

This feature has already been implemented. If this request is approved, I will submit the PR as soon as possible.

Here’s an example of how it works when matchAlgorithm is set to 'v3'

```js
// [!code highlight:2-4]
console.log('nothing')
console.log('highlighted')
console.log('highlighted')
console.log('highlighted')

// [!code ++:3-4]
console.log('nothing')
console.log('nothing')
console.log('diff add')
console.log('diff add')

console.log('nothing')  // [!code --:2-4]
console.log('diff remove')
console.log('diff remove')
console.log('diff remove')

// no breaking changes
console.log('error')  // [!code error:3]
console.log('error')
console.log('error')
console.log('nothing')
```

It will be rendered as:

Render

This image should be self-explanatory. The CSS code is omitted for brevity.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant