kpdecker / jsdiff

A javascript text differencing implementation.
BSD 3-Clause "New" or "Revised" License
8.13k stars 500 forks source link

How can this be integrated to diff2html? #198

Closed UnderNotic closed 10 months ago

UnderNotic commented 7 years ago

Is there a way to place the output from jsdiff to diff2html?

rolshevsky commented 6 years ago

@UnderNotic sure, you can do it in the following way:

var diff = JsDiff.createPatch('fileName', 'oldString', 'newString', 'oldHeader', 'newHeader');
var diff2htmlUi = new Diff2HtmlUI({ diff: diff });

diff2htmlUi.draw('#line-by-line', {inputFormat: 'diff', showFiles: true, matching: 'lines'});
diff2htmlUi.fileListCloseable('#line-by-line', false);
fracasula commented 6 years ago

@UnderNotic here's another way, very similar to the above:

import {createPatch} from 'diff';
import {Diff2Html} from 'diff2html';
import 'diff2html/dist/diff2html.min.css';

const input = createPatch('diff', this.textA, this.textB);

document.getElementById('textDiff').innerHTML = Diff2Html.getPrettyHtml(
    input,
    {
        inputFormat: 'diff',
        matching: 'lines'
    }
);
PabodhaWimalasuriya123 commented 5 years ago

@kpdecker

hi, I need a small help from you. image

I am using jsdiff to generate unified different string between two strings. Then using diff2html to generate the html.

var unifiedDiff = Diff.createPatch("", beforeChange, afterChange, "", "");
var diffHtml = Diff2Html.getPrettyHtml(unifiedDiff, { inputFormat: 'diff', matching: 'lines', outputFormat: 'side-by-side' })

My problem is, how can I get rid of compressed lines highlighted with a red arrow ? (And show available data lines on those compressed lines ?).

Is there any option to disable this behavior ?

Thank You.

ISNIT0 commented 4 years ago

I ended up getting what I needed without diff2html by doing this:

const style = {
    added: 'background-color: rgb(204, 232, 204);color: rgb(0, 55, 0);',
    removed: 'background-color: rgb(232, 204, 204);color: rgb(55, 0, 0);text-decoration:line-through;',
};
const diff = Diff.diffSentences(oldText, currentText);
const html = [];
for (const sentence of diff) {
    if (sentence.added) {
        html.push(`<span style='${style.added}'>${sentence.value.replace(/\n/g, '<br />')}</span>`);
    } else if (sentence.removed) {
        html.push(`<span style='${style.removed}'>${sentence.value.replace(/\n/g, '<br />')}</span>`);
    }
}
const outHTML = html.join('<br />');

Outputs something like this: image