Django Portfolio Journal

Integrating Prism.js to Enhance Code Snippets


Integrating Prism.js to Enhance Code Snippets

When displaying code snippets on a website, proper syntax highlighting significantly improves readability and user experience. One of the best tools for this is Prism.js, a lightweight and customizable syntax highlighter. In this article, we'll explore how to integrate Prism.js into your website to make your code snippets more visually appealing.

Why Use Prism.js?

Prism.js offers several benefits:

    • Lightweight: It is designed to be fast and efficient.

    • Customizable: Various themes and plugins allow for a personalized experience.

    • Supports Multiple Languages: Easily highlight code for different programming languages.

    • Extensible: Plugins provide additional functionality like line numbers and automatic copy buttons.

Adding Prism.js to Your Website

To integrate Prism.js into your website, follow these steps:

1. Include the Required CSS and JS Files

In your base HTML template, include the necessary Prism.js CSS and JavaScript files by adding the following lines inside the <head> section:

<!-- core/templates/base.html -->

<!-- Prism.js CSS -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/prism/1.29.0/themes/prism-tomorrow.min.css">

<!-- Prism.js JS -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/prism/1.29.0/prism.min.js"></script>

<!-- Load Additional Languages (Optional) -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/prism/1.29.0/components/prism-python.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/prism/1.29.0/components/prism-bash.min.js"></script>

2. Format Code Blocks

To enable syntax highlighting, wrap your code inside <pre> and <code> tags, specifying the appropriate language class. For example:

<pre><code class="language-python">
def hello_world():
    print("Hello, World!")
</code></pre>

3. Choosing a Theme

Prism.js provides various themes that you can easily integrate. The example above uses the Tomorrow theme, but you can replace it with other themes like:

    • prism.css (default theme)

    • prism-okaidia.css (dark theme)

    • prism-twilight.css (light theme)

Check out the available themes on Prism.js’s official website.


By integrating Prism.js, you can improve the presentation of code snippets, making them more readable and visually appealing. Whether you're running a blog, documentation site, or an online learning platform, Prism.js is a powerful tool to enhance the developer experience.

Try different themes and plugins to customize it according to your needs, and enjoy beautifully formatted code snippets on your website!


Designed by BootstrapMade and modified by DoriDoro