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

j-prism-toolbar

v1.3.1

Published

Simple toolbar for PrismJS. Typical 'windows' style toolbar, with minimize/expand, fullscreen, and copy-to-clipboard buttons.

Downloads

11

Readme

j-prism-toolbar NodeJS Test Status NPM Badge

Quick toolbar thrown together for PrismJS. Typical "windows" style toolbar, with minimize/expand, fullscreen, and copy-to-clipboard buttons.

See it in action all over joshuatz.com 😄 (example page)


Demo

Demo GIF


Installation

Loading via Script Tag

There is only one file that needs to be added to your webpage (or bundled): dist/jPrismToolbar.min.js. As an alternative to adding this as a dependency, you can also fetch it directly from a CDN, for example:

<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/jPrismToolbar.min.js"></script>

Loading Via Imports

If you are using a bundler, you can use the CommonJS or ESM exports provided by this package.

// ESM
import {PrismToolbar} from 'j-prism-toolbar'

// CJS
const {PrismToolbar} = require('j-prism-toolbar');

There are also type definitions included in this package, which both TypeScript and IDEs like VSCode can take advantage of for better type-safety and Intellisense support.

Usage

To actually instantiate and render all the toolbars, it is usually as easy as creating a new instance and calling autoInit. For example, if you always wrap your code like

<pre><code></code></pre>

Then adding the toolbar is as simple as:

(new PrismToolbar()).autoInit();

Make sure you run the initializer after the DOM is ready; either put your code in the footer, or attach to the DOMContentLoaded event.

The default CSS selector used by .autoInit() looks like pre > code[class*="language-"], whereas .autoInitAll() is pre > code[class*="language-"], pre[class] > code

You can also pass a custom CSS selector to .autoInit()

Sample

Here is a full sample:

<body>
    <!-- Here is an embed -->
    <pre>
        <code class="language-js">console.log('Hello World!');</code>
    </pre>

    <!-- Sample of how to load and init the toolbar -->
    <script src="https://cdn.jsdelivr.net/npm/j-prism-toolbar"></script>
    <script>
        (new PrismToolbar()).autoInit();
    </script>
</body>

Advanced Usage

Certain settings can be controlled on a 'per instance' basis and can be either passed into the constructor or added as attributes on the code element. Some settings are global.

Global Settings

Default values are shown. All settings are optional, and you can pass a single CSS selector string to constructor instead of options.selector, if you prefer.

interface GlobalConfig {
     /**
     * CSS selector used to find code elements to inject toolbar into.
     *  - Used for .autoInit()
     * @default 'pre > code[class*="language-"]'
     */
    selector?: string;
    /**
     * Alternative to `selector` (and overrides it)
     */
    targetElements?: NodeListOf<HTMLElement> | HTMLElement;
    /**
     * Extra logging
     * @default false
     */
    debug: boolean;
    /**
     * Whether or not the toolbar + code embed should be wrapped *together* in wrapper, or just have toolbar and code elem be sibling
     * @default true
     */
    wrapCombo: boolean;
    /**
     * The style of icon to use for the toolbar.
     * For choices other than `emoji` or `plaintext`, you must have the font pack installed.
     * @default 'emoji'
     */
    iconStyle: 'emoji' | 'plaintext' | 'material' | 'fontawesome';
    /**
     * Use if you have code blocks that are not getting picked up by the Prism highlighter, because they don't adhere to the standard.
     * - For example, `autoFix` can turn Pandoc output of `<pre class="js"><code></code></pre>` into the correct standard of `<pre><code class="language-js"></code></pre>`, and then re-highlight it with Prism.
     * @default false
     */
    autoFix: boolean;
    // The following settings can also be overridden on a per-instance basis, through HTML attributes
    /**
     * Use CSS animations
     * @default true
     */
    animate: boolean;
    /**
     * Use linewrap - e.g. white-space: pre-wrap
     */
    lineWrap: boolean;
    /**
     * If set to a URL string that returns text, it will be loaded into the embed
     * @default false
     */
    remoteSrc: boolean | string;
}

const myConfig: GlobalConfig = {
    // ...
}

const myToolbarManager = new PrismToolbar(myConfig);

Per-Embed Settings

These settings can be changed per-embed (aka per-instance), and controlled via HTML attributes. If set, they will override the global settings (see above section).

Settings key | HTML attribute key | default | description --- | --- | --- | ---- animate | data-animate | true | boolean, whether CSS animations should be used (for example, when collapsing or opening an embed) lineWrap | data-linewrap | false | boolean, whether or not the preview should use linewrap remoteSrc | data-jptremote | false | Set to a URL string that returns code as text (such as what you get when you click "view raw" on Github) and it will get pulled into the code preview box via AJAX and my tool will trigger Prism to re-highlight it.Experimental: Can also support multi-file raw GitHub Gist URLs.

Notes:

  • Almost all classes are prefixed with "j" (just like the repo) to avoid conflicts with other libraries or stylesheets.
  • Certain buttons try to use icons, but will fallback gracefully depending on what you have installed. It will try font-awesome -> materializecss -> fallback
  • For the "copy-to-clipboard" feature, it will try to use ClipboardJS, but if it is not available within the global window scope, it will fall back to:
    1. Trying the browser API (navigator.clipboard)
    2. Selecting the text and prompting user to copy themselves

Changelog

Version | Date | Notes --- | --- | --- v1.3.1 | 6/12/21 | Small tweak - attach unique IDs to each instance. v1.3.0 | 6/12/21| Major refactor, started exporting types, support ESM and CJS imports, support multi-file GitHub Gist URLs v1.2.3 | 1/10/21 | Upgraded "copy-to-clipboard" feature