-
-
Notifications
You must be signed in to change notification settings - Fork 373
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
Implement support for adding custom CSS classes to specific lines #259
Conversation
If #263 gets merged, I'll rebase this PR and use the new |
This is really a @octref call, but it does feel like they were looking for a different (more low level) API surface for how this API would look to consumers: #83 (review) interface LineOption {
line: number
classes?: string[]
skipTokenization?: boolean // default to false
}
interface HTMLRendererOption {
lineOptions: LineOption[]
} Having built this in shiki-twoslash, the likely thing that you'd also want the API to be able to do it declare characters interface LineOption {
line: number
span?: [start: number, end : number]
classes?: string[]
skipTokenization?: boolean // default to false
}
interface HTMLRendererOption {
lineOptions: LineOption[]
} |
Thanks @orta, this is good feedback. I can update the PR accordingly. |
I implemented the API you suggested, @orta. Could you please take another look? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me, I will give it somewhere between 1-2 weeks to give @octref a chance to chime in but I feel like this is what they were looking for
Rebased onto |
Hah, he was just around on the repo - I'd prefer it, but if I get nothing tomorrow I'll merge 👍🏻 |
busy with work today, i'll try to take a look tomorrow. thanks! |
Thank you, looking forward to your review and feedback. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry for my late response. Looks all good to me. I just added a note on the 1-based indexing for line number. Great work 🙌
Sorry guys, where I can find docs how to use highlighting of add/remove/focus of specific lines? Thanks |
Hi! I tried to use it and everything is fine. In my The thing is, I would like to change the color of the highlight for each I would like to ask to add to the I tried to add inside the const lineColor = 'rgb(0 0 0 / 0.5) // this comes from a React props
const codeTags = renderToHtml(tokens, {
elements: {
line({ children: line }) {
return `<span class="line" style="--lineColor: ${lineColor}">${line}</span>`
},
},
lineOptions: [{ line: 1, classes: ['highlighted'] }, { line: 1, classes: ['highlighted'] }],
}) Hope I explained myself as I don't speak English great |
In this PR, I'm implementing support for adding custom CSS classes to specific lines. This is picking up the work that @dceddia started in #83.
API
The
codeToHtml
now accepts an optionaloptions
object with an optionalhighlightedLines
property:Highlighted lines can be specified one by one, or as a range of lines:
Example
Here's an example usage that would highlight lines 1, 3, 4, 5, 6, and 8:
And here's the corresponding HTML output:
Feedback
Feedback is more than welcome, I'm happy to iterate on this as necessary :)