forked from nakajmg/gh-diff-html
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
37 lines (31 loc) · 1.03 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
var diff2html = require('diff2html').Diff2Html
var difflib = require('difflib')
var format = require('util').format
/* https://github.com/rtfpessoa/diff2html#configuration */
var defaultOptions = {
fileName: '',
inputFormat: 'diff', // diff || json
outputFormat: 'line-by-line', // line-by-line || side-by-side
showFiles: false,
matching: 'none', // lines || words || none
synchronisedScroll: false,
matchWordsThreshold: 0.25,
matchingMaxComparisons: 2500
}
function ghDiffHTML (beforeString, afterString, options) {
options = Object.assign({}, defaultOptions, options)
var beforeArray = beforeString.split(/\r\n|\r|\n/)
var afterArray = afterString.split(/\r\n|\r|\n/)
var diffArray = difflib.unifiedDiff(beforeArray, afterArray, {
fromFile: options.fileName,
toFile: options.fileName
})
var diffString = format('diff --git %s %s\n%s',
options.fileName,
options.fileName,
diffArray.join('\n')
)
var html = diff2html.getPrettyHtml(diffString, options)
return html
}
module.exports = ghDiffHTML