Diff2html diff not loading

1.1k Views Asked by At

I am using diff2html to show the diffs in a git PR in an HTML page. I have added the necessary scripts and is calling the object like this

<script type="text/javascript">
    var diffHtml = Diff2Html.getPrettyHtml(
        'https://github.com/rtfpessoa/diff2html/pull/106',
            {
                inputFormat: 'diff', 
                showFiles: true, 
                matching: 'lines', 
                outputFormat: 'side-by-side'
            }
        );
        document.getElementById("diff-container").innerHTML = diffHtml;
</script>

Now this is showing Files changed (0). The URL I have given is the one given in the demo section of their website and have diff. I am doing this correctly ? Or how can I fetch from a the URL ?

2

There are 2 best solutions below

4
On

Actually getPrettyHtml accepts a diff string, not the link itself. There is no way to handle diff calculations with diff2html itself, this library only prettifies it. You can easily retrieve diff with this GitHub tip:

Add .patch or .diff to the end of URLs for Git’s plaintext views.

To retrieve the data via URL you could take a look at this answer.

0
On

A bit irrelevant but one can use xdiff (PECL package) in PHP in order to produce unified model (ie. difference between string or file that is what the library likes) and supply that output to the Diff2HtmlUI to produce the nice Gitlab/Github diff based UI/UX.

That is how I did and its fantastically works:

Step 1: $string1 = 'contains from file-1/string1';
Step 2: $string2 = 'contains from file-2/string2';
Step 3: $getUnifiedDiff = xdiff_string_diff($string1, $string2);
Step 4: Initialise the configuration (as you've done above) and follow through 
var diff2htmlUi = new Diff2HtmlUI(targetElement, $getUnifiedDiff, configuration);
diff2htmlUi.draw();
diff2htmlUi.highlightCode();

~ All the best