prism/index.html

381 lines
20 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<!DOCTYPE html>
<html lang="en">
<head>
<script>
// Just a lil script to show off that inline JS gets highlighted
window.console && console.log('foo');
</script>
<meta charset="utf-8" />
<link rel="icon" href="assets/favicon.png" />
<title>Prism</title>
<link rel="stylesheet" href="assets/style.css" />
<link rel="stylesheet" href="themes/prism.css" data-noprefix />
<script src="assets/vendor/prefixfree.min.js"></script>
<script>var _gaq = [['_setAccount', 'UA-33746269-1'], ['_trackPageview']];</script>
<script src="https://www.google-analytics.com/ga.js" async></script>
</head>
<body>
<header>
<div class="intro" data-src="assets/templates/header-main.html" data-type="text/html"></div>
<ul id="features">
<li>
<strong>Dead simple</strong>
Include prism.css and prism.js, use proper HTML5 code tags (<code>code.language-xxxx</code>), done!
</li>
<li>
<strong>Intuitive</strong>
Language classes are inherited so you can only define the language once for multiple code snippets.
</li>
<li>
<strong>Light as a feather</strong>
The core is 2KB minified &amp; gzipped. Languages add 0.3-0.5KB each, themes are around 1KB.
</li>
<li>
<strong>Blazing fast</strong>
Supports parallelism with Web Workers, if available.
</li>
<li>
<strong>Extensible</strong>
Define new languages or extend existing ones.
Add new features thanks to Prisms plugin architecture.
</li>
<li>
<strong>Easy styling</strong>
All styling is done through CSS, with sensible class names like <code>.comment</code>, <code>.string</code>, <code>.property</code> etc
</li>
</ul>
</header>
<section id="used-by">
<h1>Used By</h1>
<p>Prism is used on several websites, small and large. Some of them are:</p>
<div class="used-by-logos">
<a href="https://www.smashingmagazine.com/" target="_blank"><img src="assets/img/logo-smashing.png" alt="Smashing Magazine" /></a>
<a href="https://alistapart.com/" target="_blank"><img src="assets/img/logo-ala.png" alt="A List Apart" /></a>
<a href="https://developer.mozilla.org/" target="_blank"><img src="assets/img/logo-mdn.png" alt="Mozilla Developer Network (MDN)" /></a>
<a href="https://css-tricks.com/" target="_blank"><img src="assets/img/logo-css-tricks.png" alt="CSS-Tricks" /></a>
<a href="https://www.sitepoint.com/" target="_blank"><img src="assets/img/logo-sitepoint.png" alt="SitePoint" /></a>
<a href="https://www.drupal.org/" target="_blank"><img src="assets/img/logo-drupal.png" alt="Drupal" /></a>
<a href="https://reactjs.org/" target="_blank"><img src="assets/img/logo-react.png" alt="React" /></a>
<a href="https://stripe.com/" target="_blank"><img src="assets/img/logo-stripe.png" alt="Stripe" /></a>
<a href="https://dev.mysql.com/" target="_blank"><img src="assets/img/logo-mysql.png" alt="MySQL" /></a>
</div>
</section>
<section id="examples">
<h1>Examples</h1>
<p>The Prism source, highlighted with Prism (dont you just love how meta this is?):</p>
<pre data-src="prism.js"></pre>
<p>This pages CSS code, highlighted with Prism:</p>
<pre data-src="assets/style.css"></pre>
<p>This pages HTML, highlighted with Prism:</p>
<pre data-src="index.html"></pre>
<p>This pages logo (SVG), highlighted with Prism:</p>
<pre data-src="assets/logo.svg"></pre>
<p>If youre still not sold, you can <a href="examples.html">view more examples</a> or <a href="test.html">try it out for yourself</a>.</p>
</section>
<section id="features-full" class="language-markup">
<h1>Full list of features</h1>
<ul>
<li><strong>Only 2KB</strong> minified &amp; gzipped (core). Each language definition adds roughly 300-500 bytes.</li>
<li>Encourages good author practices. Other highlighters encourage or even force you to use elements that are semantically wrong,
like <code>&lt;pre></code> (on its own) or <code>&lt;script></code>.
Prism forces you to use the correct element for marking up code: <code>&lt;code></code>.
On its own for inline code, or inside a <code>&lt;pre></code> for blocks of code.
In addition, the language is defined through the way recommended in the HTML5 draft: through a <code>language-xxxx</code> class.</li>
<li>The <code>language-xxxx</code> class 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.</li>
<li>Supports <strong>parallelism with Web Workers</strong>, if available.
Disabled by default (<a href="faq.html#why-is-asynchronous-highlighting-disabled-by-default">why?</a>).</li>
<li>Very easy to extend without modifying the code, due to Prisms <a href="#plugins">plugin architecture</a>.
Multiple hooks are scattered throughout the source.</li>
<li>Very easy to <a href="extending.html#language-definitions">define new languages</a>.
The only thing you need is a good understanding of regular expressions.</li>
<li>All styling is done through CSS, with <a href="faq.html#how-do-i-know-which-tokens-i-can-style-for">sensible class names</a> rather than ugly, namespaced, abbreviated nonsense.</li>
<li>Wide browser support: Edge, IE11, Firefox, Chrome, Safari, <a href="faq.html#this-page-doesnt-work-in-opera">Opera</a>, most mobile browsers.</li>
<li>Highlights embedded languages (e.g. CSS inside HTML, JavaScript inside HTML).</li>
<li>Highlights inline code as well, not just code blocks.</li>
<li>It doesnt 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 dont like it and leave no traces behind.</li>
<li>Highlight specific lines and/or line ranges (requires <a href="plugins/line-highlight/">plugin</a>).</li>
<li>Show invisible characters like tabs, line breaks etc (requires <a href="plugins/show-invisibles/">plugin</a>).</li>
<li>Autolink URLs and emails, use Markdown links in comments (requires <a href="plugins/autolinker/">plugin</a>).</li>
</ul>
</section>
<section id="limitations">
<h1>Limitations</h1>
<ul>
<li>Any pre-existing HTML in the code will be stripped off. <a href="faq.html#if-pre-existing-html-is-stripped-off-how-can-i-highlight">There are ways around it though</a>.</li>
<li>Regex-based so it *will* fail on certain edge cases, which are documented in the <a href="known-failures.html">known failures page</a>.</li>
<li>Some of our themes have problems with certain layouts. Known cases are documented <a href="known-failures.html#themes">here</a>.</li>
<li>No IE 6-10 support. If someone can read code, they are probably in the 95% of the population with a modern browser.</li>
</ul>
</section>
<section id="basic-usage" class="language-markup">
<h1>Basic usage</h1>
<p>You will need to include the <code>prism.css</code> and <code>prism.js</code> files you <a href="download.html">downloaded</a> in your page. Example:
<pre><code>&lt;!DOCTYPE html>
&lt;html>
&lt;head>
...</code>
<code class="highlight">&lt;link href="themes/prism.css" rel="stylesheet" /></code>
<code>&lt;/head>
&lt;body>
...</code>
<code class="highlight">&lt;script src="prism.js">&lt;/script></code>
<code>&lt;/body>
&lt;/html></code></pre>
<p>Prism does its best to encourage good authoring practices. Therefore, it only works with <code>&lt;code></code> elements, since marking up code without a <code>&lt;code></code> element is semantically invalid.
<a href="https://www.w3.org/TR/html52/textlevel-semantics.html#the-code-element">According to the HTML5 spec</a>, the recommended way to define a code language is a <code>language-xxxx</code> class, which is what Prism uses.
Alternatively, Prism also supports a shorter version: <code>lang-xxxx</code>.</p>
<p>The <a href="https://www.w3.org/TR/html5/grouping-content.html#the-pre-element">recommended way to mark up a code block</a>
(both for semantics and for Prism) is a <code>&lt;pre></code> element with a <code>&lt;code></code> element inside, like so:</p>
<pre><code>&lt;pre>&lt;code class="language-css">p { color: red }&lt;/code>&lt;/pre></code></pre>
<p>If you use that pattern, the <code>&lt;pre></code> will automatically get the <code>language-xxxx</code> class (if it doesnt already have it) and will be styled as a code block.</p>
<p>Inline code snippets are done like this:</p>
<pre><code>&lt;code class="language-css">p { color: red }&lt;/code></code></pre>
<p><strong>Note</strong>: You have to escape all <code>&lt;</code> and <code>&amp;</code> characters inside <code>&lt;code></code> elements (code blocks and inline snippets) with <code>&amp;lt;</code> and <code>&amp;amp;</code> respectively, or else the browser might interpret them as an HTML tag or <a href="https://developer.mozilla.org/en-US/docs/Glossary/Entity">entity</a>. If you have large portions of HTML code, you can use the <a href="plugins/unescaped-markup/">Unescaped Markup plugin</a> to work around this.</p>
<h3>Language inheritance</h3>
<p>To make things easier however, Prism assumes that the language class is inherited. Therefore, if multiple <code>&lt;code></code> elements have the same language, you can add the <code>language-xxxx</code> class on one of their common ancestors.
This way, you can also define a document-wide default language, by adding a <code>language-xxxx</code> class on the <code>&lt;body></code> or <code>&lt;html></code> element.</p>
<p>If you want to opt-out of highlighting a <code>&lt;code></code> element that inherits its language, you can add the <code>language-none</code> class to it. The <code>none</code> language can also be inherited to disable highlighting for the element with the class and all of its descendants.</p>
<p>If you want to opt-out of highlighting but still use plugins like <a href="plugins/show-invisibles/">Show Invisibles</a>, add use <code>language-plain</code> class instead.</p>
<h3>Manual highlighting</h3>
<p>If you want to prevent any elements from being automatically highlighted and instead use the <a href="extending.html#api">API</a>, you can set <a href="docs/Prism.html#.manual"><code class="language-javascript">Prism.manual</code></a> to <code class="language-javascript">true</code> before the <code>DOMContentLoaded</code> event is fired. By setting the <code>data-manual</code> attribute on the <code>&lt;script></code> element containing Prism core, this will be done automatically.
Example:</p>
<pre><code>&lt;script src="prism.js" data-manual>&lt;/script></code></pre>
<p>or</p>
<pre><code>&lt;script>
window.Prism = window.Prism || {};
window.Prism.manual = true;
&lt;/script>
&lt;script src="prism.js">&lt;/script></code></pre>
<h2 id="basic-usage-cdn">Usage with CDNs</h2>
<p>In combination with CDNs, we recommend using the <a href="plugins/autoloader">Autoloader plugin</a> which automatically loads languages when necessary.</p>
<p>The setup of the Autoloader, will look like the following. You can also add your own themes of course.</p>
<pre><code>&lt;!DOCTYPE html>
&lt;html>
&lt;head>
...</code>
<code class="highlight">&lt;link href="https://myCDN.com/prism@v1.x/themes/prism.css" rel="stylesheet" /></code>
<code>&lt;/head>
&lt;body>
...</code>
<code class="highlight" style="display: inline-block; outline-offset: .2em; margin-bottom: .2em;"> &lt;script src="https://myCDN.com/prism@v1.x/components/prism-core.min.js"&gt;&lt;/script&gt;
&lt;script src="https://myCDN.com/prism@v1.x/plugins/autoloader/prism-autoloader.min.js"&gt;&lt;/script&gt;</code>
<code>&lt;/body>
&lt;/html></code></pre>
<p>CDNs which provide PrismJS are e.g. <a href="https://cdnjs.com/libraries/prism">cdnjs</a>, <a href="https://www.jsdelivr.com/package/npm/prismjs">jsDelivr</a>, and <a href="https://unpkg.com/browse/prismjs@1/">UNPKG</a>.</p>
<h2 id="basic-usage-bundlers">Usage with Webpack, Browserify, & Other Bundlers</h2>
<p>If you want to use Prism with a bundler, install Prism with <code>npm</code>:</p>
<pre><code>$ npm install prismjs</code></pre>
<p>You can then <code class="language-js">import</code> into your bundle:</p>
<pre><code class="language-js">import Prism from 'prismjs';</code></pre>
<p>To make it easy to configure your Prism instance with only the languages and plugins you need, use the babel plugin,
<a href="https://github.com/mAAdhaTTah/babel-plugin-prismjs">babel-plugin-prismjs</a>. This will allow you to load
the minimum number of languages and plugins to satisfy your needs.
See that plugin's documentation for configuration details.</p>
<h2 id="basic-usage-node">Usage with Node</h2>
<p>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 <a href="https://www.ampproject.org/">AMP pages</a>.</p>
<p>Example:</p>
<pre><code class="language-js">const Prism = require('prismjs');
// The code snippet you want to highlight, as a string
const code = `var data = 1;`;
// Returns a highlighted HTML string
const html = Prism.highlight(code, Prism.languages.javascript, 'javascript');</code></pre>
<p>Requiring <code>prismjs</code> will load the default languages: <code>markup</code>, <code>css</code>,
<code>clike</code> and <code>javascript</code>. You can load more languages with the
<code class="language-javascript">loadLanguages()</code> utility, which will automatically handle any required dependencies.</p>
<p>Example:</p>
<pre><code class="language-js">const Prism = require('prismjs');
const loadLanguages = require('prismjs/components/');
loadLanguages(['haml']);
// The code snippet you want to highlight, as a string
const code = `= ['hi', 'there', 'reader!'].join " "`;
// Returns a highlighted HTML string
const html = Prism.highlight(code, Prism.languages.haml, 'haml');</code></pre>
<p><strong>Note</strong>: Do <em>not</em> use <code class="language-javascript">loadLanguages()</code> with Webpack or another bundler, as this will cause Webpack to include all languages and plugins. Use the babel plugin described above.</p>
<p><strong>Note</strong>: <code class="language-javascript">loadLanguages()</code> will ignore unknown languages and log warning messages to the console. You can prevent the warnings by setting <code class="language-javascript">loadLanguages.silent = true</code>.</p>
</section>
<section class="language-markup">
<h1>Supported languages</h1>
<p>This is the list of all <span id="languages-list-count"></span> languages currently supported by Prism, with
their corresponding alias, to use in place of <code>xxxx</code> in the <code>language-xxxx</code> (or <code>lang-xxxx</code>) class:</p>
<div id="languages-list"></div>
<p>Couldnt find the language you were looking for? <a href="https://github.com/PrismJS/prism/issues" target="_blank">Request it</a>!</p>
</section>
<section id="plugins">
<h1>Plugins</h1>
<p>Plugins are additional scripts (and CSS code) that extend Prisms functionality. Many of the following plugins are official, but are released as plugins to keep the Prism Core small for those who dont need the extra functionality.</p>
<ul class="plugin-list language-none"></ul>
<p>No assembly required to use them. Just select them in the <a href="download.html">download</a> page.</p>
<p>Its very easy to <a href="extending.html#writing-plugins">write your own Prism plugins</a>. Did you write a plugin for Prism that you want added to this list? <a href="https://github.com/LeaVerou/prism" target="_blank">Send a pull request</a>!</p>
</section>
<section id="languages">
<h1>Third-party language definitions</h1>
<ul>
<li><a href="https://github.com/SassDoc/prism-scss-sassdoc">SassDoc Sass/Scss comments</a></li>
<li><a href="https://github.com/Liquibase/prism-liquibase">Liquibase CLI Bash language extension</a></li>
</ul>
</section>
<section id="tutorials">
<h1>Third-party tutorials</h1>
<p>Several tutorials have been written by members of the community to help you integrate Prism into multiple different website types and configurations:</p>
<ul>
<li><a href="https://startblogging101.com/how-to-add-prism-js-syntax-highlighting-wordpress/">How to Add Prism.js Syntax Highlighting to Your WordPress Site</a></li>
<li><a href="https://websitebeaver.com/escape-html-inside-code-or-pre-tag-to-entities-to-display-raw-code-with-prismjs">Escape HTML Inside &lt;code&gt; or &lt;pre&gt; Tag to Entities to Display Raw Code with PrismJS</a></li>
<li><a href="http://wp.tutsplus.com/tutorials/plugins/adding-a-syntax-highlighter-shortcode-using-prism-js/">Adding a Syntax Highlighter Shortcode Using Prism.js | WPTuts+</a></li>
<li><a href="https://www.stramaxon.com/2012/07/prism-syntax-highlighter-for-blogger.html">Implement PrismJs Syntax Highlighting to your Blogger/BlogSpot</a></li>
<li><a href="https://schier.co/blog/2013/01/07/how-to-re-run-prismjs-on-ajax-content.html">How To Re-Run Prism.js On AJAX Content</a></li>
<li><a href="https://www.semisedlak.com/highlight-your-code-syntax-with-prismjs">Highlight your code syntax with Prism.js</a></li>
<li><a href="https://usetypo3.com/fs-code-snippet.html">A code snippet content element powered by Prism.js for TYPO3 CMS</a></li>
<li><a href="https://auralinna.blog/post/2017/code-syntax-highlighting-with-angular-and-prismjs">Code syntax highlighting with Angular and Prism.js</a></li>
<li><a href="https://mkaz.blog/wordpress/code-syntax-highlighting-in-gutenberg/">Code syntax highlighting in Gutenberg, WordPress block editor</a></li>
<li><a href="https://karlkaufmann.com/writing/technotes/code-highlighting-prism-drupal">Code Highlighting with Prism.js in Drupal</a></li>
<li><a href="https://pathof.dev/blog/code-highlighting-in-react-using-prismjs">Code highlighting in React using Prism.js</a></li>
<li><a href="https://www.akashmittal.com/react-native-prismjs-using-webview/">Using Prism.js in React Native</a></li>
</ul>
<p>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 doesnt work :)</p>
<p>Have you written a tutorial about Prism thats not already included here? Send a pull request!</p>
</section>
<section id="credits">
<h1>Credits</h1>
<ul>
<li>Special thanks to <a href="https://github.com/RunDevelopment">Michael Schmidt</a>, <a href="https://github.com/mAAdhaTTah">James DiGioia</a>, <a href="https://github.com/Golmote">Golmote</a> and <a href="https://github.com/apfelbox">Jannik Zschiesche</a> for their contributions and for being <strong>amazing maintainers</strong>. Prism would not have been able to keep up without their help.</li>
<li>To <a href="https://twitter.com/kizmarh">Roman Komarov</a> for his contributions, feedback and testing.</li>
<li>To <a href="https://twitter.com/zdfs">Zachary Forrest</a> for <a href="https://twitter.com/zdfs/statuses/217834980871639041">coming up with the name “Prism”</a></li>
<li>To <a href="https://stellarr.deviantart.com/">stellarr</a> for the <a href="https://stellarr.deviantart.com/art/Spectra-Wallpaper-Pack-97785901">spectrum background</a> used on this page</li>
<li>To <a href="https://twitter.com/thecodezombie">Jason Hobbs</a> for <a href="https://twitter.com/thecodezombie/status/217663703825399809">encouraging me</a> to release this script as standalone</li>
</ul>
</section>
<footer data-src="assets/templates/footer.html" data-type="text/html"></footer>
<script src="assets/vendor/utopia.js"></script>
<script src="prism.js"></script>
<script src="components.js"></script>
<script src="assets/code.js"></script>
<script>
(function() {
var languageItems = [];
var languages = components.languages;
var count = 0;
for (var id in languages) {
if (id == 'meta') {
continue;
}
count++;
var lang = languages[id];
var name = lang.title || lang;
var contents = [
name,
' - ',
{
tag: 'code',
contents: id
}
];
var alias = lang.alias;
if (typeof alias === 'string')
alias = [alias];
if (alias) {
for (var i = 0, l = alias.length; i < l; i++) {
contents.push(
', ',
{
tag: 'code',
contents: alias[i]
});
}
}
languageItems.push({
tag: 'li',
attributes: {
'data-id': id
},
contents: contents
});
}
$u.element.create('ul', {
contents: languageItems,
inside: '#languages-list'
});
$u.element.contents($('#languages-list-count'), count);
}());
</script>
</body>
</html>