Closed rickywu closed 5 years ago
@rickywu what is the value of d
?
@rtfpessoa copy the code save as html and open it in browser, you can see the two texts and d is the diff from these two input box
@rickywu if it would be that easy I could have done it. Time is always short to help. In general as a generic note please provide as much information as possible to help other people instead of trying for them to fix your problems.
Still, after a quick look in the documentation of google/diff-match-patch it does not seem to provide a unified diff which is the required input for diff2html. You need to generate the output from other tool probably.
Check https://github.com/rtfpessoa/diff2html/issues/198 as an alternative. or look for another diff tool in npmjs.org
var dmp = new diff_match_patch();
var a = dmp.diff_linesToChars_(firstDescribe, secondDescribe);
var lineText1 = a.chars1;
var lineText2 = a.chars2;
var lineArray = a.lineArray;
var diffs = dmp.diff_main(lineText1, lineText2, false);
dmp.diff_charsToLines_(diffs, lineArray);
console.info(diffs);
let patchMake = dmp.patch_make(firstDescribe,diffs);
let patchToText = dmp.patch_toText(patchMake);
Then patchToText can be used for diff2html
That should work
var dmp = new diff_match_patch(); var a = dmp.diff_linesToChars_(firstDescribe, secondDescribe); var lineText1 = a.chars1; var lineText2 = a.chars2; var lineArray = a.lineArray; var diffs = dmp.diff_main(lineText1, lineText2, false); dmp.diff_charsToLines_(diffs, lineArray); console.info(diffs); let patchMake = dmp.patch_make(firstDescribe,diffs); let patchToText = dmp.patch_toText(patchMake);
Then patchToText can be used for diff2html
This was a great start but was not enough to get it fully working because Git diff seems to require having the 2 files being diffed at the start of the diff header. So with that in mind, I was able to get it working by prefixing the a
and b
files as shown below, it needs more tweaks for carriage return but it's a good start as you can see in the print screen below
let patchToText = `diff --git a/file.txt b/file.txt
--- a/file.txt
+++ b/file.txt
`;
patchToText += dmp.patch_make(text1Ref.value, diffs);
EDIT
in the end, I found this Stack Overflow Create unified diff text for diff2html in browser and the answer to use jsdiff works a lot better and it's super short.
const diff = JSDiff.createTwoFilesPatch('file', 'file', text1Ref.value, text2Ref.value);
const diffHtml = html(diff, { drawFileList: true, matching: 'lines', outputFormat: 'side-by-side' });
which now gives the following, it's so much better with line number and everything :)
Seems this issue closed 3 years ago but I still got this error, download diff_match_patch.js from https://github.com/google/diff-match-patch and change path if needed
try this sample