From b43cc1ae918da47dceea730ee75c7e3e4365e12f Mon Sep 17 00:00:00 2001 From: Nick Brewer Date: Thu, 31 Jan 2019 17:58:48 -0600 Subject: [PATCH 1/3] Add configuration option to disable the render of templates. No template would render if the diff shows no difference in its comparison. --- src/diff2html.js | 3 ++- src/line-by-line-printer.js | 2 ++ test/line-by-line-tests.js | 19 ++++++++++++++++++- 3 files changed, 22 insertions(+), 2 deletions(-) diff --git a/src/diff2html.js b/src/diff2html.js index 7931496..6d7dba2 100644 --- a/src/diff2html.js +++ b/src/diff2html.js @@ -19,7 +19,8 @@ matching: 'none', matchWordsThreshold: 0.25, matchingMaxComparisons: 2500, - maxLineLengthHighlight: 10000 + maxLineLengthHighlight: 10000, + renderNothingWhenEmpty: false, }; /* diff --git a/src/line-by-line-printer.js b/src/line-by-line-printer.js index bdba538..8b41cc5 100644 --- a/src/line-by-line-printer.js +++ b/src/line-by-line-printer.js @@ -26,6 +26,8 @@ } LineByLinePrinter.prototype.makeFileDiffHtml = function(file, diffs) { + if (this.config.renderNothingWhenEmpty && file.blocks && !file.blocks.length) return ''; + var fileDiffTemplate = hoganUtils.template(baseTemplatesPath, 'file-diff'); var filePathTemplate = hoganUtils.template(genericTemplatesPath, 'file-path'); var fileIconTemplate = hoganUtils.template(iconsBaseTemplatesPath, 'file'); diff --git a/test/line-by-line-tests.js b/test/line-by-line-tests.js index 767e959..69b6dab 100644 --- a/test/line-by-line-tests.js +++ b/test/line-by-line-tests.js @@ -279,6 +279,23 @@ describe('LineByLinePrinter', function() { ' \n' + ''; + assert.equal(expected, fileHtml); + }); + it('should return empty when option renderNothingWhenEmpty is true and file blocks not present', function() { + var lineByLinePrinter = new LineByLinePrinter({ + renderNothingWhenEmpty: true, + }); + + var file = { + blocks: [], + }; + + var diffs = 'Random Html'; + + var fileHtml = lineByLinePrinter.makeFileDiffHtml(file, diffs); + + var expected = ''; + assert.equal(expected, fileHtml); }); }); @@ -399,7 +416,7 @@ describe('LineByLinePrinter', function() { isCombined: false }]; - var lineByLinePrinter = new LineByLinePrinter(); + var lineByLinePrinter = new LineByLinePrinter({ renderNothingWhenEmpty: false }); var html = lineByLinePrinter.generateLineByLineJsonHtml(exampleJson); var expected = '
\n' + From 3199a1ac7bc1815d0b8d6f86d881918ba6dae3ae Mon Sep 17 00:00:00 2001 From: Nick Brewer Date: Thu, 31 Jan 2019 18:01:56 -0600 Subject: [PATCH 2/3] Add new config option to the documentation. --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 2f30860..8cb6d05 100644 --- a/README.md +++ b/README.md @@ -188,6 +188,7 @@ The HTML output accepts a Javascript object with configuration. Possible options - `maxLineLengthHighlight`: only perform diff changes highlight if lines are smaller than this, default is `10000` - `templates`: object with previously compiled templates to replace parts of the html - `rawTemplates`: object with raw not compiled templates to replace parts of the html + - `renderNothingWhenEmpty`: render nothing if the diff shows no change in its comparison: `true` or `false`, default is `false` > For more information regarding the possible templates look into [src/templates](https://github.com/rtfpessoa/diff2html/tree/master/src/templates) ** Diff2HtmlUI Helper Options ** From 9c30d10b974112b1f17870c5c4afdb17463b9577 Mon Sep 17 00:00:00 2001 From: Nick Brewer Date: Thu, 31 Jan 2019 18:18:24 -0600 Subject: [PATCH 3/3] Fix trailing comma lint issue. --- src/diff2html.js | 2 +- test/line-by-line-tests.js | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/diff2html.js b/src/diff2html.js index 6d7dba2..2da3271 100644 --- a/src/diff2html.js +++ b/src/diff2html.js @@ -20,7 +20,7 @@ matchWordsThreshold: 0.25, matchingMaxComparisons: 2500, maxLineLengthHighlight: 10000, - renderNothingWhenEmpty: false, + renderNothingWhenEmpty: false }; /* diff --git a/test/line-by-line-tests.js b/test/line-by-line-tests.js index 69b6dab..45fef7d 100644 --- a/test/line-by-line-tests.js +++ b/test/line-by-line-tests.js @@ -283,11 +283,11 @@ describe('LineByLinePrinter', function() { }); it('should return empty when option renderNothingWhenEmpty is true and file blocks not present', function() { var lineByLinePrinter = new LineByLinePrinter({ - renderNothingWhenEmpty: true, + renderNothingWhenEmpty: true }); var file = { - blocks: [], + blocks: [] }; var diffs = 'Random Html';