PrismJS - Documentation

What is PrismJS?

PrismJS is a lightweight, extensible syntax highlighter, built with JavaScript. It’s designed to be easily integrated into any web project, requiring minimal setup and offering a wide range of supported languages. Unlike many other syntax highlighters, PrismJS works by parsing the code and applying highlighting directly in the browser, eliminating the need for server-side processing. This makes it fast, efficient, and suitable for both static and dynamic websites. PrismJS focuses on performance and ease of use, allowing developers to quickly and effectively add syntax highlighting to their projects.

Why use PrismJS?

PrismJS offers several compelling reasons for its adoption:

Setting up PrismJS

Getting started with PrismJS involves a few simple steps:

  1. Download: Download the PrismJS core files (prism.js and a CSS file for your chosen theme) from the official website or via a CDN (Content Delivery Network). Popular CDNs include jsDelivr and unpkg.

  2. Include in your HTML: Add links to the downloaded CSS file and the main PrismJS JavaScript file within the <head> section of your HTML document. For example:

<link rel="stylesheet" href="prism.css">
<script src="prism.js"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/prismjs@1.x.x/themes/prism.min.css">
<script src="https://cdn.jsdelivr.net/npm/prismjs@1.x.x/prism.min.js"></script>
  1. (Optional) Include Language Plugins: If you need support for languages beyond the core PrismJS functionality, download the necessary language plugins and include them in your HTML after the main prism.js file. For example, for JavaScript support:
<script src="prism-javascript.min.js"></script>

Basic Usage

Once PrismJS is included, highlighting code is extremely simple. Just wrap your code within a <pre> tag with a <code> tag inside, and add a language- class attribute to the <code> tag, specifying the language of your code. For instance, to highlight JavaScript code:

<pre><code class="language-javascript">
let message = "Hello, world!";
console.log(message);
</code></pre>

PrismJS will automatically detect the language and apply the appropriate syntax highlighting. Remember to include the relevant language plugin if the language isn’t included by default. If no language is specified, PrismJS will attempt auto-detection, but it might not be entirely accurate for all cases.

Core Concepts

Language Definitions

PrismJS’s core functionality relies on language definitions. These definitions are essentially grammars that describe the structure and syntax of a particular programming language or markup format. Each language definition is a JavaScript object that specifies the patterns and rules PrismJS uses to identify different elements within the code. These definitions are often organized into separate files (e.g., prism-javascript.js, prism-python.js), allowing for modularity and easy extension. A language definition typically comprises regular expressions that match different syntactic elements (keywords, identifiers, comments, strings, etc.), and assigns each a corresponding token type.

Tokens

Tokens represent the fundamental building blocks of the syntax highlighting process. When PrismJS parses code based on a language definition, it breaks down the code into a sequence of tokens. Each token is an object containing the matched text and its assigned type. The token type (e.g., keyword, string, comment, number, punctuation) determines the style applied to that specific piece of text in the highlighted output. The token types are defined within the language definition and influence the CSS styling applied to the code.

Grammar

The “grammar” of a language in PrismJS is defined by the regular expressions within the language definition. These regular expressions specify patterns to match different parts of the code. The order of these regular expressions is significant, as it dictates the precedence in which tokens are identified. A well-crafted grammar ensures that the code is parsed correctly and that tokens are assigned their appropriate types, leading to accurate and effective highlighting. The grammar essentially dictates how the code is segmented into meaningful units for styling. A complex language might require a more intricate grammar with multiple regular expressions to handle different syntactic constructs.

Highlighting Process

