22

tl;dr from T.J. Crowder's comment:

  1. A question without a language tag using Java or JavaScript syntax doesn't get syntax highlighted.
  2. Adding <!-- language: java --> to it gives it Java syntax highlighting.
  3. Adding <!-- language: javascript --> to it does not give it JavaScript syntax highlighting; instead, you have to use <!-- language: lang-javascript --> or <!-- language: lang-js -->.

Yup, sounds like a bug -- or at least, a feature enabled for Java (java as a synonym for lang-java) that isn't enabled for JavaScript.


Original post:

I frequently edit posts to add JavaScript or Java syntax highlighting.

The way I've done that is by adding something like this to the top of the code in the post:

<!-- language: java -->
<!-- language: javascript -->

More info here: What is syntax highlighting and how does it work?

This appears to work for the Java edits, but does not work for the JavaScript edits.

However, if I use the lang syntax:

<!-- language: lang-javascript -->

Then it works okay.

From my understanding, I shouldn't have to use lang-javascript, and just javascript should work okay.

Just to test, here's a code block that uses <!-- language: javascript -->:

function hello(){
  console.log('hello');
}

And here's one that uses <!-- language: lang-javascript -->:

function hello(){
  console.log('hello');
}

Notice that the first block is not highlighted. Is this a bug, or am I incorrect in expecting <!-- language: javascript --> to work?

Edit: Some people have mentioned that the Java highlighting might be automatically triggered. However, if I don't add <!-- language: java --> to the questions, then they are not automatically highlighted. If I add <!-- language: java --> then they are highlighted. Here is an example answer where I did not add it, and it's not highlighted. The question itself does have the <!-- language: java --> part, so it is highlighted.

  • Here is an example of <!-- language: lang-javascript --> working.
  • Here is an example of <!-- language: javascript --> not working.
  • Here is an example answer where I did not add any language markup, and it's not automatically highlighted. I did edit the question to include the language markup, so it is highlighted.
19
  • 10
    It looks like the lang- prefixes are consistently required. In the language codes section, each of them has that prefix, including the javascript one.
    – ryanyuyu
    Commented Nov 15, 2018 at 17:57
  • 5
    Just checking: do those posts you add a language to happen to be tagged with something whose default language is already set to something that happens to highlight appropriately anyway? Commented Nov 15, 2018 at 18:00
  • 1
    @ryanyuyu But shouldn't you also be able to use a tag? Here's an example of <!-- language: java --> working: stackoverflow.com/questions/53252638/… Commented Nov 15, 2018 at 18:01
  • @JonClements I don't think so. I mostly edit processing questions to include Java syntax highlighting, and p5.js questions to include JavaScript syntax highlighting. Commented Nov 15, 2018 at 18:01
  • 5
    I think that's luck @KevinWorkman. The syntax highlight just kinda guesses if you don't have the lang tag.
    – ryanyuyu
    Commented Nov 15, 2018 at 19:06
  • @ryanyuyu: yep. From Github: "You don't need to specify the language since PR.prettyPrint() will guess." I didn't check any deeper but I guess (😊) that's what it also does when you enter an unknown language.
    – Jongware
    Commented Nov 15, 2018 at 19:27
  • 5
    @usr2564301 The weird thing is, if I don't add <!-- language: java --> to processing questions, then they are not automatically highlighted. If I add <!-- language: java --> then they are highlighted. Here is an example answer where I did not add it, and it's not highlighted. The question itself does have the <!-- language: java --> part, so it is highlighted. Commented Nov 15, 2018 at 20:11
  • 1
    @KevinWorkman In your second example, adding the [javascript] tag makes language: javascript work (I checked it through the edit preview), so it seems this form of the language hint only works if the question has a matching tag.
    – duplode
    Commented Nov 15, 2018 at 21:55
  • 3
    @duplode Yes, questions with the javascript tag are automatically highlighted, without any language markup at all. I'm working with questions that are not tagged with javascript though. Commented Nov 15, 2018 at 21:56
  • 3
    For what its worth,<!-- language: lang-js --> works for me as does <!-- language: lang-ts --> for typescript
    – Suraj Rao
    Commented Nov 16, 2018 at 7:13
  • 1
    I have always used <!-- language: matlab -->, never needed to do lang-matlab (on posts also tagged with Python, which seems to get priority over MATLAB in syntax highlighting, so much for "PR.prettyPrint() will guess."). Commented Nov 16, 2018 at 18:05
  • 3
    So the TL;DR is: 1. A question without a language tag using Java or JavaScript syntax doesn't get syntax highlighted. 2. Adding <!-- language: java --> to it gives it Java syntax highlighting. 3. Adding <!-- language: javascript --> to it does not give it JavaScript syntax highlighting; instead, you have to use <!-- language: lang-javascript --> or <!-- language: lang-js -->. Yup, sounds like a bug -- or at least, a feature enabled for Java (java as a synonym for lang-java) that isn't enabled for JavaScript. Commented Nov 18, 2018 at 12:32
  • 1
    Did nobody else notice that the first two hyphens in the title got changed into an m-dash, but the second two didn't? I have problems looking past that. Yeah, I know it's me, but still. Instead of concentrating on the question, I'm now puzzling on what logic could be used to produce such a result.
    – Mr Lister
    Commented Nov 18, 2018 at 13:35
  • 1
    @MrLister The logic seems to be, any -- followed by a space get replaced by a dash. Does it work in comments-- no. Commented Nov 18, 2018 at 16:59
  • 1
    @T.J.Crowder Thanks for the summary. I added that to the top of my post. Commented Nov 18, 2018 at 21:00

2 Answers 2

5

If you use lang-..., it will use that language. If you use the name of a tag, it will use the default code language set for that type. If you look at the tag wiki for a tag, it will tell you the code language set for that tag. For Java it's lang-java, for JavaScript, it's default. So <!-- language: javascript --> will enable the default highlighting (meaning it will try to detect the language), not JavaScript highlighting, because that's the setting for the tag.

I assume it's done this way because questions tagged JavaScript contain code in other languages (such as HTML) more often than Java questions, so highlighting all code as JavaScript code in questions tagged JavaScript would lead to wrong highlighting more often.

2
  • 1
    That doesn't make sense. If you tag a question javascript, it gets syntax-highlighted. If you put <!-- language: javascript --> in it (without javascript), it doesn't. Commented Nov 19, 2018 at 7:59
  • @T.J.Crowder: Tagging with javascript used to set the default “best effort” highlighter. Using <!— language: [tagname] —> on the other hand will never fall back to the default highlighter, so you end up with nothing. I’ve now set the tag to use lang-js. Commented Oct 22, 2019 at 10:06
1

Martijn Pieters ♩ has fixed this now. From his comment:

Tagging with javascript used to set the default “best effort” highlighter. Using <!-- language: [tagname] --> on the other hand will never fall back to the default highlighter, so you end up with nothing. I’ve now set the tag to use lang-js.

...which means that a question with this markdown (and without the tag) gets syntax-highlighted correctly now:

Testing 1 2 3

<!-- language: javascript -->

    console.log("Testing 1 2 3");

Of course, JavaScript questions generally should have , but for the cases where there seems to be a good reason not to include it...

You must log in to answer this question.

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