Invalid code
editThe grammars flag some constructs as illegal, such as a character where the language does not allow one. By default, an illegal lexeme is written as plain text and the rest of the code is still highlighted, so a snippet with a typo, or a fragment cut from a larger file, is still readable.
Set HighlightOptions.IgnoreIllegals to false to get the whole result as plain text instead, as soon as the code has an illegal lexeme:
var code = """{"a": 1};""";
var html = SyntaxHighlighter.Highlight(code, "json", new HighlightOptions { IgnoreIllegals = false });
Assert.Equal("{"a": 1};", html);The result is still HTML-encoded, so it is safe to put in a page. Embedded languages, such as CSS in HTML or C# in Razor, always ignore illegal lexemes.
The highlighter does not report errors: to check that code is valid, use a parser, such as the Language packages for the languages they support.