Add Code Syntax Highlighter PrismJS to Nuxtjs


This article we will see how to add PrismJs to Nuxtjs to highlight our code snippets. We will add the prismJs to our nuxt content static site.

PrismJs is a light weight syntax highlighter which is built with modern web standard and have many programming language syntax supports.

PrismJs is very small in size, its 2 KB minified and g-zipped and the theme is less then 1 KB.

In this article, we will add prismJS to NuxtJS application with @nuxt/content module.

In Nuxt Content , PrismJS module handles all code highlighting in markdown content. It helps us to add the desired PrismJS theme easily in our Nuxt.js config file. You can easily change the theme if you don't like the default one.

Add PrismJs to Nuxt Content

To add Prismjs syntax highlighter module to nuxt content we need to install the package.

npm install prism-themes
//FOR YARN//
yarn add prism-themes

Configure PrismJs in nuxt

The configuration is very simple just add the following code in the nuxt.config.js file.

content: {
  markdown: {
    prism: {
      theme: 'prism-themes/themes/prism-material-oceanic.css'
    }
  }
}

NOTE : To disable the them, just set prism to false.

content: {
  markdown: {
    prism: {
      theme: false
    }
  }
}

How to change prismjs theme in nuxt?

To change the theme , you just have to change the theme property URL in the nuxt.config.js file. You can browse through the different themes here. Once you find it desire theme, click the link and copy the last css part from the url and paste it to your config file.

configure prismjs theme in nuxtjs

Now add it the theme property

content: {
  markdown: {
    prism: {
      theme: 'prism-themes/themes/prism-atom-dark.css'
    }
  }
}

That's how you can easily add prismjs in your nuxt content and highlight your code syntax in blogs.

Related Topics:

Remove Trailing Slash from URL in Nuxt + Netlify

How To Run Netlify CMS Admin Locally?


💾 Recommended Articles