0

我正在使用diff2html在 HTML 页面中显示 git PR 中的差异。我已经添加了必要的脚本并像这样调用对象

<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>

现在这显示Files changed (0)。我给出的 URL 是在他们网站的演示部分给出的,并且有差异。我这样做正确吗?或者我如何从 URL 中获取?

4

2 回答 2

0

实际上getPrettyHtml接受diff string,而不是链接本身。没有办法用 diff2html 本身处理差异计算,这个库只是美化了它。您可以使用此 GitHub 提示轻松检索差异:

将 .patch 或 .diff 添加到 Git 明文视图的 URL 末尾。

要通过 URL 检索数据,您可以查看此答案

于 2019-12-19T09:33:31.970 回答
0

有点无关紧要,但可以在 PHP 中使用xdiffPECL 包)来生成统一模型(即库喜欢的字符串或文件之间的差异)并将该输出提供给Diff2HtmlUI以生成漂亮的 Gitlab/Github 差异基于 UI/UX。

我就是这样做的,而且效果非常好:

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();

~ 一切顺利

于 2021-08-24T12:17:45.753 回答