35

Update: I've gone ahead and updated the FAQ with the specified changes


I recently became aware of the fact that the full list of language identifiers for the syntax highlighter on SE is a bit... mysterious. There's lots of confusion about it, and the list in the FAQ is a bit unreliable, mostly because it's not backed by anything official, and it has often been edited by those with good intentions but poor information (At time of writing there are 102 revisions; for reference, the final release of Google Prettify was in December of 2015, that predates Revision 52).

Having been interested in the recent syntax highlighting changes, and having discovered some issues with the highlighting lately myself, I decided to take a dive into the full, official list of languages Stack Exchange currently supports, found scattered around in SE's highlight-loader.js file (referenced by the FAQ post). Using a little regex to find all the loaded languages and aliases1, I pulled together the (current) master list.

(For spatial brevity, I've enclosed it within a table in a snippet)

// Table-Making JS Code courtesy of Manish Mulani at https://stackoverflow.com/a/11480797/11047824
var myList=[{Language:"Default",Aliases:"[lang-default]"},{Language:"None",Aliases:"[lang-none]"},{Language:"Bash",Aliases:"[lang-bash], [lang-sh], [lang-zsh]"},{Language:"C-like",Aliases:"[lang-c-like], [lang-c], [lang-cc], [lang-h], [lang-c++], [lang-h++], [lang-hpp], [lang-hh], [lang-hxx], [lang-cxx]"},{Language:"C",Aliases:"[lang-c]"},{Language:"C++",Aliases:"[lang-cpp]"},{Language:"C#",Aliases:"[lang-csharp], [lang-cs], `lang-c#`"},{Language:"Clojure",Aliases:"[lang-clojure], [lang-clj]"},{Language:"CoffeeScript",Aliases:"[lang-coffeescript], [lang-coffee], [lang-cson], [lang-iced]"},{Language:"CSS",Aliases:"[lang-css]"},{Language:"Dart",Aliases:"[lang-dart]"},{Language:"Delphi",Aliases:"[lang-delphi], [lang-dpr], [lang-dfm], [lang-pas], [lang-pascal], [lang-freepascal], [lang-lazarus], [lang-lpr], [lang-lfm]"},{Language:"Erlang",Aliases:"[lang-erlang], [lang-erl]"},{Language:"Go",Aliases:"[lang-go], [lang-golang]"},{Language:"Haskell",Aliases:"[lang-haskell], [lang-hs]"},{Language:"Http",Aliases:"[lang-http], [lang-https]"},{Language:"Ini",Aliases:"[lang-ini], [lang-toml]"},{Language:"Java",Aliases:"[lang-java], [lang-jsp]"},{Language:"JavaScript",Aliases:"[lang-javascript], [lang-js], [lang-jsx], [lang-mjs], [lang-cjs]"},{Language:"JSON",Aliases:"[lang-json]"},{Language:"Kotlin",Aliases:"[lang-kotlin], [lang-kt]"},{Language:"LaTeX",Aliases:"[lang-latex], [lang-tex]"},{Language:"Less",Aliases:"[lang-less]"},{Language:"Lisp",Aliases:"[lang-lisp]"},{Language:"Lua",Aliases:"[lang-lua]"},{Language:"Makefile",Aliases:"[lang-makefile], [lang-mk], [lang-mak]"},{Language:"Markdown",Aliases:"[lang-markdown], [lang-md], [lang-mkdown], [lang-mkd]"},{Language:"MATLAB",Aliases:"[lang-matlab]"},{Language:"Objective-C",Aliases:"[lang-objectivec], [lang-mm], [lang-objc], [lang-obj-c]"},{Language:"OCaml",Aliases:"[lang-ocaml], [lang-ml]"},{Language:"Perl",Aliases:"[lang-perl], [lang-pl], [lang-pm]"},{Language:"PHP-Template",Aliases:"[lang-php-template]"},{Language:"PHP",Aliases:"[lang-php], [lang-php], [lang-php3], [lang-php4], [lang-php5], [lang-php6], [lang-php7]"},{Language:"Plaintext",Aliases:"[lang-plaintext], [lang-text], [lang-txt]"},{Language:"Protobuf",Aliases:"[lang-protobuf]"},{Language:"Python",Aliases:"[lang-python], [lang-py], [lang-gyp], [lang-ipython]"},{Language:"R",Aliases:"[lang-r]"},{Language:"Ruby",Aliases:"[lang-ruby], [lang-rb], [lang-gemspec], [lang-podspec], [lang-thor], [lang-irb]"},{Language:"Rust",Aliases:"[lang-rust], [lang-rs]"},{Language:"Scala",Aliases:"[lang-scala]"},{Language:"Scheme",Aliases:"[lang-scheme]"},{Language:"SCSS",Aliases:"[lang-scss]"},{Language:"Shell",Aliases:"[lang-shell], [lang-console]"},{Language:"SQL",Aliases:"[lang-sql]"},{Language:"Swift",Aliases:"[lang-swift]"},{Language:"TypeScript",Aliases:"[lang-typescript], [lang-ts]"},{Language:"VB.Net",Aliases:"[lang-vbnet], [lang-vb]"},{Language:"VHDL",Aliases:"[lang-vhdl]"},{Language:"XML",Aliases:"[lang-xml], [lang-html], [lang-xhtml], [lang-rss], [lang-atom], [lang-xjb], [lang-xsd], [lang-xsl], [lang-plist], [lang-wsf], [lang-svg]"},{Language:"YAML",Aliases:"[lang-yaml], [lang-yml], [lang-YAML]"}];function buildHtmlTable(a){for(var l=addAllColumnHeaders(myList,a),g=0;g<myList.length;g++){for(var n=$("<tr/>"),e=0;e<l.length;e++){var s=myList[g][l[e]];null==s&&(s=""),n.append($("<td/>").html(s))}$(a).append(n)}}function addAllColumnHeaders(a,l){for(var g=[],n=$("<tr/>"),e=0;e<a.length;e++){var s=a[e];for(var i in s)-1==$.inArray(i,g)&&(g.push(i),n.append($("<th/>").html(i)))}return $(l).append(n),g}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><body onLoad="buildHtmlTable('#excelDataTable')"><table id="excelDataTable" border="1"></table></body>

As I expected, I discovered a fair amount of discrepancies between the FAQ list and the master list. I went ahead and compiled the following list of those differences, cross-referencing with the highlight.js supported language list, and touching on each language or language group mentioned in the FAQ that either is missing an ID or has one or more extra IDs that are unrecognized by highlight.js. Apologies in advance for how long the following list is.

  • Bash: Remove lang-bsh & lang-csh, add lang-zsh
  • C/ C-likes: Remove lang-cyc & lang-m
  • C and C++: Caveats
    • Currently, highlight.js does not internally distinguish between C, C-like, and C++ (reference)
    • However, there are plenty of identifiers for both and more, some of which aren't currently in the FAQ
      • Full list: lang-c-like, lang-c, lang-cc, lang-h, lang-c++, lang-h++, lang-cpp, lang-hpp, lang-hh, lang-hxx, lang-cxx
  • C#: Add lang-c#
  • CoffeeScript: Add lang-cson & lang-iced
  • Go: Add lang-golang
  • Haskell: Add lang-haskell
  • Http2: Not currently included, add all:
    • lang-http, lang-https
  • HTML/ XML: Add lang-xhtml, lang-rss, lang-atom, lang-xjb, lang-xsd, lang-plist, lang-wsf, lang-svg
  • Ini/ TOML: Not currently included, add all:
    • lang-ini, lang-toml
  • Java: Add lang-jsp
    • Officially JSP isn't exactly Java, but the highlighter supports the identifier
  • Javascript: Add lang-jsx, lang-mjs, lang-cjs
  • Kotlin: Add lang-kt
  • Less: Not included, add all:
    • lang-less
  • Lisp, Scheme: Remove all except lang-lisp, add lang-scheme
  • Makefile: Not included, add all:
    • lang-makefile, lang-mk, lang-mak
  • Markdown: Not included, but... is this one relevant?
    • All: lang-markdown, lang-md, lang-mkdown, lang-mkd
  • Objective-C: Add lang-mm, lang-objc, lang-obj-c
  • "OCaml, SML, F#, et al."
    • OCaml: Add lang-ocaml
    • SML: Not included in SE's current load file
      • Highlight.js supports SML, SE just didn't include it
        • Special note: If SML is added, lang-ml shouldn't be used for OCaml, due to alias overlap
    • F#: Not included in SE's current load file
      • Highlight.js supports F#, SE just didn't include it
      • Until added, remove lang-fs
  • Pascal, Delphi: Add lang-delphi, lang-dpr, lang-dfm, lang-pas, lang-freepascal, lang-lazarus, lang-lpr, lang-lfm
  • Perl: Add lang-pm
  • PHP: Not included, add all:
    • lang-php, lang-php3, lang-php4, lang-php5, lang-php6, lang-php7
  • PHP-Template: Not included, add all:
    • lang-php-template
  • Plaintext: Plain text support is also available, but it's practically equivalent to lang-none
    • lang-plaintext, lang-text, lang-txt
  • Protocol Buffers: lang-proto doesn't exist, it should be lang-protobuf
    • 's default language should be fixed to point to the real identifier
  • Python: Add lang-gyp, lang-ipython, Remove lang-cv
  • "R, S"
    • S is unsupported by highlight.js, remove lang-s
  • Regex: Unsupported, remove altogether
  • Ruby: Add lang-gemspec, lang-podspec, lang-thor, lang-irb
  • Rust: Remove lang-rc
  • SCSS: Not included, add all:
    • lang-scss
  • Shell: Not included, add all:
    • lang-shell, lang-console
  • VHDL: Remove lang-vhd
  • "Visual Basic, VBScript":
    • Visual Basic: hightlight.js only supports VB.Net, not sure whether that covers original VB or not
      • IDs: lang-vbnet, lang-vb
    • VBScript: Not included in SE's current load file
      • Highlight.js supports VBScript, SE just didn't include it
        • IDs: lang-vbscript, lang-vbs
      • Until added, remove lang-vbs
  • YAML: Add lang-yml

I'm aware that this is an... intense amount of content to drop in one post. I wanted to compile it all in one public place for reference, however, so that the list can eventually be brought up to par with the real list currently in use.

My apprehension to editing directly without posting is that any significant edits made to the FAQ language list would have likely been rolled back without a post to refer back to (the mod note below the post asks editors to link to a meta post to confirm changes).

I also hope that by putting this out in the open, some confusion might be cleared up regarding which tags are and aren't officially in use, and why some tags that are in the FAQ list aren't working as anticipated.

Bonus related note:
I also learned recently, courtesy of T.J.Crowder in his recent post, that using ```lang-X to identify a code block is different than using ```X, because X alone refers to a tag, rather than a language. When you use X above a code block, you're telling the renderer to look at the tag's default language highlighting setting to determine how it should highlight. Most of the time, this is perfect. But sometimes, it yields unexpected results, like with typescript, which has lang-js set as the tag default language.


1 - /hljs.registerLanguage\(('.*')|^\s+aliases: (\[.*\])/
2 - Apparently this is supposed be for "HTTP request and response headers with automatic body highlighting" (Link)

11
  • 2
    Hmm, it looks like even the syntax highlighting options available to moderators when setting the default syntax highlighter for a tag are in need of an update. The current list misses many of the options you listed in your post, and also lists some that are apparently obsolete, like "lang-proto". The full list of what is currently available to us is here, but I can't vouch for its accuracy or completeness with all the recent changes to syntax highlighting: pastebin.com/bXVLPAg3 Commented Oct 21, 2020 at 4:37
  • 1
    The FAQ has been edited by the developer that was responsible for the highlight.js integration, I don’t think it needs to be dismissed directly. The edit count of the post reflects the long history of syntax highlighting on Stack Exchange. Commented Oct 21, 2020 at 7:07
  • 4
    This is an excellent effort, but what’s the goal here? Why not update the FAQ to reflect your findings? Commented Oct 21, 2020 at 7:17
  • 1
    To be fair @MartijnPieters - an edit like this is likely to be rolled back without some proof from somewhere. The people who actually compiled that list are animuson and Ben Kelly. Commented Oct 21, 2020 at 7:51
  • 1
    @benisuǝqbackwards the only edits to that post that have been rolled back where for languages added that were not in fact supported. If it was edited with a note citing having used a regex search though the loader I highly doubt that animuson or Sonic would roll back without first having verified their facts. Commented Oct 21, 2020 at 7:57
  • 1
    That's not what I meant @MartijnPieters - the original list, which is being criticised was compiled by animuson and Ben. Bad additions have been rolled back, but a major change to what animuson and Ben did without supporting documentation is likely to be rolled back, meaning that putting this question up with the proof is a decent way to ensure that editing the FAQ is successful. Commented Oct 21, 2020 at 8:28
  • @benisuǝqbackwards Bingo
    – zcoop98
    Commented Oct 21, 2020 at 14:51
  • @ben Added a note to the bottom of the post confirming that reasoning.
    – zcoop98
    Commented Oct 21, 2020 at 15:33
  • @MartijnPieters You're right about the FAQ, my intent wasn't/ isn't to dismiss it. And, the revision count isn't solely the fault of bad/ uninformed edits, you're right. But for reference, the final release of Prettify predates revision 52.
    – zcoop98
    Commented Oct 21, 2020 at 22:26
  • @zcoop98 If many of these are "legacy" the correct solution would likely be to leave them and add them as aliases to the "correct" language in the Highlight.js sense... not remove them... since they will continue to live on forever in old posts, etc... [Disclaim: Highlight.js maintainer] Commented Oct 23, 2020 at 22:09
  • @Josh I fully agree, that sounds like an excellent idea. Especially since the vast majority of changed IDs were just different, rather than removed, with a couple of exceptions.
    – zcoop98
    Commented Oct 25, 2020 at 17:21

1 Answer 1

6

I've written a script that gets a list of language codes and their aliases straight from Highlight.js version used by Stack Exchange, through Highlight.js API, and generates a Markdown formatting for embedding into a post:

var langNameOverride = {    // when name needs some tweaking
  bash:  'Bash and other shell scripts', c: 'C and C-likes', delphi: 'Delphi, Pascal',
  http:  'HTTP request/&#8203;response',                     ini:    'INI, TOML',
  javascript:     'JavaScript',                              latex:  'LaTeX, TeX',
  mathematica:    'Mathematica / Wolfram Language',          matlab: 'MATLAB',
  ocaml: 'OCaml, F#, SML and other ML-family languages',     php:    'PHP',
  'php-template': 'PHP template (HTML+PHP)',  r: 'R, S',     scheme: 'Scheme, Racket',
  vbnet: 'Visual Basic&nbsp;(.NET), VBScript',               shell:  'Shell session',
  xml:   'XML, HTML and their derivatives'
};
var langComments = {
  plaintext:   'explicitly disables syntax highlighting',
  mathematica: 'Mathematica&nbsp;SE only'
};
var markdown = `<!-- Generated by https://meta.stackexchange.com/a/372141/578924 using Highlight.js v${hljs.versionString} on ${(new Date()).toISOString()} -->
Format: **Language name: \`lang-code\`**, custom Stack Exchange aliases, (other Highlight.js aliases)\n\n`;

var languages = hljs.listLanguages();
languages.splice(languages.indexOf('plaintext'), 1);
languages.sort().unshift('plaintext');

for (var langCode of languages) {
  var lang = hljs.getLanguage(langCode), aliasesSE = '', langProcessed = false;
  for (var alias in customAliases) {
    if (customAliases[alias] != langCode)
      // custom aliases go in blocks per language,
      // no need to search further if the block is over
      if (langProcessed) break; else continue;

    aliasesSE += ', `lang-'+alias+'`';
    if (lang.aliases && (i = lang.aliases.indexOf(alias)) > -1)
      if (lang.aliases.length > 1)
        lang.aliases.splice(i, 1);
      else
        delete lang.aliases;
    langProcessed = true;
    delete customAliases[alias];        // don't check the processed aliases again
  }

  var aliases = '';
  if (lang.aliases)
    aliases = ', (`lang-'+lang.aliases.join('`, `lang-')+'`)';

  var langName = langNameOverride[langCode] || lang.name;
  var comment = langComments[langCode] || '';
  if (comment)
    comment = '  \n  ('+comment+')';
  markdown += '- **'+langName+': `lang-'+langCode+'`**'+aliasesSE+aliases+comment+'\n';
}
markdown += '<!'+'-'.repeat(90)+'>';
document.getElementById('markdown').textContent = markdown;

function copyLangListToClipboard() {
  // Majority kindly borrowed from https://stackoverflow.com/a/987376
  const node = document.getElementById('markdown');
  const selection = window.getSelection();
  const range = document.createRange();
  range.selectNodeContents(node);
  selection.removeAllRanges();
  selection.addRange(range);
  document.execCommand('copy');
}
<script>
    var StackExchange = {};             // prevents highlightjs-loader execution error

    // hack to get custom Stack Exchange aliases
    Object._keys = Object.keys;
    Object.keys = function(obj) {
        if (obj.none) {
            window.customAliases = obj;
            Object.keys = Object._keys;
        }
        return Object._keys(obj);
    };
</script>
<script id="webpack-public-path"></script>
<script src="https://cdn.sstatic.net/Js/highlightjs-loader.en.js"></script>
<script src="https://cdn.sstatic.net/Js/third-party/highlight.js/additional-langs/lang-mma.min.js"></script>

<button type="button" onclick="copyLangListToClipboard()">Copy to Clipboard</button>
<pre><code id="markdown"></code></pre>

There are also custom aliases defined by Stack Exchange for backward compatibility with Prettify. Some languages are intentionally aliased to similar ones to prevent overhead from having more languages. Most of them were listed by @animuson in revision 30.

In some special cases languages might be supported through an additional file. Currently this is only known to be used for Mathematica / Wolfram Language on Mathematica SE. Unfortunately, it's not possible to list them with JS due to CORS restrictions.

Stack Exchange has even ported a line numbers support from Prettify, but they are only supported in link previews (oneboxes) on private instances.


The reason I decided to write this script is because the process of determining language support on Stack Exchange was confusing, outdated and not really needed since the list of languages is already present at the end of the FAQ post. But I noticed some regression introduced by this edit and wanted to make sure it's up-to-date.

There are not many changes since the previous major edit: custom aliases and some language names were restored, some aliases were added and removed by Highlight.js itself, no new languages were found.

0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .