React code editor and syntax highlighter using PrismJS

In this react article, you will learn how to create a code editor and highlight the syntax using PrismJS. Prism.js is a lightweight, extensible syntax highlighter, built with modern web standards in mind.

Demo Application

Output - React code editor and syntax highlighter using PrismJS - Clue Mediator
Output – React code editor and syntax highlighter using PrismJS – Clue Mediator

Steps to implement code editor and highlight the syntax

  1. Create a React application
  2. Install dependencies
  3. Add code editor
  4. Highlight the syntax using Prism.js
  5. Output

File Structure

  • react-code-editor
    • node_modules
    • public
      • index.html
    • src
      • App.js
      • index.css
      • index.js
      • editor.css
      • prism-vsc-dark-plus.css
    • package-lock.json
    • package.json
    • README.md

1. Create a React application

Set up a React application using create-react-app. Run the following command to create a React app.

2. Install dependencies

Here we will use the react-simple-code-editor and prismjs NPM packages to create a demo. Run the following command to install the packages.

3. Add code editor

Let’s use the following sample code.

Next, we have to import the react-simple-code-editor to render the editor in the React component.

4. Highlight the syntax using Prism.js

After rendering the editor, we will highlight the syntax using Prism.js. Here, we are importing the core component and javascript component to highlight the syntax in JavaScript format.

Import the following code in your component. Here, we have wrapped the Editor with the title bar to design the editor.

Let’s import the following CSS.

prism-vsc-dark-plus.css
editor.css

5. Output

Let’s put all code together and see how it looks.

App.js

Run the application and check the output in the browser.

I hope you find this article helpful.
Thank you for reading. Happy Coding..!! 🙂

Demo & Source Code

GitHub Repository StackBlitz Project
If you found value in this article,
you can support us by buying me a coffee! ☕

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *