Convert Figma logo to code with AI

PrismJS logoprism

Lightweight, robust, elegant syntax highlighting.

12,288
1,293
12,288
413

Top Related Projects

JavaScript syntax highlighter with language auto-detection and zero dependencies.

In-browser code editor (version 5, legacy)

9,701

A beautiful yet powerful syntax highlighter

3,330

A pure Ruby code highlighter that is compatible with Pygments

SyntaxHighlighter is a fully functional self-contained code syntax highlighter developed in JavaScript.

Quick Overview

Prism is a lightweight, extensible syntax highlighter for the web. It's designed to be fast, easy to use, and capable of highlighting a wide range of programming languages. Prism is used by many popular websites and frameworks to provide attractive and accurate code highlighting.

Pros

  • Lightweight and fast, with minimal impact on page load times
  • Highly extensible with a large number of plugins and language definitions
  • Easy to customize with CSS, allowing for seamless integration with various design themes
  • Active community and regular updates

Cons

  • Limited built-in language support; many languages require additional plugins
  • Some advanced features (like line numbers) require plugins, which can increase complexity
  • Documentation can be overwhelming for beginners due to the large number of options and plugins
  • May require additional setup for certain frameworks or build systems

Code Examples

  1. Basic usage:
<pre><code class="language-javascript">
function greet(name) {
  console.log(`Hello, ${name}!`);
}
greet('World');
</code></pre>
  1. Highlighting specific lines:
<pre><code class="language-python line-numbers" data-line="2,4">
def factorial(n):
    if n == 0:
        return 1
    return n * factorial(n - 1)
</code></pre>
  1. Using the JavaScript API:
Prism.highlightAll();

// Or highlight a specific element
Prism.highlightElement(document.querySelector('code.language-css'));

Getting Started

  1. Include Prism CSS and JS in your HTML:
<link href="https://cdnjs.cloudflare.com/ajax/libs/prism/1.24.1/themes/prism.min.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/prism/1.24.1/prism.min.js"></script>
  1. Add language-specific class to your code elements:
<pre><code class="language-css">
p { color: red; }
</code></pre>
  1. If not using the async or defer attributes on the script tag, Prism will automatically highlight all code elements on the page. Otherwise, call Prism.highlightAll(); after the DOM is ready.

Competitor Comparisons

JavaScript syntax highlighter with language auto-detection and zero dependencies.

Pros of highlight.js

  • Larger language support out-of-the-box (189+ languages)
  • Automatic language detection
  • Simpler setup and usage for basic implementations

Cons of highlight.js

  • Larger file size, potentially impacting page load times
  • Less extensible and customizable compared to Prism
  • Fewer built-in plugins and add-ons

Code Comparison

highlight.js:

<pre><code class="language-javascript">
function greet(name) {
  console.log(`Hello, ${name}!`);
}
</code></pre>
<script>hljs.highlightAll();</script>

Prism:

<pre><code class="language-javascript">
function greet(name) {
  console.log(`Hello, ${name}!`);
}
</code></pre>
<script>Prism.highlightAll();</script>

Both libraries use similar markup, but Prism offers more granular control over highlighting and provides a wider range of customization options through its plugin system. highlight.js is often preferred for its simplicity and automatic language detection, while Prism is favored for its extensibility and lighter weight. The choice between the two depends on specific project requirements and preferences.

In-browser code editor (version 5, legacy)

Pros of CodeMirror 5

  • Full-featured code editor with advanced functionality like autocomplete, search/replace, and line wrapping
  • Highly customizable with a wide range of options and add-ons
  • Supports real-time collaborative editing

Cons of CodeMirror 5

  • Larger file size and more complex setup compared to Prism
  • Steeper learning curve for basic syntax highlighting use cases
  • May be overkill for simple code snippet display

Code Comparison

Prism:

Prism.highlightAll();

CodeMirror 5:

var editor = CodeMirror.fromTextArea(document.getElementById("code"), {
  lineNumbers: true,
  mode: "javascript"
});

Summary

Prism is lightweight and focused on syntax highlighting, making it ideal for displaying code snippets on websites. CodeMirror 5 is a full-featured code editor that offers more advanced functionality but comes with increased complexity and file size. Choose Prism for simple highlighting needs and CodeMirror 5 for interactive editing experiences.

9,701

A beautiful yet powerful syntax highlighter

Pros of Shiki

  • Produces pixel-perfect syntax highlighting using TextMate grammars
  • Supports a wide range of themes out of the box
  • Generates HTML with inline styles, making it easier to use in various environments

Cons of Shiki

  • Larger bundle size due to including VSCode's syntax highlighting engine
  • Slower performance compared to Prism, especially for large code blocks
  • Less flexibility in customizing language definitions

Code Comparison

Prism:

Prism.highlight(code, Prism.languages.javascript, 'javascript');

Shiki:

const highlighter = await shiki.getHighlighter({ theme: 'nord' });
const html = highlighter.codeToHtml(code, { lang: 'js' });

Both Prism and Shiki are popular syntax highlighting libraries for web applications. Prism is lightweight and highly customizable, making it a popular choice for many developers. It uses regular expressions for tokenization, which can be faster but less accurate than Shiki's approach.

