amyjko / bookish

A simple framework for publishing online books.
MIT License
69 stars 14 forks source link

Custom programming language highlighting #344

Open rbohrer opened 1 year ago

rbohrer commented 1 year ago

I'm assuming that the code highlighting feature uses some sort of syntax highlighting library for which you created or found theme files to support the given programming languages, if not, then this request might not apply.

The motivation is that my book is a programming languages textbook, and it includes programming languages where I would not really think it fair to ask you to go out of your way to implement them for me, but I'd like to implement them:

I would potentially be open to developing styles for these languages which you could make available to everybody, but I'm aware the quality may not be high enough for reuse

amyjko commented 1 year ago

This is definitely a good thing to support. There are a few barriers right now. The two major syntax highlighting libraries are PrismJS and HighlightJS. I chose Prism because it's more modular, which is key for being able to ship books with different language support dynamically with Svelte. Unfortunately, making this work requires ESM support, which PrismJS will release with v2, which isn't out yet, and progress seems to have stalled. Switching to HighlightJS isn't really an option either, since it uses a DOM manipulation approach, which breaks with Svelte. No good solutions for this yet that don't involve manually adapting Prism language definitions, hence the limited language support.

One workaround for this would be just adding Rust support. It wouldn't increase the size of the book bundles too much to add this, and it's common enough. See ExtendedPrism.js for where the manual language definitions are added.

rbohrer commented 1 year ago

Thanks for the rundown. I'm focusing on content before highlighting but if I ever write my own style files I'll absolutely send them your way

On Sat, Jun 10, 2023, 11:43 AM Amy J. Ko @.***> wrote:

This is definitely a good thing to support. There are a few barriers right now. The two major syntax highlighting libraries are PrismJS https://prismjs.com/ and HighlightJS https://highlightjs.org/. I chose Prism because it's more modular, which is key for being able to ship books with different language support dynamically with Svelte. Unfortunately, making this work requires ESM support, which PrismJS will release with v2, which isn't out yet, and progress seems to have stalled. Switching to HighlightJS isn't really an option either, since it uses a DOM manipulation approach, which breaks with Svelte. No good solutions for this yet that don't involve manually adapting Prism language definitions, hence the limited language support.

One workaround for this would be just adding Rust support. It wouldn't increase the size of the book bundles too much to add this, and it's common enough. See ExtendedPrism.js https://github.com/amyjko/bookish/blob/2c927dc08d5361e6445386f09ac4e27db705e5eb/src/lib/components/chapter/ExtendedPrism.js#L5 for where the manual language definitions are added.

— Reply to this email directly, view it on GitHub https://github.com/amyjko/bookish/issues/344#issuecomment-1585710635, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAO6WYO4VVRXZ2U6CDVXRIDXKSI3ZANCNFSM6AAAAAAY3UVKFU . You are receiving this because you authored the thread.Message ID: @.***>