npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

svelte-prismjs

v1.0.2

Published

This package is used to help people include prismjs in their svelte, sapper and routify projects.

Downloads

918

Readme

SvelteJS Prism

This package is a wrapper for PrismJS. It works with line numbers and whitespace clean up out of the box. You can enable other plugins and languages as well. It was inspired by svelte-prism, another Svelte Prism package.

Live Svelte Demo

The repo has the Svelte code to run the demo.

Sapper Example

Routify Example

Features

  • Supports PrismJS plugins and data attributes being passed to the pre element.
  • Allows for client side loading in Sapper and Routify.
  • Supports code being changed dynamically in the Prism html element.
  • Examples and setup instructions.

Svelte Setup Instructions

  1. Run npm install --save-dev svelte-prismjs

  2. Load the css via CDN (if loading via CDN skip ahead to Option B) or...

  3. npm install rollup-plugin-css-only

Rollup Config

  1. Import the css into your main.js file.
import "prismjs/plugins/line-numbers/prism-line-numbers.css";
import "prismjs/plugins/command-line/prism-command-line.css";
import "prismjs/plugins/line-highlight/prism-line-highlight.css";

import "prismjs/themes/prism.css";
import "prismjs/themes/prism-coy.css";

Option B - CDN links to copy to your index.html

Be sure to double check link integrity at PRISM CDN

<!-- base theme -->
<link
  rel="stylesheet"
  href="https://cdnjs.cloudflare.com/ajax/libs/prism/1.23.0/themes/prism.min.css"
  integrity="sha512-tN7Ec6zAFaVSG3TpNAKtk4DOHNpSwKHxxrsiw4GHKESGPs5njn/0sMCUMl2svV4wo4BK/rCP7juYz+zx+l6oeQ=="
  crossorigin="anonymous"
/>
<!-- coy theme -->
<link
  rel="stylesheet"
  href="https://cdnjs.cloudflare.com/ajax/libs/prism/1.23.0/themes/prism-coy.min.css"
  integrity="sha512-CKzEMG9cS0+lcH4wtn/UnxnmxkaTFrviChikDEk1MAWICCSN59sDWIF0Q5oDgdG9lxVrvbENSV1FtjLiBnMx7Q=="
  crossorigin="anonymous"
/>
<!-- Number lines  -->
<link
  rel="stylesheet"
  href="https://cdnjs.cloudflare.com/ajax/libs/prism/1.23.0/plugins/line-numbers/prism-line-numbers.min.css"
  integrity="sha512-cbQXwDFK7lj2Fqfkuxbo5iD1dSbLlJGXGpfTDqbggqjHJeyzx88I3rfwjS38WJag/ihH7lzuGlGHpDBymLirZQ=="
  crossorigin="anonymous"
/>

Sapper Instructions

Sapper Example

  1. Run npm install svelte-prismjs

  2. You can either include the cdn links in your template.html file or you can install the rollup-css-only-plugin. It is basically what step 2 is for Svelte minus different directories. For Sapper be sure you put the file in the static folder.

  3. Import Svelte-Prism (Because Prism uses the window object we have to do some weird stuff to get it work. For now here is the work around.).

let Prism;
onMount(async () => {
    // Load the prismjs first after the page is loaded
    const prismModule = await import("svelte-prismjs");
    await import("prismjs/components/prism-c.js");
    await import("prism-svelte");

    await import("prismjs/plugins/line-highlight/prism-line-highlight.js");
    await import("prismjs/plugins/file-highlight/prism-file-highlight.js");
    // Once everything is loaded load the prismjs module
    Prism = prismModule.default;

  });

  <svelte:component this={Prism}>
  {`let b = 3;
function helloWorld() {
	console.log("Hello World");
}
`}
</svelte:component>

Routify Instructions

Routify Example

  1. Run npm install svelte-prismjs

  2. Go to scripts/base.config.js and add the rollup-css-only-plugin or go to the static/\_\_index.html file. If you do use npm to include css you will have to add it to App.svelte file under global styles.

let Prism;
onMount(async () => {
    // Load the prismjs first after the page is loaded
    const prismModule = await import("svelte-prismjs");
    await import("prismjs/components/prism-c.js");
    await import("prism-svelte");

    await import("prismjs/plugins/line-highlight/prism-line-highlight.js");
    await import("prismjs/plugins/file-highlight/prism-file-highlight.js");
    // Once everything is loaded load the prismjs module
    Prism = prismModule.default;

  });

  <svelte:component this={Prism}>
  {`let b = 3;
function helloWorld() {
	console.log("Hello World");
}
`}
</svelte:component>

Examples

Full Examples

Simple Example using the slot. Language will default to javascript.

<Prism>
  { `let b = 3; function helloWorld() { console.log("Hello World"); } `}
</Prism>

Example Language and line numbers and using the code prompt.

<Prism
  showLineNumbers="{true}"
  language="c"
  code="{`int b= 3;
int c=32;
  `}
/>

Props

  • code -> (string) for passing code into the element. You can also pass the code between the elements because it uses a slot.
  • language -> (string) Used the pass the language you are using. You will need to import the language if it is not included by default. Import this after you import the element, client side only. This is default to javascript.
import "prismjs/components/prism-c.js";
  • showLineNumbers -> (bool) Will turn on and off line numbers for your code. Defaulted to false.
  • normalizeWhiteSpace -> (bool) Will clean up the white space in your code. This is default to true.
  • normalizeWhiteSpaceConfig -> (object) Will be used to over ride the default config. For more information go here.
  • classes -> custom css classes for the pre-element.
  • You can also pass any props or styles in the component and it will be applied to the pre-element.