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

Custom Language Syntax Highlighting #657

Open
brendanzab opened this issue Apr 2, 2018 · 11 comments
Open

Custom Language Syntax Highlighting #657

brendanzab opened this issue Apr 2, 2018 · 11 comments
Labels
A-Highlighting Area: Code colored syntax highlighting

Comments

@brendanzab
Copy link
Member

I'm currently using mdBook for the documentation of my Pikelet language. I want to be able to add my own, custom language to highlight.js.

What would be the best way to do this? At the moment the languages seem to be minified into the highlight.js code. Will I need to provide a custom rebuild of highlight.js instead? Would we be able to have support for custom languages in the future?

@brendanzab
Copy link
Member Author

cc. @Marwes - I know you're starting to use mdBook for Gluon as well.

@Marwes
Copy link

Marwes commented Apr 2, 2018

I tried to use highlight.js to get syntax highlighting on http://gluon-lang.org/try/ a couple of weeks ago by dynamically adding the necessary definitions. Unfortunately all the distributions of highlight.js are minified so it weren't feasible. Not sure how mdbook does it now, but to get highlighting for custom languages I think a full custom build of highlight.js may be needed.

@brendanzab
Copy link
Member Author

brendanzab commented Apr 2, 2018

Yeah, I'm thinking that it might require you to use a custom fork, which is a little disappointing. Might have to include it as a submodule. 😭

@brendanzab
Copy link
Member Author

I'm guessing this would also be a problem for LALRPOP too.

@Michael-F-Bryan
Copy link
Contributor

I've actually wanted this in the past when documenting some legacy code at work, but I'm not sure how we'd actually do it. A while back we threw around the idea of using a build system for our frontend code, but decided against it (@mattico, should we revisit this?).


Not sure how mdbook does it now, but to get highlighting for custom languages I think a full custom build of highlight.js may be needed.

Looking at the highlight JS README, it seems like you can just include a link to your pre-compiled language and it should be picked up automatically.

The CDN-hosted package doesn't have all the languages. Otherwise it'd be too big. If you don't see the language you need in the "Common" section, it can be added manually:

<script src="//cdnjs.cloudflare.com/ajax/libs/highlight.js/9.4.0/languages/go.min.js"></script>

@brendanzab
Copy link
Member Author

brendanzab commented Apr 2, 2018

Hmm, maybe if you built it with the node tools/build.js -t cdn option it would give you the separate minified language files… 🤔 - would still need to include a fork of the repo as a submodule though, it doesn't really seem set up all that well to be extensible.

For now I've gone with the hacky approach! See brendanzab/pikelet#21.

@Michael-F-Bryan
Copy link
Contributor

It's annoying, but that ci/build-highlight-js script of yours is probably going to be the best solution. Unless highlight.js publish their tools/build.js script to NPM so we can generate custom highlighting packs locally.

@brendanzab
Copy link
Member Author

I mean, if we really wanted to head down the rabbit hole we could always fork highlight.js and update it to modern JS 😱

@ehuss ehuss added the A-Highlighting Area: Code colored syntax highlighting label May 17, 2019
@XVilka
Copy link

XVilka commented May 30, 2019

What about using https://github.com/trishume/syntect instead? And don't force the readers to run more JS than is necessary.

brendanzab added a commit to brendanzab/fathom that referenced this issue Sep 28, 2020
I’ve tried using a patched version of highlight.js in the past, but it’s a pain to keep it up to date. Instead I’m using a version of highlight.js sorced from npm, rather than the minified version included in mdBook. This requires stubbing out the global `hljs` object in `theme/highlight.js` to avoid any method not found exceptions coming from mdBook’s generated book.js file. Ideally there’d be an easier way to do this in mdBook itself (see rust-lang/mdBook#657).
@raviqqe
Copy link

raviqqe commented Sep 22, 2021

I found a quick and dirty way. We can just reload highlight.js again after language registration. pen-lang/pen#395

@Atreyagaurav
Copy link

@raviqqe Thank you for sharing your fix. This was the only thing I was able to find that worked for custom highlight.

But since that is not mentioned in mdbook or highlight.js, isn't there a high chance that the custom highlight code will break when the highlight.js version change happens? Do you think we should make an issue upstream so that they don't change the API too much, or give a way to register custom languages like this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-Highlighting Area: Code colored syntax highlighting
Projects
None yet
Development

No branches or pull requests

7 participants