Skip to content

Commit

Permalink
Release v0.5.0
Browse files Browse the repository at this point in the history
  • Loading branch information
dpilafian committed Feb 14, 2021
1 parent ca25d5d commit 904bb48
Show file tree
Hide file tree
Showing 12 changed files with 101 additions and 46 deletions.
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ _Pretty-print JSON data into HTML to indent and colorize (written in TypeScript)
[![Vulnerabilities](https://snyk.io/test/github/center-key/pretty-print-json/badge.svg)](https://snyk.io/test/github/center-key/pretty-print-json)
[![Hits](https://data.jsdelivr.com/v1/package/npm/pretty-print-json/badge?style=rounded)](https://www.jsdelivr.com/package/npm/pretty-print-json)
[![Build](https://travis-ci.org/center-key/pretty-print-json.svg)](https://travis-ci.org/center-key/pretty-print-json)
[![Build](https://github.com/center-key/pretty-print-json/workflows/build/badge.svg)](https://github.com/center-key/pretty-print-json/actions?query=workflow%3Abuild)

<img width=350 alt=screenshot
src=https://3.bp.blogspot.com/-M13HQRG7cqQ/XaQvF0Q_KyI/AAAAAAAAJeg/3_CTIgPAh5Yqa29aYPvB1aTO9VsUlksLACNcBGAsYHQ/s1600/pretty-print-json.png>
Expand All @@ -21,10 +22,11 @@ https://pretty-print-json.js.org
### Web browser
Load from the [jsdelivr.com CDN](https://www.jsdelivr.com/package/npm/pretty-print-json):
```html
<link rel=stylesheet href=https://cdn.jsdelivr.net/npm/pretty-print-json@0.4/dist/pretty-print-json.css>
<link rel=stylesheet href=https://cdn.jsdelivr.net/npm/pretty-print-json@0.5/dist/pretty-print-json.css>
...
<script src=https://cdn.jsdelivr.net/npm/pretty-print-json@0.4/dist/pretty-print-json.min.js></script>
<script src=https://cdn.jsdelivr.net/npm/pretty-print-json@0.5/dist/pretty-print-json.min.js></script>
```
For **dark mode**, replace `pretty-print-json.css` with `pretty-print-json.dark-mode.css` in the `<link>` tag.
### Node.js server
Install package for node:
```shell
Expand All @@ -34,6 +36,10 @@ Import into your application:
```javascript
import { prettyPrintJson } from 'pretty-print-json';
```
Or for older CommonJS/UMD environments:
```javascript
const { prettyPrintJson } = require('pretty-print-json'); //deprecated
```

## 3) Usage
### API
Expand Down
13 changes: 12 additions & 1 deletion dist/pretty-print-json.css
Original file line number Diff line number Diff line change
@@ -1,10 +1,21 @@
/*! pretty-print-json v0.4.5 ~ github.com/center-key/pretty-print-json ~ MIT License */
/*! pretty-print-json v0.5.0 ~ github.com/center-key/pretty-print-json ~ MIT License */
.json-key { color: brown; }
.json-string { color: olive; }
.json-number { color: navy; }
.json-boolean { color: teal; }
.json-null { color: dimgray; }
.json-mark { color: black; }
a.json-link { color: purple; transition: all 400ms; }
a.json-link:visited { color: slategray; }
a.json-link:hover { color: blueviolet; }
a.json-link:active { color: slategray; }
.dark-mode .json-key { color: indianred; }
.dark-mode .json-string { color: darkkhaki; }
.dark-mode .json-number { color: deepskyblue; }
.dark-mode .json-boolean { color: mediumseagreen; }
.dark-mode .json-null { color: darkorange; }
.dark-mode .json-mark { color: silver; }
.dark-mode a.json-link { color: mediumorchid; }
.dark-mode a.json-link:visited { color: slategray; }
.dark-mode a.json-link:hover { color: violet; }
.dark-mode a.json-link:active { color: silver; }
3 changes: 2 additions & 1 deletion dist/pretty-print-json.d.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
//! pretty-print-json v0.4.5 ~ github.com/center-key/pretty-print-json ~ MIT License
//! pretty-print-json v0.5.0 ~ github.com/center-key/pretty-print-json ~ MIT License

export declare type FormatOptions = {
indent?: number;
linkUrls?: boolean;
quoteKeys?: boolean;
};
export declare type JsonType = 'key' | 'string' | 'number' | 'boolean' | 'null' | 'mark';
declare const prettyPrintJson: {
version: string;
toHtml(thing: unknown, options?: FormatOptions | undefined): string;
Expand Down
11 changes: 11 additions & 0 deletions dist/pretty-print-json.dark-mode.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/*! pretty-print-json v0.5.0 ~ github.com/center-key/pretty-print-json ~ MIT License */
.json-key { color: indianred; }
.json-string { color: darkkhaki; }
.json-number { color: deepskyblue; }
.json-boolean { color: mediumseagreen; }
.json-null { color: darkorange; }
.json-mark { color: silver; }
a.json-link { color: mediumorchid; transition: all 400ms; }
a.json-link:visited { color: slategray; }
a.json-link:hover { color: violet; }
a.json-link:active { color: silver; }
11 changes: 6 additions & 5 deletions dist/pretty-print-json.esm.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! pretty-print-json v0.4.5 ~ github.com/center-key/pretty-print-json ~ MIT License
//! pretty-print-json v0.5.0 ~ github.com/center-key/pretty-print-json ~ MIT License

const prettyPrintJson = {
version: '0.4.5',
version: '0.5.0',
toHtml(thing, options) {
const defaults = { indent: 3, linkUrls: true, quoteKeys: false };
const settings = { ...defaults, ...options };
Expand All @@ -12,6 +12,7 @@ const prettyPrintJson = {
.replace(/</g, '&lt;')
.replace(/>/g, '&gt;');
};
const spanTag = (type, display) => display ? '<span class=json-' + type + '>' + display + '</span>' : '';
const buildValueHtml = (value) => {
const strType = /^"/.test(value) && 'string';
const boolType = ['true', 'false'].includes(value) && 'boolean';
Expand All @@ -20,16 +21,16 @@ const prettyPrintJson = {
const urlRegex = /https?:\/\/[^\s"]+/g;
const makeLink = (link) => '<a class=json-link href="' + link + '">' + link + '</a>';
const display = strType && settings.linkUrls ? value.replace(urlRegex, makeLink) : value;
return '<span class=json-' + type + '>' + display + '</span>';
return spanTag(type, display);
};
const replacer = (match, p1, p2, p3, p4) => {
const part = { indent: p1, key: p2, value: p3, end: p4 };
const findName = settings.quoteKeys ? /(.*)(): / : /"([\w$]+)": |(.*): /;
const indentHtml = part.indent || '';
const keyName = part.key && part.key.replace(findName, '$1$2');
const keyHtml = part.key ? '<span class=json-key>' + keyName + '</span>: ' : '';
const keyHtml = part.key ? spanTag('key', keyName) + spanTag('mark', ': ') : '';
const valueHtml = part.value ? buildValueHtml(part.value) : '';
const endHtml = part.end || '';
const endHtml = spanTag('mark', part.end);
return indentHtml + keyHtml + valueHtml + endHtml;
};
const jsonLine = /^( *)("[^"]+": )?("[^"]*"|[\w.+-]*)?([{}[\],]*)?$/mg;
Expand Down
11 changes: 6 additions & 5 deletions dist/pretty-print-json.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! pretty-print-json v0.4.5 ~ github.com/center-key/pretty-print-json ~ MIT License
//! pretty-print-json v0.5.0 ~ github.com/center-key/pretty-print-json ~ MIT License

const prettyPrintJson = {
version: '0.4.5',
version: '0.5.0',
toHtml(thing, options) {
const defaults = { indent: 3, linkUrls: true, quoteKeys: false };
const settings = { ...defaults, ...options };
Expand All @@ -12,6 +12,7 @@ const prettyPrintJson = {
.replace(/</g, '&lt;')
.replace(/>/g, '&gt;');
};
const spanTag = (type, display) => display ? '<span class=json-' + type + '>' + display + '</span>' : '';
const buildValueHtml = (value) => {
const strType = /^"/.test(value) && 'string';
const boolType = ['true', 'false'].includes(value) && 'boolean';
Expand All @@ -20,16 +21,16 @@ const prettyPrintJson = {
const urlRegex = /https?:\/\/[^\s"]+/g;
const makeLink = (link) => '<a class=json-link href="' + link + '">' + link + '</a>';
const display = strType && settings.linkUrls ? value.replace(urlRegex, makeLink) : value;
return '<span class=json-' + type + '>' + display + '</span>';
return spanTag(type, display);
};
const replacer = (match, p1, p2, p3, p4) => {
const part = { indent: p1, key: p2, value: p3, end: p4 };
const findName = settings.quoteKeys ? /(.*)(): / : /"([\w$]+)": |(.*): /;
const indentHtml = part.indent || '';
const keyName = part.key && part.key.replace(findName, '$1$2');
const keyHtml = part.key ? '<span class=json-key>' + keyName + '</span>: ' : '';
const keyHtml = part.key ? spanTag('key', keyName) + spanTag('mark', ': ') : '';
const valueHtml = part.value ? buildValueHtml(part.value) : '';
const endHtml = part.end || '';
const endHtml = spanTag('mark', part.end);
return indentHtml + keyHtml + valueHtml + endHtml;
};
const jsonLine = /^( *)("[^"]+": )?("[^"]*"|[\w.+-]*)?([{}[\],]*)?$/mg;
Expand Down
4 changes: 2 additions & 2 deletions dist/pretty-print-json.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 6 additions & 5 deletions dist/pretty-print-json.umd.cjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//! pretty-print-json v0.4.5 ~ github.com/center-key/pretty-print-json ~ MIT License
//! pretty-print-json v0.5.0 ~ github.com/center-key/pretty-print-json ~ MIT License

(function (factory) {
if (typeof module === "object" && typeof module.exports === "object") {
Expand All @@ -13,7 +13,7 @@
Object.defineProperty(exports, "__esModule", { value: true });
exports.prettyPrintJson = void 0;
const prettyPrintJson = {
version: '0.4.5',
version: '0.5.0',
toHtml(thing, options) {
const defaults = { indent: 3, linkUrls: true, quoteKeys: false };
const settings = { ...defaults, ...options };
Expand All @@ -24,6 +24,7 @@
.replace(/</g, '&lt;')
.replace(/>/g, '&gt;');
};
const spanTag = (type, display) => display ? '<span class=json-' + type + '>' + display + '</span>' : '';
const buildValueHtml = (value) => {
const strType = /^"/.test(value) && 'string';
const boolType = ['true', 'false'].includes(value) && 'boolean';
Expand All @@ -32,16 +33,16 @@
const urlRegex = /https?:\/\/[^\s"]+/g;
const makeLink = (link) => '<a class=json-link href="' + link + '">' + link + '</a>';
const display = strType && settings.linkUrls ? value.replace(urlRegex, makeLink) : value;
return '<span class=json-' + type + '>' + display + '</span>';
return spanTag(type, display);
};
const replacer = (match, p1, p2, p3, p4) => {
const part = { indent: p1, key: p2, value: p3, end: p4 };
const findName = settings.quoteKeys ? /(.*)(): / : /"([\w$]+)": |(.*): /;
const indentHtml = part.indent || '';
const keyName = part.key && part.key.replace(findName, '$1$2');
const keyHtml = part.key ? '<span class=json-key>' + keyName + '</span>: ' : '';
const keyHtml = part.key ? spanTag('key', keyName) + spanTag('mark', ': ') : '';
const valueHtml = part.value ? buildValueHtml(part.value) : '';
const endHtml = part.end || '';
const endHtml = spanTag('mark', part.end);
return indentHtml + keyHtml + valueHtml + endHtml;
};
const jsonLine = /^( *)("[^"]+": )?("[^"]*"|[\w.+-]*)?([{}[\],]*)?$/mg;
Expand Down
32 changes: 18 additions & 14 deletions docs/dynamic/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<link rel=stylesheet href=https://cdn.jsdelivr.net/npm/@fortawesome/[email protected]/css/all.min.css>
<link rel=stylesheet href=https://cdn.jsdelivr.net/npm/[email protected]/dist/dna.css>
<link rel=stylesheet href=https://cdn.jsdelivr.net/npm/[email protected]/dist/reset.min.css>
<link rel=stylesheet href=https://cdn.jsdelivr.net/npm/pretty-print-json@0.4/dist/pretty-print-json.css>
<link rel=stylesheet href=https://cdn.jsdelivr.net/npm/pretty-print-json@0.5/dist/pretty-print-json.css>
<style>
body {
max-width: 700px;
Expand Down Expand Up @@ -44,7 +44,7 @@ <h2>ES Module Dynamic Loading</h2>

<main>
<p>Page load:<br><code></code></p>
<p><button>Reveal JSON</button></p>
<p><button>Load Module</button></p>
<pre><output></output></pre>
</main>

Expand All @@ -59,30 +59,34 @@ <h2>ES Module Dynamic Loading</h2>
<script src=https://cdn.jsdelivr.net/npm/[email protected]/dist/lib-x.min.js></script>
<script>
const app = {
reveal(module) {
const buttonClickTime = new Date();
const url = 'https://cdn.jsdelivr.net/npm/pretty-print-json@0.4/dist/pretty-print-json.esm.js';
loadModule(module) {
const buttonClicked = new Date();
const url = 'https://cdn.jsdelivr.net/npm/pretty-print-json@0.5/dist/pretty-print-json.esm.js';
import(url).then(module => {
const moduleLoadedTime = new Date();
const pageLoaded = $('body').data().pageLoaded;
const moduleLoaded = new Date();
const message = {
message: 'ES Module Dynamic Loading',
version: module.prettyPrintJson.version,
times: {
pageLoaded___: $('body').data().pageLoadTime,
buttonClicked: buttonClickTime,
moduleLoaded_: moduleLoadedTime,
pageLoaded___: pageLoaded,
buttonClicked: buttonClicked,
moduleLoaded_: moduleLoaded,
},
milliseconds: {
waitForClick: buttonClicked - pageLoaded,
loadModule: moduleLoaded - buttonClicked,
},
loadMilliseconds: moduleLoadedTime - buttonClickTime,
};
const html = module.prettyPrintJson.toHtml(message);
$('main output').hide().html(html).slideDown();
});
},
setup() {
const pageLoadTime = new Date();
$('body').data().pageLoadTime = pageLoadTime;
$('main code').text(pageLoadTime.toString());
$('main button').on({ click: app.reveal });
const pageLoaded = new Date();
$('body').data().pageLoaded = pageLoaded;
$('main code').text(pageLoaded.toString());
$('main button').on({ click: app.loadModule });
libX.ui.autoDisableButtons();
},
};
Expand Down
Loading

0 comments on commit 904bb48

Please sign in to comment.