-
Notifications
You must be signed in to change notification settings - Fork 1.1k
could generate something like git diff unified format by diff-match-patch? #114
Comments
Hey were you able to solve this? |
Was this solved? |
any updates? |
hey everyone, so I wanted to see if it's doable and after a little bit of research here and on the diff2html GitHub, I found this comment
However, that is only solving only a portion of the problem, diff2html requires a Git diff format and that requires file import DiffMatchPatch from 'diff-match-patch';
import { html, parse } from 'diff2html';
function showTextDiff(cleanType?: string) {
cleanType ||= cleanupType.value;
const dmp = new DiffMatchPatch();
const a = dmp.diff_linesToChars_(text1Ref.value, text2Ref.value);
const lineText1 = a.chars1;
const lineText2 = a.chars2;
const lineArray = a.lineArray;
const diffs = dmp.diff_main(lineText1, lineText2, false);
dmp.diff_charsToLines_(diffs, lineArray);
let patchToText = `diff --git a/file.txt b/file.txt
--- a/file.txt
+++ b/file.txt
`;
patchToText += dmp.patch_make(text1Ref.value, diffs);
const diffHtml = html(patchToText, { drawFileList: true, matching: 'lines', outputFormat: 'side-by-side' });
if (cleanType === 'semantic') {
dmp.diff_cleanupSemantic(diffs);
} else if (cleanType === 'efficiency') {
dmp.diff_cleanupEfficiency(diffs);
} else {
// no cleanup
}
diff2HtmlText = diffHtml; // diff2html result
} HTML PreviewConclusionSo it works even if it's a bit hacky, if someone else find a better and more elegant way of doing it then please post your answer. Also another thing to note is that the text might need some escaping or decoding since we can see in the print screen that carriage return were convert to |
I was wonder how could diff-match-patch generate a unified formate, so that we could use this unified formate to generate a beautiful diff side-by-side using [diff2html]
https://github.com/rtfpessoa/diff2html
For now, the text string generated by method
patch_toText
, could not be parsed because it's not a standard formate like whatgit diff
and linux diff commanddiff -u -N file1 file2
.Could anyone be helpful and tell me how to solve this issue? Much appriciated if anyone could help~
The text was updated successfully, but these errors were encountered: