3

As adequately explained in this post, Stack Overflow does not have its own syntax highlighting engine but uses Google Code Prettify. It is however possible to override this syntax in use with a language of your own choice.

But does it allow to mix different languages in one snippet? Apparently, the lexer has troubles to handle the following fragment where three languages are used.

<!-- language: lang-js -->
var a = 3;
while( !(a < 0)){
    alert("JavaScript code <b>goes</b> here.");
    a--;
}

<!-- language: lang-css -->
#content {
    margin: 0 auto;
    width: 80%;
}

<!-- language: lang-html -->  
<div id="content">
    <p>Different highlighting in the same snippet</p>
</div>

So, is it possible to mix syntax highlighting?

3
  • 1
    Not when the language hint comments are escaped in the code block... As you can see, when they're actually comments it's fine. By definition, therefore, you cannot mix in a single block.
    – jonrsharpe
    Jan 18, 2016 at 22:01
  • 1
    @jonrsharpe Erm, you fixed the post to remove the problem? Now we can't see what the issue was :/ Jan 19, 2016 at 0:25
  • I re-edited the snippet to its original context in order to have a clear view on what I meant. Jan 19, 2016 at 12:27

1 Answer 1

5

It's not possible to mix highlightings in a single code block, no. But if it's important to highlight each block in the appropriate language, its easy to just have consecutive code blocks with different highlighting, which will only be separated by a small white-space:

var a = 3;
while( !(a < 0)){
    alert("JavaScript code <b>goes</b> here.");
    a--;
}
#content {
    margin: 0 auto;
    width: 80%;
}
<div id="content">
    <p>Different highlighting in the same snippet</p>
</div>

You must log in to answer this question.

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