Experiment: Using syntect as a higlighting backend rather than highlight.js #1494
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
I would like to start out by saying that this is definitely not something that should be merged (yet). I'm mostly posting this PR to document how this could be potentially helpful (once made ready for production, as it's definitely not feature-complete yet), and to gauge the community's opinion of having this as an option in mdBook.
Background and Rationale
syntect is a library by Tristan Hume that is meant for syntax highlighting for static sites, command line applications, and more. It has interfaces for both CLI apps (with ANSI codes and similar), and for websites (with HTML and CSS). syntect uses Sublime Text syntax definitions, which are much more widespread and are easier to convert from the ubiquitous TextMate syntax definition format (which itself is used in editors like Atom (which recommends tree-sitter but I believe still supports TextMate syntax definitions), VSCode, and more). However, highlight.js uses its own format that is not compatible with TextMate or Sublime Text syntax definitions due to major architectural differences in how syntax is parsed. This is my main reason for creating this fork, but there are a few other benefits of using syntect.
Since syntect does all of the syntax highlighting when building a book, it means that page load times for chapters with lots of syntax highlighting would be theoretically significantly reduced. (I haven't tested this but it would make sense because syntax highlighting at build-time would cause the only impact to be from HTML/CSS, rather than JS and HTML/CSS).
It also means that adding a new language to the highlighter doesn't mean you have to modify the
index.hbsfile orhighlight.js(which I had to do for my programming language's books). Instead, you can just drop a.sublime-syntaxfile into a folder such assrc/theme/syntaxesand it will just work.Implementation
Right now this PR completely replaces highlight.js with syntect. I imagine that this is probably not what the majority of people would want, as some people may still have reasons for wanting to use highlight.js. Also, because the CSS generated by syntect from the
.sublime-color-schemefiles that I used is not exactly the same as the CSS that was used before for highlight.js, there are some differences in styling. There may also be some small bugs.Line hiding is not very efficient and definitely not bug-free at the moment, which is something I'll have to sort out eventually. Also, since now the effort of syntax highlighting is put into build-time rather than run-time (and because my code is... not the best to say the least), this does impact build time by a few seconds. I believe this probably scales up as the number of highlighted lines/code blocks increases, but I haven't done much testing. I'm sure that the performance could be improved, probably by moving syntax highlighting into a different stage of book building.
This build-time overhead is one possible reason why it may be helpful to allow users to choose between syntect and highlight.js — it's a tradeoff between build-time overhead and run-time overhead. Some users may choose one or the other.
I would also like to note that as Sublime Text syntax definitions are a bit more verbose on scoping than highlight.js, the size of generated HTML is somewhat increased. In my opinion, this probably isn't too big of a problem.
If you have any questions for me or find any bugs/things that could be improved, feel free to open an issue or PR on the forked repo. I'd rather not overwhelm the maintainers of mdBook with questions/improvements for my fork.
Checklist
This is a checklist of what minimally has to be done before this can be used well in production:
.sublime-color-schemeto CSS generator