A wrapper around Google's
diff_match_patch
library. Google's library is awesome and can be fine-tuned in
multiple ways, but their API is a bit awkward, probably cause they
support the same API in 8 different languages.
Also, the library does not provide any beautification of the diff-ed
output. They provide a reference implementation (look for
diff_prettyHtml
in the
API docs
) but recommend developers to write their own - and the given method is
not customizable
.
This library is an alternative to embedding
30+ lines of cryptic JS
in your code. In short, it tries to bridge the gap between the
awesome diff_match_patch
library and a simple user-experience.
This blog post explains things in a bit more detail.
From npm.js.
jquery.js
, diff_match_patch.js
and
jquery.pretty-text-diff.js
in your page, something like:<script src="https://github.com/arnab/jQuery.PrettyTextDiff/raw/master/path/to/jquery.min.js" type="text/javascript"></script>
<script src="https://github.com/arnab/jQuery.PrettyTextDiff/raw/master/path/to/diff_match_patch.js" type="text/javascript"></script>
<script src="https://github.com/arnab/jQuery.PrettyTextDiff/raw/master/path/to/deal_override_requests.min.js" type="text/javascript"></script>
$(selector).prettyTextDiff({
// options
});
Check out the customizable options below.
<ins>
and <del>
as much as you want. A simple example is
in the jsfiddle demo.option | default | explanation |
---|---|---|
originalContainer | .original | CSS-selector for the element that holds the original text. $(elem).text() will be used to extract the text. |
changedContainer | .changed | CSS-selector for the element that holds the changed text. $(elem).text() will be used to extract the text. |
diffContainer | .diff | CSS-selector for the element where the diff will be put. |
cleanup | true | Whether diff_match_patch's diff_cleanupSemantic() will be used or not. true will produce a human-redable diff, whereas false will be more to the liking of machines. See the demo for the effect. |
debug | false | see some debug output in your browser's console |
originalContentv1.0.4+ | None | Pass the original content as a parameter in your JS file, instead of creating an element in your HTML code to extract this content. |
changedContentv1.0.4+ | None | Pass the changed content as a parameter in your JS file, instead of creating an element in your HTML code to extract this content. |
node
and npm
(comes with node now)coffeescript
and
uglify
:
npm install -g coffee-script uglify-js
cake build
to produce the output JS (minified)package.json
file.git tag x.y.z
git push && git push --tags && npm publish
git checkout -b branch_name
git push origin branch_name
Copyright (c) 2013 Arnab Deka. Licensed under MIT LICENSE for details.