Syntax highlighting code with Javascript

JavascriptHtmlSyntaxSyntax Highlighting

Javascript Problem Overview


What Javascript libraries can you recommend for syntax highlighting <code> blocks in HTML?

(One suggestion per answer please).

Javascript Solutions


Solution 1 - Javascript

StackOverflow uses the Prettify library.

Solution 2 - Javascript

I recently developed one called rainbow.

The main design goal was to make the core library really small and make it really easy for developers to extend.

See http://rainbowco.de.

Solution 3 - Javascript

SyntaxHighlighter is available as a GitHub project.

Solution 4 - Javascript

jQuery Syntax Highlighter is a new one based on Google's Prettify - a really really really popular plain javascript syntax highlighter.

It supports such things as code and pre blocks, able to use classnames like language-javascript to indicate we want it to highlight, as well as wordwrap. You can copy and paste code by selecting it normally instead of having to open a raw view like many others. It can be further customised by using the HTML5 data attribute data-sh or via specifying options at initialisation. A great stable choice which is updated regularly.

Solution 5 - Javascript

What about Prism by Lea Verou.

From her blog post announcement in June (2012):

> - It’s tiny. The core is only 1.5KB minified & gzipped. > - It’s incredibly extensible. Not only it’s easy to add new languages (that’s a given with every syntax highlighter these days), but also to > extend existing ones. > - It supports parallelism through Web Workers, for better performance in certain cases. > - It doesn’t force you to use any Prism-specific markup, not even a Prism-specific class name, only standard markup you should be using > anyway. So, you can just try it for a while, remove it if you don’t > like it and leave no traces behind.

Solution 6 - Javascript

Solution 7 - Javascript

If you're using jQuery there's Chilli:

http://code.google.com/p/jquery-chili-js/

All you have to do is include the jquery-chili.js and recipes.js, and do the highlight with

$("code").chili();

It should figure out the language by itself.

Solution 8 - Javascript

I'm very happy with SHJS. It supports a bevy of languages and seems pretty fast and accurate.

Here's an example where I use it on my blog. I'm using my own custom CSS file that simulates Coda's syntax highlighting. Email me if you'd like to use it.

Solution 9 - Javascript

jQuery.Syntax is an extremely fast and lightweight syntax highlighter. It has dynamic loading of syntax source files and integrates cleanly using CSS or modelines.

It was developed specifically to fill a gap - that is: a fast, clean, client-side syntax parser.

Solution 10 - Javascript

I'm not being argumentative but just thought it worth mentioning that if you're using a CMS or blog platform then using a backend highlighter is better for obvious reasons — Have a look at Geshi(http://qbnz.com/highlighter/) if you're interested. Actually you could set up your server to parse HTML content through a backend technology — so there is no need for the JS highlighters at all. (The only functionality they add is the ability to print/copy[using swf].)

Solution 11 - Javascript

If you are looking for syntax highlighting in an in-browser editor, try CodeMirror.

Solution 12 - Javascript

Solution 13 - Javascript

This article at the Web Resources Depot lists a bunch of options for highlighting code, some of which use Javascript. It was published on 4th May 2009.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionmlambieView Question on Stackoverflow
Solution 1 - JavascriptAaronSiebView Answer on Stackoverflow
Solution 2 - JavascriptCraigView Answer on Stackoverflow
Solution 3 - JavascriptmlambieView Answer on Stackoverflow
Solution 4 - JavascriptbaluptonView Answer on Stackoverflow
Solution 5 - JavascriptIlya TuvschevView Answer on Stackoverflow
Solution 6 - JavascriptChris PietschmannView Answer on Stackoverflow
Solution 7 - JavascriptEdu FelipeView Answer on Stackoverflow
Solution 8 - JavascriptAndrew HedgesView Answer on Stackoverflow
Solution 9 - JavascriptioquatixView Answer on Stackoverflow
Solution 10 - JavascriptJamesView Answer on Stackoverflow
Solution 11 - JavascriptrplevyView Answer on Stackoverflow
Solution 12 - JavascriptFranci PenovView Answer on Stackoverflow
Solution 13 - JavascriptmlambieView Answer on Stackoverflow