-
-
Notifications
You must be signed in to change notification settings - Fork 37
feat(transformers): two new transformers for word highlighting #92
Conversation
✅ Deploy Preview for shikiji ready!
To edit notification comments on pull requests, go to your Netlify site configuration. |
I noticed |
That's a nice idea! But instead of overloading the code block meta, I feel it might be better to use a control comment, like |
That is plausible, I'd agree using notation transformers for highlight features. But I think the current curly braces approach might be inconsistent if we prefer transformers over meta strings when highlighting lines. Perhaps removing support for curly braces in a major release? So that we can solve many compatibility issues with meta string. |
I have just added ```js
export function transformerNotationFocus(
options = {}, // [!code highlight[options]:4]
) {
const options = "Hello World"
console.log(options)
}
``` |
packages/shikiji-transformers/src/transformers/notation-highlight.ts
Outdated
Show resolved
Hide resolved
I am also in favor of this syntax ```js /foo/ const foo = 'bar' ``` This is also a cool feature to be able to highlight only the first two instances of ```js /x/1-2 let x = 1 x++ ... x++ ``` or give the highlighted token an id ```js /x/#highlighted_token const x = 1 ``` |
Probably can be an additional feature to the meta highlight transformer as @antfu suggested. This can solve compatibility issues, especially if you are working with other rehype plugins. |
I think we could have two transformers with different syntax (where shared the internal logic), like the current line highlight |
Just implemented a meta transformer similar to line highlighting.
About highlighting a word in a specific range, I would recommend using the notation transformer instead. You can easily specify a range right under the comment, so you don't have to waste time counting the line number of code. |
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## main #92 +/- ##
==========================================
+ Coverage 96.77% 96.88% +0.10%
==========================================
Files 51 54 +3
Lines 4495 4648 +153
Branches 572 595 +23
==========================================
+ Hits 4350 4503 +153
Misses 142 142
Partials 3 3 ☔ View full report in Codecov by Sentry. |
This is an awesome feature to have! I love using this library so much more than shikijs and having a highlight word basically removes the need to use rehype pretty code. Is there a way with this transformer to make a specific class for the highlighted words? For example this way two words could be targeted, and a different color could be applied to each one const foo = ref() //add 'highlighted-word' class to foo + '#foo' id for example
const bar = ref() // in addition to 'highlighted-word', '#bar' |
You can always wrote your custom https://shikiji.netlify.app/guide/transformers to do the customization you want |
It's not hard to add this as a new feature though, welcome for contributions! |
Description
Add functionality for highlighting a specific word/token, for example, a variable name. It adds a
highlighted-word
class to the matching element.transformerNotationWordHighlight
Use
[!code word:xxx]
to highlight a word (addinghighlighted-word
class).Notice that you cannot place anything before the comment in the same line of code.
transformerMetaWordHighlight
Highlight words based on the meta string provided on the code snippet. Requires integrations supports.
Linked Issues
fix #95
Additional context
The current implementation does a bit more than line highlighting because a token can contain whitespaces, like
a
for example. We will separate the matched string and its whitespaces:Before:
After:
All properties from the original node will be kept.