Code2Img

How auto-detection works

Auto-detection is a short ordered list of regexes with one fallback. Here is the real order, the two collisions that surprise people, and when to set the language yourself.

"Auto" on Code2Img is not a model and not a guess about intent. It is an ordered list of eighteen regular expressions, most specific first, and a fixed fallback. Knowing the order is the difference between a correctly highlighted card and one where every type annotation is the same colour as every identifier.

The list, in order

The rules are tried from top to bottom and the first match wins. The head of the list is the specific stuff — Python's def … :, Ruby's end and attr_accessor, Go's package main and :=, Rust's fn, let mut and println! — and the tail is the generic JavaScript, TypeScript, YAML, JSON and shell rules. If nothing matches at all, the answer is typescript, deliberately: it is the most common case for a snippet that nobody labelled.

Collision one: TypeScript usually lands on JavaScript

The JavaScript rule sits above the TypeScript rule. It fires on a leading brace, async function, =>{, const, let, or console.. Almost every TypeScript snippet on earth contains a const, so the ordinary case is a snippet of TypeScript that is detected as JavaScript:

// detected as javascript
const timings = [1, 2, 3];
export function total(xs: number[]): number {
  return xs.reduce((a, b) => a + b, 0);
}

The TypeScript rule only gets its turn when JavaScript did not match — an interface or type declaration at the start of a line, an annotation that is exactly : string, : number or : boolean, or an import type. Paste the interface and detection is right; paste the implementation and the annotations lose their distinct colour, because the JavaScript grammar has no concept of a type position.

Collision two: function is the PHP rule too

The PHP rule matches <?php and, as a second alternative, function name( at the start of a line. That second alternative catches a great deal of other languages:

// detected as php
function load(x: string) {
  return x.trim();
}

PHP sits well above JavaScript in the list, so a snippet that opens with an ordinary function signature is claimed before JavaScript or TypeScript ever sees it. Java's public static void and C's int main( are separate rules near the same height for the same reason — the language's own signature is the strongest signal available, and it is worth being wrong for a bare function than wrong for a bare C program.

The other useful ones

Three rules do more work than their size suggests. The HTML rule fires on <div, <span or <!DOCTYPE, so a JSX fragment about a div becomes HTML. The CSS rule fires on @media, on any property: value; and on a selector line starting with a dot — a YAML file with a dotted key can look like CSS. And shell fires on #!/bin/sh, on echo $… or on a line starting with apt, curl, cd, mkdir or export.

What to do about it

Rule of thumb for anything you publish: set the language explicitly. Auto is a convenience for a quick paste, and the honest description of its behaviour is "eighteen ordered guesses, most specific first" — good at telling Python from Go, structurally unable to tell TypeScript from JavaScript at the top level, and quick to call a function PHP. On the API, that is the config.language field; in the editor, it is the language select. Leave it on auto for the snippet you are about to throw away, and set it for the one that goes in a README.