peacock
v1.0.0
Published
JavaScript syntax highlighter that generates pygments compatible html and therefore supports pygments styles.
Downloads
72
Maintainers
Readme
peacock
JavaScript syntax highlighter that generates pygments compatible html and therefore supports pygments styles.
Peacock (Pavo cristasus)
Installation
npm install peacock
Included demo
npm exlore peacock
npm run demo
Features and limitations
- works server side via nodejs
- works in the browser via browserify, AMD, if available, or as global window object
- only highlights JavaScript - if that is a dealbreaker look into some alternatives
API
peacock.defaultTheme
Returns Object the default peacock theme used to highlight code
peacock.spans
Returns Object the default peacock spans used to wrap code tokens
peacock.classes
Returns Object the default peacock classes used to highlight code
peacock.highlight
Highlights the proviced code or throws an error if it was not able to parse it.
Parameters
code
String to highlight$0
Object options$0.theme
(Object | String)? to use when highlighting empty sample (optional, defaultpeacock.defaultTheme
)$0.linenos
Boolean? iftrue
line numbers will be included (optional, defaultfalse
)$0.jsx
Boolean? iftrue
peacock will supportjsx
syntax (which makes highlighting a tad bit slower) (optional, defaulttrue
)
Returns String the HTML with containing the highlighted code
Browser support
AMD
Ensure to include redeyed and esprima as your dependencies
define(['peacock'], function (peacock) {
[ .. ]
});
Attached to global window object
peacock will be exposed as window.peacock
which allows calling peacock.highlight
from anywhere.
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/esprima/2.7.2/esprima.min.js"></script>
<script type="text/javascript" src="https://rawgit.com/thlorenz/redeyed/master/redeyed.js"></script>
<script type="text/javascript" src="https://rawgit.com/thlorenz/peacock/master/peacock-browser.js"></script>
Examples (browse)
- pageofself highlights itself using the tango style
- pageofself-hide-semicolons highlights itself using the fruity style and hides all semicolons
- amd browser example which uses requirejs to resolve peacock
- simple browser example which uses the global window object to access peacock
Alternatives
- for client side highlighting give SyntaxHighlighter a try
- for server side highlighting driven by nodejs, try node-syntaxhighlighter
- highlightjs is another client side option
- pygments is a great server side highlighter, granted you are running python