在浏览器中为diff2html创建统一的差异文本

时间:2019-02-22 11:48:44

标签: javascript difflib unified-diff diff2html

是否有一个库可以根据diff2html可以使用的两个字符串生成统一的diff?我已经尝试过difflib,但是输出似乎不符合diff2html所需的要求。我需要一个.js库,我可以将其导入网页中以在JSON之间产生差异。

试图使用lineterm参数,但无法使该工具正常工作。如果我以文档中的字符串为例,那么它将起作用:

--- a/server/vendor/golang.org/x/sys/unix/zsyscall_linux_mipsle.go\n+++ b/server/vendor/golang.org/x/sys/unix/zsyscall_linux_mipsle.go\n@@ -1035,6 +1035,17 @@ func Prctl(option int, arg2 uintptr, arg3 uintptr, arg4 uintptr, arg5 uintptr) (\n \n // THIS FILE IS GENERATED BY THE COMMAND AT THE TOP; DO NOT EDIT\n \n+func Pselect(nfd int, r *FdSet, w *FdSet, e *FdSet, timeout *Timespec, sigmask *Sigset_t) (n int, err error) {\n+\tr0, _, e1 := Syscall6(SYS_PSELECT6, uintptr(nfd), uintptr(unsafe.Pointer(r)), uintptr(unsafe.Pointer(w)), uintptr(unsafe.Pointer(e)), uintptr(unsafe.Pointer(timeout)), uintptr(unsafe.Pointer(sigmask)))\n+\tn = int(r0)\n+\tif e1 != 0 {\n+\t\terr = errnoErr(e1)\n+\t}\n+\treturn\n+}\n+\n+// THIS FILE IS GENERATED BY THE COMMAND AT THE TOP; DO NOT EDIT\n+\n func read(fd int, p []byte) (n int, err error) {\n \tvar _p0 unsafe.Pointer\n \tif len(p) > 0 {\n

但是无法获得difflib的任何输出以在diff2html中生成任何内容。

1 个答案:

答案 0 :(得分:0)

最终使用jsdiff

var diff = Diff.createTwoFilesPatch("file", "file", oldText, newText);

,以便它可以像这样与diff2html一起使用:

Diff2Html.getPrettyHtml(diff,
    {inputFormat: 'diff', showFiles: false, matching: 'lines', outputFormat: 'side-by-side'}
);