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 🙏

© 2025 – Pkg Stats / Ryan Hefner

prism-json-fold

v1.0.1

Published

Adds toggle switch to JSON objects and arrays

Downloads

125

Readme

Prism JSON fold

Convert JSON arrays and objects to foldable groups in Prism.Js

travis-image npm-image

Table of Contents

This plugin parses the json output of Prism and wraps them inside groups to make them foldable. In nutshell it does the following:

  1. Adds a before-update hook to Prism hooks system.
  2. Reads the HTML generated by Prism.
  3. Parses the HTML and wraps them inside groups.
  4. Updates the HTML on Prism instance.

💁 Learn more about why we took the approach of parsing HTML.

Setup

Install it from npm as follows

npm i prism-json-fold

Require or import it after Prism.

import 'prismjs'
import 'prism-json-fold'

// commonjs
require('prismjs')
require('prism-json-fold')

or grap it from CDN

<script type="text/javascript" src="https://unpkg.com/prism-json-fold"></script>

Why parse the HTML?

After observing the Prism output carefully, it does not group the HTML in any way. All of the prism elements are sibling span tags.

Also extending the json grammar is not possible, since Prism grammar (ideally javascript) regular expressions are not recursive in nature, which means you cannot define a regex, which will recursively find Objects or Arrays inside JSON.

Finally, the only way is to parse the HTML output and wrap things together. There is little overhead in doing that, since we have to go through each HTML element and here's the benchmarks on same.

  • The benchmarks are ran on Chrome Mac OSX.
  • And here's the sample data.
## Small data set
Chrome 69.0.3497 (Mac OS X 10.13.6)  Small sample: Without json fold at 64445 ops/sec
Chrome 69.0.3497 (Mac OS X 10.13.6)  Small sample: With json fold at 51232 ops/sec

## Large data set
Chrome 69.0.3497 (Mac OS X 10.13.6)  Large sample: Without json fold at 1995 ops/sec
Chrome 69.0.3497 (Mac OS X 10.13.6)  Large sample: With json fold at 1431 ops/sec

Tips to improve speed

In ideal situation, you must load and run PrismJs inside a worker. It will avoid blocking the UI completely and helps in delivering better experience. Learn more about loading prism inside a worker.

CSS

The following CSS is required to get the desired output. Feel free to modify the CSS as per your needs (if required).

Not everything requires changes. So I have added a comment You may have to change this, for properties that may require changes.

code .block {
 position: relative;
}

code i.caret {
  position: absolute;
  font-style: normal;
  cursor: pointer;

  /** You may have to change this */
  width: 10px;
  height: 10px;
  top: -3px;
  left: -12px;
  color: #ccc;
}

code i.caret:before {
  /** You may have to change this: It only works when using font awesome */
  content: '\25B8';
}

code .block-wrapper {
  display: inline-block;
  position: relative;
  overflow: hidden;
  vertical-align: top;
  
  /** You may have to modify this */
  max-height: 24px;
  max-width: 45px;
  color: #ccc;
}

code .block-wrapper > * {
  opacity: 0;
}

code .block-wrapper:before {
  content: '...';
  top: -2px;
  position: absolute;
  left: 3px;
}

code .block-wrapper:after {
  top: 0px;
  position: absolute;
  right: 0;
}

code .block-keyed-object > .block-wrapper:after,
code .block-object > .block-wrapper:after {
  content: '}';
}

code .block-keyed-array > .block-wrapper:after,
code .block-array > .block-wrapper:after {
  content: ']';
}

code .block.open > .block-wrapper {
  display: initial;
}

code .block.open > .block-wrapper > * {
  opacity: 1;
}

code .block.open > .block-wrapper:before,
code .block.open > .block-wrapper:after {
  display: none;
}

code .block.open > i.caret:before {
  transform: rotate(90deg);
}

Event listeners

The plugin also attaches an event listener on the i.caret to toggle css classes. Ideally these event listeners will be removed by the garbage collector when DOM elements have been removed. However, feel free to ping me if you detect any memory leaks (especially with SPA's).

Change log

The change log can be found in the CHANGELOG.md file.

Contributing

The code is written as clean as possible, while keeping the peformance as the main priority. If you are contributing, make sure:

  1. To avoid creating too many arrays and then joining them recursively to create the string. Instead concatenate to the string directly.
  2. Avoid using ES next code, which requires polyfills or will increase the bundle size in any manner.

Everyone is welcome to contribute. Please take a moment to review the contributing guidelines.

Authors & License

Harminder Virk and contributors.

MIT License, see the included MIT file.