Utilizing Prism.js on the client-side using npm package management

I am experimenting with utilizing the Prism.js syntax highlighter on the client side as a dependency through npm, rather than loading it via <script src="..."> tags. Below is the reference to Prism in my package.json file:

{
    "dependencies": {
        "prismjs": "^1.5.1"
    }
}

This is how I am attempting to integrate it into my code:

import Prism from 'prismjs'
Prism.highlightAll();

The outcome of this implementation is as follows:

  • Tokenizing functions properly for basic languages like HTML and JavaScript.
  • However, tokenizing fails for certain specific languages such as Lua and Handlebars.
  • Additionally, syntax coloring does not apply across all languages (suggesting that the CSS file may not be loading).

As a result, I have some questions:

  • Are there separate language-specific packages available (e.g., prismjs-handlebars)?
  • Can theme-specific packages be used (like prism-okaidia) to import the necessary CSS?

--

TL;DR

How can I implement Prism.js on the client side using npm instead of script tags?

Answer №1

After some searching, I finally discovered the solution.

1. Include style-loader and css-loader in package.json

{
    "dependencies": {
        "style-loader": "^0.13.1",
        "css-loader": "^0.23.0",
        "prismjs": "^1.5.1"
    }
}

2. Configure loading of css files in webpack.config.js

module: {
    loaders: [
        {
            test: /\.css/,
            loader: 'style-loader!css-loader'
        }
    ]
}

3. Import the necessary files into the application

import Prism from 'prismjs'
import 'prismjs/themes/prism-okaidia.css'
import 'prismjs/components/prism-handlebars.min.js'
import 'prismjs/components/prism-lua.min.js'

Prism.highlightAll();

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the best way to prevent the onClick event from triggering during the page rendering process?

I am currently working with React, Gatsby, and Material UI Buttons. I'm facing an issue where the most recently pressed button is getting disabled along with all other buttons when running my code. Despite already implementing bindings, as suggested b ...

Transforming httpClient responses into structured model objects in Angular 6

I am seeking guidance on the usage of Angular 5 httpClient. This particular model class contains a method called foo() that I wish to retrieve from the server export class MyClass implements Deserializable{ id: number; title: string; deserialize(i ...

Serve the mobile version to mobile visitors and the desktop version to all other visitors

Can someone please explain to me how I can display the Mobile Version of a website when visiting on my phone, and the Desktop Version when viewing on anything larger than 500px? I have separately created both a Mobile Website and a Desktop Website. Is it ...

Unable to run the Vexflow easyscore tutorial in React due to an error

Currently, I am trying to incorporate a stave into my React project and have been following the instructions outlined in the Vexflow official tutorial on Using EasyScore Displayed below is the code snippet from my React application. import React, { useRef ...

Sending form data using javascript without refreshing the page

I have a wall/social system similar to Facebook where users can post statuses. I want to add the functionality for users to like, dislike, and comment on a status without the page reloading. How can I achieve this with the form below? if(empty($_GET[&apos ...

Is it possible to convert an NPM project into a JavaScript file that is compatible with

Can a modestly sized NPM package be transformed into a JavaScript file for direct reference in HTML using a <script> tag? The NPM package in question is straightforward and serves as an API wrapper that I wish to implement without depending on Node. ...

Creating interactive comments in Vue 3 using dynamic rendering

Is there a way to properly display a dynamic comment in Vue 3? I attempted using v-html, but it's not working as expected in my scenario. Here is the example: https://i.sstatic.net/ddX39.png <template> <!-- Method 1: not displaying correctl ...

Tips for handling users generated through AWS Amplify Authentication

Currently, I am working on setting up an authentication page with Vue.js using AWS Amplify. The tutorial I am following for this can be found at the following link: https://dev.to/dabit3/how-to-build-production-ready-vue-authentication-23mk. As part of thi ...

During the next build process, there seems to be an issue with getStaticPaths and getStatic

I am encountering a problem with my Next.js Application that utilizes a [slug] for Static Site Generation. Everything works perfectly fine on my localhost, but when I try to deploy it, I encounter the following error: “Unhandled error during request: Ty ...

Update NPM automatically in Bash/GitBash only when a newer version is detected

Is there a way to automatically check for the availability of a new stable version of NPM online without manually opening a browser? If a newer version is found and it's greater than what I currently have installed globally, then I want to run the com ...

There seems to be an issue with the jQuery plugin not functioning properly with the latest version of

Just stumbled upon a tutorial for creating an image cross fade transition using jquery. The demo on the tutorial page seems to work flawlessly with jquery version 1.2.3. However, when I try to implement the code on my own website with jquery version 10.1.0 ...

Fixing the slide bar in React using styled components

In the early stages of creating a slider where cards (divs) can be moved left and right with a click, I encountered an issue. The onClick handler is functioning properly. However, upon running the project, I noticed that the cards start 230px away from the ...

What is the method for utilizing the value component as the key for the second component?

In my JSON data below: [{"id":19,"text":"A-Z CLI 19/03/2015"},{"id":36,"text":"Wavetel Retail1"},{"id":37,"text":"Wavetel A2Z Platinum"},{"id":38,"text":"Wavetel A2Z Gold"},{"id":40,"text":"mysql test2"},{"id":63,"text":"inbound test"},{"id":137,"text": ...

Using AngularJS to dynamically populate a dropdown menu from a JSON object

I am a beginner to Angular and currently facing my first significant challenge. The JSON object below is the address data retrieved from a third-party API and added to $scope.AddressData in a controller: $scope.AddressData = [{ "Address1":"South Row", ...

Guide on how to include a parent key in JSON within a React application

In my current web application setup, I am using Rails as the backend and React as the frontend. The data entered by users in HTML forms is transmitted between the applications in JSON format. However, there seems to be an issue with the format of the JSON ...

What is the best location in my redux store to store my socket connection?

As a beginner in the world of Redux, I've been using slices and redux-thunks to manage my redux store. However, I've come to realize that storing my socket connection in the state is not ideal. This connection is critical across multiple componen ...

Adjust website content depending on user's authentication status

My goal is to display a logout button when the user is logged in and a login button if they are not. I am using JSON tokens to determine if a user is logged in or not, by checking if the token is null. However, this approach does not seem to be working. Ca ...

Anticipating the arrival of data from an unspecified child component prior to executing the effect

Suppose there is a parent component: function Parent({ children }) { useEffet(() => { // Perform action only after all children have returned their values via props (or context) }) return <div>{ children }</div> } How do I ensure ...

The controller function is not triggered if the user does not have administrator privileges

I have a code snippet in which my controller function is not being triggered for users who do not have the role of "Admin". Can someone help me identify where I went wrong? Just to clarify, the controller function works fine for users with the role of "Adm ...

The potential issue of undefined objects in TypeScript when utilizing computed properties in Vue3

https://i.sstatic.net/I5ZVO.png I've attempted adding a ? after each word and also experimented with the following code: const totalNameLenght = computed(() => { if (userFirstnameLenght.value && userLastnameLenght.value){ ret ...