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

pretty-json-custom-element

v1.1.16

Published

Pretty JSON HTML Custom Element

Downloads

1,314

Readme

<pretty-json> HTML Custom Element

See the live demo

<pretty-json> is an HTML custom element that allows you to render JSON objects in HTML documents with human-readable formatting and expandable interaction for browsing deep JSON structures.

Usage

Include pretty-json-custom-element/index.js in your HTML page and use the HTML custom element as follows:

<script defer src="https://unpkg.com/pretty-json-custom-element/index.js"></script>
<pretty-json>
{
  "hello": "world",
  "value": 42,
  "enabled": true,
  "extra": null,
  "nested": { "key": "value" }
}
</pretty-json>

Your JSON will be rendered as a human-readable format:

Features

  • Outputs a valid JSON string that can be copied and pasted into a JSON file
  • HTML Custom Element without any dependencies, works in any modern browser
  • No need to install any dependencies or build tools, just drop the script in your HTML and start using it
  • Allows a high level of customization using CSS variables
  • Display large JSON objects with expandable and collapsible sections
  • Supports truncating very large strings and arrays with an ellipsis

To-do List

  • [ ] Support grouping very large arrays

Attributes

You can customize the rendering of the JSON object by setting the following attributes on the <pretty-json> element:

expand

By default, the JSON object is rendered expanded up to 1 level deep. You can set the expand attribute to a number to expand the JSON object up to that level deep:

<pretty-json expand="2">{"hello": {"world": "!"}}</pretty-json>

Collapsed by default

You can set the expand attribute to 0 to render the JSON object collapsed by default:

<pretty-json expand="0">{"hello": {"world": "!"}}</pretty-json>

truncate-string

By default, strings longer than 500 characters are truncated with an ellipsis. You can set the truncate-string attribute to a number to truncate strings longer than that number of characters:

<pretty-json truncate-string="10">
  {"hello": "long string that will be truncated"}
</pretty-json>

Customization

You can customize the appearance of the rendered JSON using CSS variables:

pretty-json {
  --key-color: #cc0000;
  --arrow-color: #737373;
  --brace-color: #0030f0;
  --bracket-color: #0030f0;
  --string-color: #009900;
  --number-color: #0000ff;
  --null-color: #666666;
  --boolean-color: #d23c91;
  --comma-color: #666666;
  --ellipsis-color: #666666;

  --indent: 2rem;
}

/* Also handle the dark mode */
@media (prefers-color-scheme: dark) {
  pretty-json {
    --key-color: #f73d3d;
    --arrow-color: #6c6c6c;
    --brace-color: #0690bc;
    --bracket-color: #0690bc;
    --string-color: #21c521;
    --number-color: #0078b3;
    --null-color: #8c8888;
    --boolean-color: #c737b3;
    --comma-color: #848181;
    --ellipsis-color: #c2c2c2;
  }
}

Development

To run the development server:

yarn install
yarn start

Running tests

This repository uses Playwright for testing.

To run the tests:

yarn test