Post

Today I shipped a huge change to syntax highlighting in Discord: code blocks and file previews on web are now being highlighted by Tree-sitter, instead of highlight.js.

8 reposts67 likes

SUPER COOL!!!! Rebane's tool made this update fun. 😄 I hope one of these days a syntax highlighter will support Ren'Py out of the box, but it is a particularly challenging language to parse, since it is, like, three languages in one (ATL, Screen Language, regular Ren'Py, Python expressions(!!)...)

Well the good news is 1) someone has already built a tree-sitter grammar for any language you can think of, and 2) our highlighter is open source. While we are unlikely to invest in maintaining languages like this, you're welcome to.

That was one of the factors in deciding to move forward with tree-sitter, since a native solution can run on mobile as well. There are some unique technical challenges in getting something like this on mobile, so I can't promise any sort of timeline, but it's something I want to do eventually.

You'll note that this came with some improvements to our ANSI colors 😉 (although ANSI coloration is handled separately from regular syntax highlighting)

Why, you might ask? highlight.js is a genuinely great project, but it suffers from one great weakness: regular expressions. This comes with 2 main downsides: RegEx is often vulnerable to denial-of-service, and it has no semantic understanding of the code.

highlight.js is fairly resilient to ReDoS, but even so we'd occasionally have to field some security reports about it. Tree-sitter is not immune to DoS either, but we can tune it to protect against DoS in ways that aren't possible with RegEx.

More importantly, however, tree-sitter is semantically aware, meaning that it can highlight code in context rather than based on keywords and pattern matching. This means that your code blocks in Discord are now as accurate as the best code editors. For example, None is not a keyword anymore.

Shout-out to the tree-sitter contributors and the hundreds of independent grammar authors for creating such a strong ecosystem, and shout-out @fasterthanli.me for building Arborium (the basis for our new syntax highlighter).