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.

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.

0 reposts10 likes

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).

i'm super interested to add some grammars. would you be willing to put up some documentation on how to add one via PR?

I'll see if I can put something together tomorrow, but it's fairly straightforward if you look at the existing languages we've had to modify. The main challenge is building the project locally since it requires some more niche tooling.

this broke the css classes going from `{alias}` to something like `language-{main}`. example being `hbs` => `language-html`. they have different use cases. please, at the very least revert that change. the `language-` prefix doesn't matter here. keep the aliases separate.

I don't follow, is this some issue with CSS highlighting or with some missing languages? It's true that some languages got aliased to a close equivalent, which I figured was better than completely dropping support for the language.

So we're just gonna use formatting for similar languages now instead of actually using the correct highlighter? that sure isn't gonna cause confusion. I can imagine the CSS is gonna be hell to deal with for this…

I believe you're asking a similar question, although I'm still unsure what this has to do with CSS. bsky.app/profile/wnel...

Will Nelson@wnelson.dev

I don't follow, is this some issue with CSS highlighting or with some missing languages? It's true that some languages got aliased to a close equivalent, which I figured was better than completely dropping support for the language.