GitHub-like HTML Diff Tool – diff2html.js

Category: Javascript , Recommended | June 21, 2019
Author: rtfpessoa
Views Total: 7,092
Official Page: Go to website
Last Update: June 21, 2019
License: MIT


GitHub-like HTML Diff Tool – diff2html.js


diff2html.js is a tiny JavaScript library used to create a Github-style HTML diff tool with syntax highlighting for git diff output.


# Yarn
$ yarn add diff2html

$ npm install diff2html --save

How to use it:

Include the diff2html.js and other required resources on the web page.

<!-- diff2html files -->
<link rel="stylesheet" href="dist/diff2html.min.css">
<script src="dist/diff2html.min.js"></script>

<!-- highlight.js -->
<link rel="stylesheet" href="/path/to/github.min.css">
<script src="/path/to/highlight.min.js"></script>
<script src="/path/to/scala.min.js"></script>

Basic usage.

// Pretty HTML diff
Diff2Html.getPrettyHtml(exInput, configuration)

// Intermediate Json From Git Word Diff Output

Available configurations with default values.

Diff2Html.getPrettyHtml(exInput, {

  // the format of the input data: 'diff' or 'json', default is 'diff'
  inputFormat: 'diff',

  // the format of the output data: 'line-by-line' or 'side-by-side'
  outputFormat: 'line-by-line', 

  // show a file list before the diff: true or false,
  showFiles: false,

  // 'lines' for matching lines, 'words' for matching lines and words
  matching: 'none',

  // similarity threshold for word matching, default is 
  matchWordsThreshold: .25

  // perform at most this much comparisons for line matching a block of changes
  matchingMaxComparisons: 2500,

  // maximum number os characters of the bigger line in a block to apply comparison
  maxLineSizeInBlockForComparison: 200

  // only perform diff changes highlight if lines are smaller than this
  maxLineLengthHighlight: 10000,

  // object with previously compiled templates to replace parts of the html
  templates: {}

  // object with raw not compiled templates to replace parts of the html
  rawTemplates: {},

  // render nothing if the diff shows no change in its comparison
  renderNothingWhenEmpty: false



v2.10.0 (06/21/2019)

  • Add configuration option maxLineSizeInBlockForComparison to configure max line size to do comparisons per block. This options helps avoiding OOM in blocks with large line (e.g.: minified files).

v2.8.0 (05/02/2019)

  • Fix linenumber scroll

You Might Be Interested In:

Leave a Reply