13

While going through the edit queue I came across an edit to this answer. I rejected it for two reasons; firstly it wasn't very substantive and secondly (the reason for this post) it was simply wrong - javascript code blocks should start with ```js rather than ```javascript (example shown at the bottom of the post).

Had the edit been more substantive or the code block larger I would have simply improved the edit by correcting their mistake.

Before I go further I should make it clear that I am not taking issue with the edit (though it wasn't the greatest I do agree with the the editors intent; highlighting syntax does make a post easier to read when you have a large block of code). My issue is how we, or rather, the site, can help users get it right first time.

I can't blame the editor for their mistake; if I look to the right of this box I'm typing into right now I see this -

How to Format

Which tells me that if I am entering a block of Python code I add python after the backticks. Seeing this I think -

Ah, so if I want code highlighting I need to add the language name after the backticks! So as I am adding Javascript code I add 'javascript'.

As mentioned above, in this case I would be wrong. But still how would I know that? Short of editing and checking the rendered markup below? But in this instance, should I need to? I read the "How to Format" help box and followed the convention it suggested.

While this question relates to the Javascript language I'm sure there will be others where the language identifier is not immediately obvious to new users. With this in mind could I suggest an addition to the "How to Format" help box with a link to a page with all accepted language identifiers?


```javascript

function example()
{
  var string = "something";
  return string;
}

```js

function example()
{
  var string = "something";
  return string;
}
1
  • 5
    According to the answer here you should be able to use either the language code or the name of a tag. So this may be a bug? Edit: actually, it seems the javascript tag uses default syntax highlighting instead of lang-js. Not sure if that's intended.
    – TiiJ7
    Commented Oct 22, 2019 at 9:49

1 Answer 1

13

When you use ```[language] (or <!—- language: [language] -->) where the language hint doesn’t start with lang-, the actual language to use is taken from the default language setting for the tag named language. So ```javascript takes the language setting listed at the bottom of the javascript tag wiki.

For years now the tag has been set to use lang-default, which is a “best effort” highlighter, not specific to any language. But both ```[tagname] and <!—- language: [tagname] --> will not use lang-default, presumably because that conflicts with the explicit choice you made. So you end up with no highlighting at all.

The motivation for the lang-default choice was that posts tagged with javascript have a high likelihood of containing HTML.

But with the addition of both snippets and ``` code-fences, it is now so much easier to add the correct language hints to code blocks, that I think the time has come to change the syntax highlighter default to lang-javascript. And I am not so sure anymore that the majority of JS questions and answers also include HTML these days.

I’ve made this change now, we’ll see how this pans out!

At worst I just made a whole series of HTML in posts tagged only as look slightly worse as the HTML sections will use the wrong highlighter (e.g. such as in this example, which I fixed by adding a language hint).

As for your request: technically speaking, the full list is all tags on Stack Overflow. And that list changes constantly; you can’t put that list in the help text really. The shorter lang-... list is also changing from time to time as Stack Exchange updates their prettier.js deployment. So, it’s complex and not an easy list to generate or show. The central FAQ on the subject currently advices you to look through the deployed JavaScript source code, even.

4
  • "...changing from time to time as Stack Exchange updates their prettier.js deployment." Really, now? When was the last time this happened?
    – Cody Gray Mod
    Commented Oct 22, 2019 at 16:01
  • @CodyGray: I don't have dates, but lang-rust.js was added in at some point. Commented Oct 22, 2019 at 16:05
  • @CodyGray: that said, I don't see any changes past Jan 2017 show up in the version served by the CDN. At least it is newer than this most recent evidence of an update. Commented Oct 22, 2019 at 16:11
  • Thanks for explaining the logic. I would have never quessed that syntax ```javascript refers to tag, not the name of the language for that code block. Why do they not have a list of language names and only use fallback to tag default when unknown value is used? Commented May 10 at 14:51

You must log in to answer this question.

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