Markdown Syntax highlighting (StackExchange)


Example

On StackExchange sites, code snippets may provide optional syntax highlighting. On sites like Stack Overflow the default language is derived from the tags used in the associated question (if applicable). In addition, a code snippet's syntax highlighting language may also be defined by adding an HTML comment to the text body.

<!-- language: lang-vb -->

    Sub ShowVB()
    Dim i As Long
    For i = 1 To 2
        If i = 3 Then
            MsgBox "How did that happen?"
        End If
    Next
    End Sub

Such a comment will change the syntax highlighting language for all subsequent code snippets, which can be rather useful, especially when several languages are involved in one post.

The above will be rendered with Visual Basic highlighting as:

Sub ShowVB()
Dim i As Long
For i = 1 To 2
    If i = 3 Then
        MsgBox "How did that happen?"
    End If
Next
End Sub