Prism

# Used By

Prism is used on several websites, small and large. Some of them are:

It’s also used on the personal blog of Brendan Eich, creator of JavaScript itself!

# Examples

The Prism source, highlighted with Prism (don’t you just love how meta this is?):

This page’s CSS code, highlighted with Prism:

This page’s HTML, highlighted with Prism:

This page’s logo (SVG), highlighted with Prism:

If you’re still not sold, you can view more examples or try it out for yourself.

# Full list of features

- Only 2KB minified & gzipped (core). Each language definition adds roughly 300-500 bytes.
- Encourages good author practices. Other highlighters encourage or even force you to use elements that are semantically wrong, like `<pre>` (on its own) or `<script>`. Prism forces you to use the correct element for marking up code: `<code>`. On its own for inline code, or inside a <pre> for blocks of code. In addition, the language is defined through the way recommended in the HTML5 draft: through a language-xxxx class.
- The language definition is inherited. This means that if multiple code snippets have the same language, you can just define it once, in one of their common ancestors.
- Supports parallelism with Web Workers, if available. Disabled by default (why?).
- Very easy to extend without modifying the code, due to Prism’s plugin architecture. Multiple hooks are scattered throughout the source.
- Very easy to define new languages. Only thing you need is a good understanding of regular expressions
- All styling is done through CSS, with sensible class names rather than ugly namespaced abbreviated nonsense.
- Wide browser support: IE9+, Firefox, Chrome, Safari, Opera, most Mobile browsers
- Highlights embedded languages (e.g. CSS inside HTML, JavaScript inside HTML)
- Highlights inline code as well, not just code blocks
- Highlights nested languages (CSS in HTML, JavaScript in HTML)
- 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.
- Highlight specific lines and/or line ranges (requires plugin)
- Show invisible characters like tabs, line breaks etc (requires plugin)
- Autolink URLs and emails, use Markdown links in comments (requires plugin)

# Limitations

- Any pre-existing HTML in the code will be stripped off. There are ways around it though.
- Regex-based so it *will* fail on certain edge cases.
- No IE 6-8 support. If someone can read code, they are probably in the 85% of the population with a modern browser.

# Basic usage

You will need to include the `prism.css` and `prism.js` files you downloaded in your page. Example:

    <!DOCTYPE html>
    <html>
    <head>
    	...
    	<link href="themes/prism.css" rel="stylesheet" />
    </head>
    <body>
    	...
    	<script src="prism.js"></script>
    </body>
    </html>

Prism does its best to encourage good authoring practices. Therefore, it only works with `<code>` elements, since marking up code without a `<code>` element is semantically invalid. According to the HTML5 spec, the recommended way to define a code language is a `language-xxxx` class, which is what Prism uses. To make things easier however, Prism assumes that this language definition is inherited. Therefore, if multiple `<code>` elements have the same language, you can add the `language-xxxx` class on one of their common ancestors. This way, you can also define a document-wide default language, by adding a `language-xxxx` class on the `<body>` or `<html>` element.

If you want to opt-out of highlighting for a `<code>` element that is a descendant of an element with a declared code language, you can add the class `language-none` to it (or any non-existing language, really).

The recommended way to mark up a code block (both for semantics and for Prism) is a `<pre>` element with a `<code>` element inside, like so:

    <pre><code class="language-css">p { color: red }</code></pre>

If you use that pattern, the `<pre>` will automatically get the `language-xxxx` class (if it doesn’t already have it) and will be styled as a code block.

If you want to prevent any elements from being automatically highlighted, you can use the attribute `data-manual` on the `<script>` element you used for prism and use the API. Example:

    <script src="prism.js" data-manual></script>

If you want to use Prism on the server or through the command line, Prism can be used with Node.js as well. This might be useful if you're trying to generate static HTML pages with highlighted code for environments that don't support browser-side JS, like AMP pages.

You can install Prism for Node.js by running:

    $ npm install prismjs

Example:

    var Prism = require('prismjs');

    // The code snippet you want to highlight, as a string
    var code = "var data = 1;";

    // Returns a highlighted HTML string
    var html = Prism.highlight(code, Prism.languages.javascript);

# Supported languages

This is the list of all languages currently supported by Prism, with their corresponding alias, to use in place of `xxxx` in the `language-xxxx` class:

# Plugins

Plugins are additional scripts (and CSS code) that extend Prism’s functionality. Many of the following plugins are official, but are released as plugins to keep the Prism Core small for those who don’t need the extra functionality.

No assembly required to use them. Just select them in the download page.

It’s very easy to write your own Prism plugins. Did you write a plugin for Prism that you want added to this list? Send a pull request!

# Third-party language definitions

- SassDoc Sass/Scss comments

# Third-party tutorials

Several tutorials have been written by members of the community to help you integrate Prism into multiple different website types and configurations:

- Escape HTML Inside <code> or <pre> Tag to Entities to Display Raw Code with PrismJS
- How To Implement Prism.js Syntax Highlighting Into Your WordPress Site
- Adding a Syntax Highlighter Shortcode Using Prism.js | WPTuts+
- Implement PrismJs Syntax Highlighting to your Blogger/BlogSpot
- Beautify Source Codes In Your Posts With Prism Syntax Highlighter For Blogger
- Extending and customizing Prism syntax highlighter
- How To Re-Run Prism.js On AJAX Content
- Highlight your code syntax with Prism.js
- Jekyll Plugin: Syntax Highlighting With Prism
- [Spanish] Tutorial sobre Prism.js Como utilizarlo en Web, Wordpress y Blogger
- A code snippet content element powered by Prism.js for TYPO3 CMS

Please note that the tutorials listed here are not verified to contain correct information. Read at your risk and always check the official documentation here if something doesn’t work :)

Have you written a tutorial about Prism that’s not already included here? Send a pull request!

# Credits

- Special thanks to Golmote and Jannik Zschiesche for their contributions and for being amazing maintainers. Prism would not have been able to keep up without their help.
- To Roman Komarov for his contributions, feedback and testing.
- To Zachary Forrest for coming up with the name “Prism”
- To stellarr for the spectrum background used on this page
- To Jason Hobbs for encouraging me to release this script as standalone