Skip to content

Commit 1f46968

Browse files
authored
Merge pull request #389 from benjamine/site
feat: README and html demo updates
2 parents 51fea78 + 929ec02 commit 1f46968

10 files changed

+266
-46
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<p align="center">
2+
<img src="demos/html-demo/logo.svg" width="48px" align="center" alt="jsondiffpatch logo" />
23
<h1 align="center">jsondiffpatch</h1>
34
<p align="center">
45
<a href="https://jsondiffpatch.com">jsondiffpatch.com</a>

demos/html-demo/demo.ts

+62-19
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,9 @@ const instance = jsondiffpatch.create({
271271
if (typeof objRecord.id !== 'undefined') {
272272
return objRecord.id;
273273
}
274+
if (typeof objRecord.key !== 'undefined') {
275+
return objRecord.key;
276+
}
274277
if (typeof objRecord.name !== 'undefined') {
275278
return objRecord.name;
276279
}
@@ -485,11 +488,16 @@ const compare = function () {
485488
return;
486489
}
487490
const selectedType = getSelectedDeltaType();
491+
const resultsSections = document.getElementById('results')!;
488492
const visualdiff = document.getElementById('visualdiff')!;
489493
const annotateddiff = document.getElementById('annotateddiff')!;
490494
const jsondifflength = document.getElementById('jsondifflength')!;
491495
try {
492496
const delta = instance.diff(left, right);
497+
resultsSections.setAttribute(
498+
'data-diff',
499+
typeof delta === 'undefined' ? 'no-diff' : 'has-diff',
500+
);
493501

494502
if (typeof delta === 'undefined') {
495503
switch (selectedType) {
@@ -536,6 +544,7 @@ const compare = function () {
536544
console.error(err);
537545
console.error((err as Error).stack);
538546
}
547+
resultsSections.removeAttribute('data-diff');
539548
}
540549
document.getElementById('results')!.style.display = '';
541550
};
@@ -637,7 +646,7 @@ interface Data {
637646

638647
interface Load {
639648
data: (dataArg?: Data) => void;
640-
gist: (this: Load, id: string) => void;
649+
gist: (this: Load, id: string, onSuccess?: (gist: GistData) => void) => void;
641650
leftright: (
642651
this: Load,
643652
descriptionArg: string | undefined,
@@ -696,7 +705,7 @@ const load: Load = {
696705
}
697706
},
698707

699-
gist: function (id) {
708+
gist: function (id, onSuccess) {
700709
dom.getJson('https://api.github.com/gists/' + id, function (error, data) {
701710
interface GistError {
702711
message?: string;
@@ -712,29 +721,30 @@ const load: Load = {
712721
return;
713722
}
714723

715-
interface GistData {
716-
files: Record<
717-
string,
718-
{ language: string; filename: string; content: string }
719-
>;
720-
html_url: string;
721-
description: string;
722-
}
723-
724724
const gistData = data as GistData;
725725

726-
const filenames = [];
726+
const files: GistData['files'][string][] = [];
727+
727728
for (const filename in gistData.files) {
728729
const file = gistData.files[filename];
729-
if (file.language === 'JSON') {
730-
filenames.push(filename);
730+
if (/^json[5c]?$/i.test(file.language)) {
731+
files.push(file);
731732
}
732733
}
733-
filenames.sort();
734-
const files = [
735-
gistData.files[filenames[0]],
736-
gistData.files[filenames[1]],
737-
];
734+
735+
if (files.length < 1) {
736+
load.data({
737+
error: 'no JSON files found in this gist',
738+
});
739+
return;
740+
}
741+
if (files.length < 2) {
742+
files.push({
743+
language: 'JSON',
744+
filename: 'missing.json',
745+
content: '"only 1 JSON files found in the gist, need 2 to compare"',
746+
});
747+
}
738748
/* jshint camelcase: false */
739749
load.data({
740750
url: gistData.html_url,
@@ -748,6 +758,8 @@ const load: Load = {
748758
content: files[1].content,
749759
},
750760
});
761+
762+
onSuccess?.(gistData);
751763
});
752764
},
753765

@@ -908,3 +920,34 @@ if (urlQuery) {
908920
}
909921
},
910922
);
923+
924+
interface GistData {
925+
id: string;
926+
files: Record<
927+
string,
928+
{ language: string; filename: string; content: string }
929+
>;
930+
html_url: string;
931+
description: string;
932+
owner: { login: string };
933+
}
934+
935+
document.querySelector('#gist-link')?.addEventListener('input', (e) => {
936+
const match =
937+
/^(?:https?:\/\/)?gist\.github\.com\/([^/]+)\/([0-9a-f]+)/i.exec(
938+
(e.target as HTMLInputElement).value,
939+
);
940+
if (!match) return;
941+
load.gist(match[2], (gist) => {
942+
window.history.pushState({}, '', `?${gist.owner.login}/${gist.id}`);
943+
document.querySelector('h1')?.scrollIntoView({
944+
behavior: 'smooth',
945+
block: 'start',
946+
});
947+
const input = document.querySelector('#gist-link') as HTMLInputElement;
948+
if (input) {
949+
input.value = '';
950+
input.blur();
951+
}
952+
});
953+
});

demos/html-demo/favicon.ico

15 KB
Binary file not shown.

demos/html-demo/index.html

+111-15
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,43 @@
44
<title>JsonDiffPatch</title>
55
<meta
66
name="description"
7-
content="JsonDiffPatch is javascript/typescript library to deep diff json or text"
7+
content="JsonDiffPatch is a free online json diff tool and npm library to compare json and get a json diff"
88
/>
9-
<meta content="width=device-width, initial-scale=1" name="viewport" />
9+
<meta name="viewport" content="width=device-width, initial-scale=1" />
10+
<link rel="canonical" href="https://jsondiffpatch.com/" />
11+
<link rel="icon" href="logo.svg" type="image/svg+xml" />
12+
13+
<meta name="twitter:card" content="summary_large_image" />
14+
<meta name="twitter:creator" content="@beneidel" />
15+
<meta name="twitter:title" content="JsonDiffPatch" />
16+
<meta
17+
name="twitter:description"
18+
content="JsonDiffPatch is a free online json diff tool and npm library to compare json and get a json diff."
19+
/>
20+
<meta
21+
name="twitter:image"
22+
content="https://jsondiffpatch.com/jsondiffpatch-visual-diff.png"
23+
/>
24+
<meta property="og:type" content="website" />
25+
<meta property="og:title" content="JsonDiffPatch" />
26+
<meta
27+
property="og:description"
28+
content="JsonDiffPatch is a free online json diff tool and npm library to compare json and get a json diff."
29+
/>
30+
<meta property="og:locale" content="en_US" />
31+
<meta property="og:url" content="http://jsondiffpatch/" />
32+
<meta
33+
property="og:image"
34+
content="https://jsondiffpatch.com/jsondiffpatch-visual-diff.png"
35+
/>
36+
<meta property="og:image:type" content="image/png" />
37+
<meta property="og:image:width" content="774" />
38+
<meta property="og:image:height" content="774" />
39+
1040
<meta name="color-scheme" content="dark light" />
11-
<meta content="text/html;charset=utf-8" http-equiv="Content-Type" />
12-
<meta content="utf-8" http-equiv="encoding" />
1341
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
14-
<link rel="canonical" href="https://jsondiffpatch.com/" />
42+
<meta content="utf-8" http-equiv="encoding" />
43+
<meta content="text/html;charset=utf-8" http-equiv="Content-Type" />
1544
<link rel="stylesheet" href="style.css" type="text/css" media="screen" />
1645
<link
1746
rel="stylesheet"
@@ -130,13 +159,14 @@
130159
</style>
131160
<header>
132161
<div>
133-
<h1>
162+
<h1>JsonDiffPatch</h1>
163+
<div aria-hidden="true" id="diffed-h1">
134164
Js<span class="jsondiffpatch-textdiff-deleted"><span>on</span></span
135165
><span class="jsondiffpatch-textdiff-added"><span>Diff</span></span
136166
>Patch
137-
</h1>
167+
</div>
138168
<span id="description"></span>
139-
<a id="external-link" style="display: none">source</a>
169+
<a id="external-link" style="display: none" target="_blank">source</a>
140170
</div>
141171
<nav>
142172
<button id="color-scheme-toggle" title="Toggle theme">
@@ -243,7 +273,7 @@ <h2>JSON Right</h2>
243273
<span class="error-message"></span>
244274
</div>
245275
</div>
246-
<div id="results">
276+
<section id="results">
247277
<h2>Delta</h2>
248278
<label>
249279
<input
@@ -281,13 +311,79 @@ <h2>Delta</h2>
281311
<textarea id="json-delta" readonly> </textarea>
282312
<span class="error-message"></span>
283313
</div>
284-
</div>
314+
</section>
315+
<section id="features">
316+
<h2>Features</h2>
317+
<ul>
318+
<li>
319+
compare 2 json. also supports json5, json with comments, or plain text
320+
</li>
321+
<li>
322+
json diff as visual diff or json (see
323+
<a
324+
href="https://github.com/benjamine/jsondiffpatch/blob/master/docs/deltas.md"
325+
rel="nofollow"
326+
target="_blank"
327+
>delta format</a
328+
>)
329+
</li>
330+
<li>
331+
compare json from a gist (<a href="/?benjamine/9188826">example</a
332+
>)<br />
333+
<ol>
334+
<li>
335+
<a href="https://gist.github.com/" rel="nofollow"
336+
>create a gist</a
337+
>
338+
</li>
339+
<li>add 2 json files on the gist</li>
340+
<li>
341+
paste the gist url here: <input id="gist-link" type="text" />
342+
</li>
343+
</ol>
344+
</li>
345+
<li>
346+
smart diff arrays/lists. if items are objects a "key", "id", or "name"
347+
is automatically used to match
348+
</li>
349+
<li>
350+
text diff. for long string or text, a text diff is done at characther
351+
level (using
352+
<a href="https://github.com/google/diff-match-patch" target="_blank"
353+
>google/diff-match-patch</a
354+
>)
355+
</li>
356+
</ul>
357+
</section>
285358
<footer>
286-
<a href="https://github.com/benjamine/jsondiffpatch">Get JsonDiffPatch</a>
287-
<br />
288-
<p class="credits">
289-
JsonDiffPatch was developed by
290-
<a href="https://twitter.com/beneidel">Benjamin Eidelman</a>
359+
<p>
360+
powered by the
361+
<a
362+
class="library-link"
363+
href="https://github.com/benjamine/jsondiffpatch"
364+
>jsondiffpatch</a
365+
>
366+
library
367+
</p>
368+
<p align="center">
369+
<a href="https://twitter.com/beneidel" rel="nofollow"
370+
><img
371+
src="https://img.shields.io/badge/created%[email protected]"
372+
alt="Created by Benjamin Eidelman"
373+
/></a>
374+
<a href="https://opensource.org/licenses/MIT" rel="nofollow"
375+
><img
376+
src="https://img.shields.io/github/license/benjamine/jsondiffpatch"
377+
alt="License"
378+
/></a>
379+
<a href="https://www.npmjs.com/package/jsondiffpatch" rel="nofollow"
380+
><img src="https://img.shields.io/npm/dw/jsondiffpatch.svg" alt="npm"
381+
/></a>
382+
<a href="https://github.com/benjamine/jsondiffpatch" rel="nofollow"
383+
><img
384+
src="https://img.shields.io/github/stars/benjamine/jsondiffpatch"
385+
alt="stars"
386+
/></a>
291387
</p>
292388
</footer>
293389
<script src="build/demo.js" type="module"></script>
24.7 KB
Loading

demos/html-demo/llms.txt

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# https://jsondiffpatch.com llms.txt
2+
3+
jsondiffpatch is an npm package.
4+
it compares 2 json (or any 2 objects in javascript or typescript), and get a json diff with the changes.
5+
6+
``` ts
7+
import { diff } from "jsondiffpatch";
8+
9+
const delta = diff(left, right);
10+
```
11+
12+
## features
13+
14+
- deep diff json or objects, get a compact json diff delta
15+
- use delta to patch
16+
- smart array diffing using [LCS](http://en.wikipedia.org/wiki/Longest_common_subsequence_problem), **_IMPORTANT NOTE:_** to match objects inside an array you must provide an `objectHash` function (this is how objects are matched, otherwise a dumb match by position is used). For more details, check [Array diff documentation](docs/arrays.md)
17+
- multiple formatters (visual html, jsonpatch, console)
18+
- (optionally) uses [google-diff-match-patch](http://code.google.com/p/google-diff-match-patch/) for long text diffs (diff at character level)
19+
- reverse a delta, unpatch (eg. revert object to its original state using a delta)
20+
- simplistic, pure JSON, low footprint [delta format](docs/deltas.md)
21+
- multiple output formatters:
22+
- html (check it at the [Live Demo](https://jsondiffpatch.com))
23+
- annotated json (html), makes the JSON delta format self-explained
24+
- console (colored), try running `./node_modules/.bin/jsondiffpatch left.json right.json`
25+
- JSON Patch format RFC 6902 support
26+
- write your own! check [Formatters documentation](docs/formatters.md)
27+
- BONUS: `jsondiffpatch.clone(obj)` (deep clone)
28+
29+
## links
30+
31+
- [github repo](https://github.com/benjamine/jsondiffpatch)
32+
- [npm package](https://www.npmjs.com/package/jsondiffpatch)

demos/html-demo/logo.svg

+5
Loading

demos/html-demo/robots.txt

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
User-agent: *
2+
Allow: /
3+
Sitemap: https://jsondiffpatch.com/sitemap.xml

demos/html-demo/sitemap.xml

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
2+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">
4+
<url>
5+
<loc>https://jsondiffpatch.com/</loc>
6+
</url>
7+
</urlset>

0 commit comments

Comments
 (0)