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-prism-action

v1.1.4

Published

Svelte action for lazy render code blocks with prism.js.

Downloads

16

Readme

NPM Version

svelte-prism-action

Svelte action for lazy loading Prism.js code highlighting languages from remote or local path.

Example

Example Page

The action uses IntersectionObserver to dynamically render code highlighting. The prism.js language file is lazy loaded when the <code> element gets visible in the viewport.

Installation

$ npm install svelte-prism-action

Include a Prism.js CSS theme in your svelte component or use the <head> section of your index.html.

<link
  href="https://unpkg.com/[email protected]/themes/prism.css"
  rel="stylesheet"
/>

Usage

Import prism from svelte-prism-action. Add the action to a component with use:prism. All <code> elements with a class="language-..." will get highlighted once they have entered the viewport.

For available language tags see https://prismjs.com/#supported-languages .

HINT If you are writing your codeblocks directly into a svelte component you need to escape special characters (eg. curley brackets). Another way is to wrap the code inside {``}.

<script>
  import { prism } from "svelte-prism-action";
</script>

<!-- add action to component -->
<main use:prism>
  <!-- or set some options
  <main use:prism={{
    componentsUrl: "https://myPathToPrism/components"
  }}>
-->

  <!-- use in code blocks -->
  <pre>
    <code class="lang-css">{` <!-- wrap inside {``} if using inside svelte component-->
      .bg-gold{
        background: gold;
      }
    `}
    </code>
  </pre>

  <!-- use inline -->
  <code class="lang-javascript"
    >{`import {prism} from "svelte-prism-action";`}</code
  >
</main>

Options

You can use componentsUrl to set the URL from where to import the Prism.js language files. By default it uses unpkg cdn, but you can use a local resource instead.

To lazy load third party language files (eg. prism-svelte) or define a different file per language you can use thirdPartyUrls object with language id as key and the URL as the value.

You can also change the IntersectionObserver options. For more information on what they do see https://developer.mozilla.org/en-US/docs/Web/API/Intersection_Observer_API#Interfaces .

<main use:prism={{
  root: null,
  rootMargin: "100px",
  threshold: 0,
  componentsUrl: "https://unpkg.com/[email protected]/components",
  thirdPartyUrls: {
    svelte: "https://cdn.jsdelivr.net/npm/[email protected]/index.js"
  }
}}>
...
</main>

Changelog

1.1.3 (2024-04-17)

  • Add: Typescript support (generated types from JSDoc)
  • make svelte a peer dependency and version less restrictive (because should work with svelte version >= 3)

1.1.2 (2023-03-16)

  • Fix: highlight fails when prism-markup.js import not finished

1.1.1 (2023-03-15)

  • Fix: SvelteKit error while loading prism-markup.js

1.1.0 (2023-03-15)

  • Add: support for third party language files
  • Updated dependencies

1.0.7 (2021-06-10)

  • Fix #1: SvelteKit support. Fixed vite compile errors and added /* @vite-ignore */ to dynamic import().

1.0.6 (2021-03-19)

  • Updated dependencies

1.0.5 (2020-12-09)

  • Add: use custom build components.js to reduce package file size
  • updated dev dependencies
  • replaced custom language loader with prism.js builtin loader

1.0.4 (2020-12-08)

  • Fix: error if language dependency has dependency on its own

1.0.3 (2020-12-08)

  • Fix: error if markdown additional languages is null

1.0.2 (2020-12-08)

  • Add: import languages from markdown code blocks
  • Fix: loading languages with dependencies (eg. markdown) leads to error

1.0.1

  • added .npmignore

1.0.0 (2020-12-04)

  • initial release

License

MIT