tl;dr from T.J. Crowder's comment:
- A question without a language tag using Java or JavaScript syntax doesn't get syntax highlighted.
- Adding
<!-- language: java -->
to it gives it Java syntax highlighting. - 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 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.
- 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.
lang-
prefixes are consistently required. In the language codes section, each of them has that prefix, including the javascript one.<!-- language: java -->
working: stackoverflow.com/questions/53252638/…<!-- 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.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.<!-- language: lang-js -->
works for me as does<!-- language: lang-ts -->
for typescript<!-- language: matlab -->
, never needed to dolang-matlab
(on posts also tagged with Python, which seems to get priority over MATLAB in syntax highlighting, so much for "PR.prettyPrint() will guess.").<!-- 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 forlang-java
) that isn't enabled for JavaScript.--
followed by a space get replaced by a dash. Does it work in comments-- no.