The PrismJS highlighting process can be broken down into these main steps:

  1. Parsing: The input code is parsed according to the specified language definition’s grammar. This involves applying the regular expressions in the order they are defined to identify tokens.

  2. Tokenization: The code is broken down into a sequence of tokens. Each token is assigned a type based on the matched regular expression.

  3. Styling: Based on the token type, a corresponding CSS class is applied to the token’s text. These CSS classes are defined in the theme’s CSS file and determine the visual appearance (color, font style, etc.) of each token type. This mapping of token types to CSS classes is inherent to the theme used, not directly part of the language definition.

  4. Rendering: The HTML output is generated by wrapping each token’s text with a <span> element having the appropriate CSS class, maintaining the original structure and order of the code. This results in the visually highlighted code.

  5. Output: Finally, the highlighted HTML is inserted into the DOM, replacing the original plain-text code element, resulting in the visually highlighted code displayed on the webpage. This process is entirely client-side within the user’s browser.

Supported Languages

List of Supported Languages

PrismJS boasts a comprehensive list of supported languages, constantly expanding thanks to its community-driven plugin system. A complete and up-to-date list is available on the official PrismJS website. This list typically includes a wide range of programming languages (e.g., JavaScript, Python, Java, C++, C#, Go, PHP, Ruby, Swift, Kotlin, TypeScript), markup languages (HTML, XML, Markdown), and various other formats. The availability of plugins for specific languages ensures broad compatibility and versatility. To use a language, you need to include the corresponding plugin file after the core PrismJS script in your HTML.

Adding Custom Languages

Extending PrismJS to support new languages is a core strength. This involves creating a language definition file that follows PrismJS’s grammar specification. The definition comprises regular expressions that match various syntactic elements (keywords, identifiers, comments, strings, etc.), assigning each a token type. The structure of the language definition typically resembles the existing language definitions provided in the PrismJS distribution. Once created, the custom language definition is included in your HTML, similar to the pre-built language plugins. Consult the PrismJS documentation and existing language definitions for guidance on creating a new language definition. It’s crucial to test the new grammar thoroughly to ensure accuracy.

Language Aliases

Language aliases provide alternative names for supported languages. This is useful when a language might have multiple names or commonly used abbreviations. Aliases enable you to use a shorter or more familiar name in your <code class="language-…"> tags without needing a separate plugin. For instance, language-js might be an alias for language-javascript. The use of aliases simplifies code and enhances readability while ensuring consistent syntax highlighting. The available aliases are generally documented on the official PrismJS website along with the language plugins. Check the documentation for your specific version of PrismJS to see which aliases are supported.

Themes and Styling

Built-in Themes

PrismJS offers several pre-built themes that provide different visual styles for the highlighted code. These themes are CSS files that define the appearance of various token types (keywords, comments, strings, etc.). The default theme often provides a clean and functional look, but several others offer variations in color schemes, font styles, and overall aesthetics. Users can choose a theme based on their preferences and website design. Each theme’s CSS file is included in the HTML <head> section via a <link> tag. The choice of theme is independent of the languages being highlighted.

Custom Themes

Creating a custom theme allows for complete control over the visual presentation of the highlighted code. This involves creating a new CSS file that defines styles for the various PrismJS token classes (e.g., token-keyword, token-comment, token-string). The process typically involves copying an existing theme as a starting point and modifying the styles as desired. Understanding the CSS classes used by PrismJS is essential for effective theme customization. The custom CSS file is then linked in the HTML document, overriding or augmenting the default theme.

Theme Customization

Customizing existing themes is often simpler than creating a completely new one. By modifying the CSS rules of a pre-built theme, one can adapt the colors, fonts, and other visual aspects to better match the overall design of a website. This typically involves adjusting color values, font sizes, and other CSS properties within the theme’s CSS file. Careful modification ensures that the code remains readable and aesthetically pleasing while maintaining consistency with the website’s visual style guide. Remember to ensure sufficient contrast for accessibility.

Inline Styles

While generally discouraged for maintainability, inline styles can be used in exceptional circumstances to override specific aspects of the theme’s styling. This involves adding style attributes directly to the HTML elements generated by PrismJS. However, this approach is generally not recommended as it makes maintaining consistency difficult and can lead to less manageable code. It is far preferable to modify the CSS directly within the theme’s CSS file or create a custom CSS file to achieve specific styling changes without resorting to inline styles. Inline styles should only be used for quick prototyping or debugging and should not be considered a best practice for styling in production.

Advanced Usage

Line Numbers

Adding line numbers to highlighted code enhances readability, particularly for longer code snippets. PrismJS offers a plugin for this functionality. After including the core PrismJS files, you need to include the prism-line-numbers.js plugin. This plugin automatically adds line numbers to the <pre> element containing the code. Configuration options might be available to customize the appearance and behavior of the line numbers (e.g., styling, starting number). Refer to the plugin’s documentation for detailed instructions and options.

Autolinking

The autolinking feature automatically converts URLs and email addresses within the highlighted code into clickable links. This enhances usability and makes it easier to interact with the code examples. Similar to line numbers, this often requires a plugin (e.g., prism-autolinker.js). Once included, the plugin automatically processes the highlighted code, converting eligible elements into hyperlinks. This can improve the user experience by allowing direct access to resources or contact information mentioned in the code.

Plugins

Plugins significantly extend PrismJS’s capabilities. They provide additional features beyond the core functionality, such as line numbers, autolinking, and support for additional languages. PrismJS’s plugin architecture allows for modularity and easy extension. Each plugin typically comes with its own instructions on how to include and configure it. Plugins are included in your HTML after the core PrismJS script. The official PrismJS website maintains a list of available plugins and their functionalities.

Custom Components

For highly specialized requirements, developers can create custom components to integrate directly into the PrismJS highlighting process. This might involve creating a custom token type or modifying the rendering process. This requires a deep understanding of PrismJS’s internal workings and involves extending or modifying the language definitions or the highlighting process itself. While powerful, this is generally only necessary for highly specialized cases and requires advanced JavaScript skills. Consult the advanced documentation for detailed guidance.

Show/Hide Code

The ability to show or hide code snippets enhances the user experience, particularly on pages with multiple code examples. This functionality can be implemented using JavaScript and CSS, toggling the visibility of the code blocks. This might involve using a button or link to trigger the show/hide action, coupled with CSS to control the display property of the code blocks. This typically doesn’t require a specific PrismJS plugin but rather custom JavaScript and CSS to manage the visibility of the code elements. Ensure that accessibility is considered when implementing this feature; for example, using ARIA attributes to communicate the state of the code visibility to assistive technologies.

Plugins

Available Plugins

PrismJS boasts a rich ecosystem of plugins that extend its core functionality. These plugins add features not included in the base library, providing added value and flexibility. The range of available plugins is constantly expanding, thanks to contributions from the community. Common plugins include those that add support for specific languages (which are essentially language definition plugins), line numbers, autolinking, automatic insertion of copy buttons, file highlighting, and more. A comprehensive list of available plugins, along with descriptions and installation instructions, is typically found on the official PrismJS website or in the project’s repository. The availability and versions of plugins may vary depending on the version of PrismJS you are using.

Plugin Development

Developing a PrismJS plugin involves creating a JavaScript file that extends PrismJS’s functionality. This typically involves adding new token types, modifying the parsing process, or creating entirely new features. The plugin should adhere to the PrismJS plugin API and documentation to ensure compatibility. The plugin’s code should be well-documented and clearly explain its purpose, functionality, and usage. Testing the plugin thoroughly is essential to ensure that it works correctly and doesn’t interfere with other plugins or the core functionality of PrismJS. Sharing plugins with the community via the official PrismJS channels is encouraged to contribute to the ecosystem and allow others to benefit from your work. It’s essential to follow the style guide and best practices to ensure consistency and ease of integration.

Plugin Usage

Using a PrismJS plugin is generally straightforward. After including the core PrismJS files (prism.js and a theme CSS file) in your HTML, you must include the plugin’s JavaScript file after the core prism.js file. This ensures that the plugin has access to the core PrismJS functionality. Some plugins may require additional configuration options or initialization steps; this information is typically found in the plugin’s documentation. The plugin’s functionality should then be automatically applied to the code blocks on the page that match the plugin’s criteria. For example, a line-number plugin will add line numbers to every <pre> block containing code. If a plugin has specific requirements or configuration options, consult its documentation for detailed instructions. Incorrect ordering of included files or missing configuration can lead to the plugin not functioning correctly.

API Reference

Prism Object

The core of PrismJS’s functionality is exposed through the global Prism object. This object provides access to various methods and properties that allow for programmatic control over the highlighting process. It is the central point of interaction for developers who want to manipulate the syntax highlighting beyond the basic usage. The Prism object contains properties related to language definitions and methods for highlighting code, managing plugins and other advanced features. Accessing and understanding the Prism object is critical for developers who need more fine-grained control over the syntax highlighting process.

highlightAll() Method

The highlightAll() method is a convenient way to highlight all code blocks on a page that have the appropriate class attributes. It automatically finds all elements matching the pre code structure and applies the syntax highlighting according to the specified language classes. This method is ideal for situations where you want to highlight multiple code blocks on a single page without explicitly calling the highlightElement() method for each one. It simplifies the process of adding syntax highlighting across an entire page. It automatically detects and processes code blocks that match the conventions established by PrismJS. This method is a good starting point for most projects using PrismJS.

highlightElement() Method

The highlightElement() method allows for more granular control over the highlighting process. It takes a single DOM element as an argument and highlights the code within that element. This method provides more flexibility compared to highlightAll(), as it allows you to explicitly select and highlight specific code blocks, giving you more precise control. This is particularly useful when dealing with dynamically added or manipulated code blocks. Using highlightElement(), you can selectively highlight code elements as needed without affecting other blocks, thereby controlling the highlighting process more directly.

Other API Methods

Beyond highlightAll() and highlightElement(), the Prism object offers several other methods and properties for advanced use cases. These might include:

The specific methods and their functionalities are detailed in the complete API documentation provided with the PrismJS library. The availability and use of these methods will depend on the version of PrismJS used, and detailed documentation should always be consulted to ensure proper usage.

Troubleshooting

Common Issues

Several common issues can arise when using PrismJS. These often stem from incorrect setup, plugin conflicts, or problems with the code itself. Some of the most frequently encountered problems include:

Debugging Tips

Effective debugging techniques are crucial for resolving issues. Here are some helpful tips:

Error Handling

PrismJS might encounter errors during the highlighting process, such as invalid language specifications or problems with the code itself. While PrismJS attempts to handle many errors gracefully, it’s helpful to understand how to handle potential errors gracefully in your own code:

By understanding common issues, implementing effective debugging techniques and incorporating appropriate error handling, you can efficiently resolve problems and ensure the smooth functioning of PrismJS in your projects.

Contributing to PrismJS

Reporting Bugs

If you encounter a bug in PrismJS, reporting it effectively is crucial to help the maintainers improve the library. When reporting a bug, please provide the following information:

The more information you provide, the easier it will be to diagnose and fix the problem. Use the issue tracker of the official PrismJS repository to report bugs; follow any instructions provided on how to submit a bug report. Be respectful and concise in your communication.

Submitting Pull Requests

Submitting a pull request (PR) is a way to contribute code changes directly to the PrismJS project. Before submitting a PR, please ensure you have followed these guidelines:

Following these steps will streamline the review process and increase the likelihood of your PR being accepted.

Coding Style Guide

PrismJS follows a specific coding style to maintain consistency across the project. Adhering to the style guide is crucial for ensuring that your contributions integrate seamlessly with the existing codebase. Key aspects of the style guide typically include:

Refer to the official PrismJS repository or documentation for the most up-to-date version of the coding style guide. Consistent adherence to the style guide is important for readability and maintainability of the project. Tools like linters can help automate checking for style guide compliance.