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

Idea: Start/Stop Transformer blocks #620

Open
5 tasks done
richardgill opened this issue Mar 9, 2024 · 0 comments
Open
5 tasks done

Idea: Start/Stop Transformer blocks #620

richardgill opened this issue Mar 9, 2024 · 0 comments

Comments

@richardgill
Copy link
Contributor

richardgill commented Mar 9, 2024

Validations

Describe the bug

This suggestion applies to any common transformers which support range.

I'll use // [!code highlight] as an example.

When using @shikijs/transformers there are 3 options for highlighting many (N) lines of code:

a)

console.log('highlight') // [!code highlight]
console.log('highlight') // [!code highlight]
console.log('highlight') // [!code highlight]
  • This requires N comments.
  • This doesn't work well with some code formatters like prettier which unconditionally moves comments.
  • Users must remember to add more comments when they add more lines of code they want to highlight (feels ~ok though)

b)

// [!code highlight:4]
console.log('highlight') 
console.log('highlight') 
console.log('highlight')
  • Requires 1 comment ✅
  • Works with code formatter ✅
  • If a user adds more code it's easy to forget to change 4 -> 5 (brittle)

c)

```ts {1-3}
console.log('highlight') 
console.log('highlight') 
console.log('highlight')
```
  • Requires 1 meta ✅
  • Works with code formatter ✅
  • If a user adds more code it's easy to forget to change 1-3 -> 1-4 (brittle)
  • Not every code block has a meta

Idea: Start and Stop blocks.

Inspiration: // prettier-ignore-start and // prettier-ignore-end: https://prettier.io/docs/en/ignore.html#range-ignore

// [!code highlight-start]
console.log('highlight') 
console.log('highlight') 
console.log('highlight')
// [!code highlight-end]
  • Requires 2 lines of code ✅
  • Works with code formatter ✅
  • If a user adds more code it works

New transformer: transformerStartEndRange

Usage:

  transformers: [
    startEndTransformer(['highlight']), // must go before transformerNotationHighlight
    transformerNotationHighlight()
  ]

This transformer transforms code like this:

// [!code highlight-start]
console.log('highlight') 
console.log('highlight') 
console.log('highlight')
// [!code highlight-end]

Becomes:

// [!code highlight:4]
console.log('highlight') 
console.log('highlight') 
console.log('highlight')

Then transformerNotationHighlight can do it's normal thing.

It works with any existing transformer which accepts a 'range' e.g. :4 as a parameter.

PR

I've written this transformer in a project I'm working on, If you'd accept a PR for this transformer let me know and I'll hopefully be able to create a PR for it 👍

Let me know what naming conventions etc. you'd like (my proposal introduces a - but that might not be what you had in mind)

Reproduction

idea, no repro

Contributes

  • I am willing to submit a PR to fix this issue
  • I am willing to submit a PR with failing tests
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