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.

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

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.

0 reposts0 likes

the aliasing itself in terms of highlighting isn't the issue, a lack of what alias is being used being exposed in the css is the issue

some screen readers may use the HTML element classes to know what the language of the codeblock is. `less` for whatever reason is now aliased with `css` despite having a slightly different use case. `svg` is now registered as `xml` even tho the spec is basically xml with flair. what's with `yuri`?

`tsx` was split off from `typescript`/`ts` despite `jsx` still being part of `javascript`/`js`? nothing about it is highlighting incorrectly afaik, it's more odd changes than anything. also third party themes can make use of the aliases to use the css to show what lang is being used

Do you have more information about this? My understanding is that CSS is not used in accessibility except for element visibility and interaction. Screen readers pulling specific labels off of CSS classes would be very surprising to me.