Shiki, on the other hand, uses VSCode's TextMate grammars for highlighting, resulting in more accurate and consistent output across different themes and languages. However, this comes at the cost of a larger bundle size and potentially slower performance.

Ultimately, the choice between Prism and Shiki depends on the specific needs of your project, balancing factors such as accuracy, performance, and customization requirements.

3,330

A pure Ruby code highlighter that is compatible with Pygments

Pros of Rouge

  • Written in Ruby, making it easier to integrate with Ruby-based projects
  • Supports a wider range of languages (over 100)
  • Designed for server-side syntax highlighting, which can be more efficient for static site generation

Cons of Rouge

  • Slower performance compared to Prism, especially for client-side highlighting
  • Less extensive theming options and customization features
  • Smaller community and fewer plugins/extensions available

Code Comparison

Rouge (Ruby):

require 'rouge'
formatter = Rouge::Formatters::HTML.new
lexer = Rouge::Lexers::Ruby.new
formatter.format(lexer.lex("puts 'Hello, World!'"))

Prism (JavaScript):

const Prism = require('prismjs');
const code = "console.log('Hello, World!');";
const html = Prism.highlight(code, Prism.languages.javascript, 'javascript');

Both Rouge and Prism are popular syntax highlighting libraries, but they cater to different use cases. Rouge is better suited for Ruby-based projects and server-side highlighting, while Prism excels in client-side highlighting and offers more extensive customization options. The choice between the two depends on the specific requirements of your project and the ecosystem you're working in.

SyntaxHighlighter is a fully functional self-contained code syntax highlighter developed in JavaScript.

Pros of SyntaxHighlighter

  • More extensive language support out of the box
  • Better compatibility with older browsers
  • Easier to customize themes and styles

Cons of SyntaxHighlighter

  • Larger file size and slower performance
  • Less active development and community support
  • More complex setup and configuration

Code Comparison

SyntaxHighlighter:

<pre class="brush: js">
function helloWorld() {
  console.log("Hello, World!");
}
</pre>

Prism:

<pre><code class="language-javascript">
function helloWorld() {
  console.log("Hello, World!");
}
</code></pre>

Both SyntaxHighlighter and Prism are popular syntax highlighting libraries for web applications. SyntaxHighlighter offers more extensive language support and better compatibility with older browsers, making it suitable for projects with legacy requirements. It also provides easier customization options for themes and styles.

However, SyntaxHighlighter has a larger file size and slower performance compared to Prism. It also has less active development and community support, which may impact long-term maintenance and updates. Additionally, SyntaxHighlighter's setup and configuration process is more complex than Prism's.

Prism, on the other hand, is lightweight, performant, and has a more active community. It offers a simpler setup process and is more suitable for modern web applications. The code comparison shows the slight differences in markup between the two libraries, with Prism using a more straightforward approach.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

Prism

Build Status npm

Prism is a lightweight, robust, and elegant syntax highlighting library. It's a spin-off project from Dabblet.

You can learn more on prismjs.com.

Why another syntax highlighter?

More themes for Prism!

Contribute to Prism!

Important Notice

We are currently working on Prism v2 and will only accept security-relevant PRs for the time being.

Once work on Prism v2 is sufficiently advanced, we will accept PRs again. This will be announced on our Discussion page and mentioned in the roadmap discussion.

Prism v1 contributing notes

Prism depends on community contributions to expand and cover a wider array of use cases. If you like it, consider giving back by sending a pull request. Here are a few tips:

  • Read the documentation. Prism was designed to be extensible.
  • Do not edit prism.js, it’s just the version of Prism used by the Prism website and is built automatically. Limit your changes to the unminified files in the components/ folder. prism.js and all minified files are generated by our build system (see below).
  • Use npm ci to install Prism's dependencies. Do not use npm install because it will cause non-deterministic builds.
  • The build system uses gulp to minify the files and build prism.js. With all of Prism's dependencies installed, you just need to run the command npm run build.
  • Please follow the code conventions used in the files already. For example, I use tabs for indentation and spaces for alignment. Opening braces are on the same line, closing braces on their own line regardless of construct. There is a space before the opening brace. etc etc.
  • Please try to err towards more smaller PRs rather than a few huge PRs. If a PR includes changes that I want to merge and also changes that I don't, handling it becomes difficult.
  • My time is very limited these days, so it might take a long time to review bigger PRs (small ones are usually merged very quickly), especially those modifying the Prism Core. This doesn't mean your PR is rejected.
  • If you contribute a new language definition, you will be responsible for handling bug reports about that language definition.
  • If you add a new language definition or plugin, you need to add it to components.json as well and rebuild Prism by running npm run build, so that it becomes available to the download build page. For new languages, please also add a few tests and an example in the examples/ folder.
  • Go to prism-themes if you want to add a new theme.

Thank you so much for contributing!!

Software requirements

Prism will run on almost any browser and Node.js version but you need the following software to contribute:

  • Node.js >= 10.x
  • npm >= 6.x

Translations

NPM DownloadsLast 30 Days