gjtorikian / commonmarker

Ruby wrapper for the comrak (CommonMark parser) Rust crate
MIT License
416 stars 80 forks source link

Syntax highlighting using HTML `class` #258

Closed zachahn closed 7 months ago

zachahn commented 7 months ago

Hello!

Do you have any plans to support syntax highlighting using CSS classes rather than inline styles? I've only done some light searching, but it looks like the syntect crate has something called ClassedHTMLGenerator that generates output along the lines of <span class="...">.

This feature would let me use different themes based on dark/light mode. Totally understand if this won't be supported—I'm sure I can figure out a workaround.

gjtorikian commented 7 months ago

Hello! That sounds interesting. I certainly wouldn’t be opposed to it. I’ll take a look at what’s possible.

Without necessary knowing how syntect works, how would you expect to use this? You pass in a string of classes, and have that applied to the container pre?

zachahn commented 7 months ago

Yup, something like that. I'm on my phone so I'm a little limited with what I can look up!

I currently use a CommonMarker's custom renderer to process code blocks with Rouge, and I use a custom container to namespace my code styles. It also provides a way to generate CSS with various themes.

https://github.com/rouge-ruby/rouge

I found an example for syntect. At a glance, it doesn't look like it supports a generating CSS with custom container class.

https://github.com/trishume/syntect/blob/master/examples/synhtml-css-classes.rs

gjtorikian commented 7 months ago

Good news bad news.

Good news is I was able to easily slot this into the underlying Rust library: https://github.com/kivikakk/comrak/pull/347

Bad news is we'll have to wait for that to be accepted and merged before Commonmarker can make use of it. Sorry!

zachahn commented 7 months ago

Thank you for looking into this so quickly!

kivikakk commented 7 months ago

@gjtorikian Thanks for that :3 Comrak 0.20.0 has been released with that PR merged!