Diff2HtmlUI is a wrapper for browser-based diff rendering. It handles DOM injection, code highlighting, and UI effects like collapsible file lists.
Mandatory Imports
Include the following CSS and JS via CDN:
<link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/npm/diff2html/bundles/css/diff2html.min.css" />
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/diff2html/bundles/js/diff2html-ui.min.js"></script>
Initialization and Rendering
- Identify a target
HTMLElement where the diff will be injected. - Create a new
Diff2HtmlUI instance passing the target, the diff input (string or DiffFile[]), and an optional configuration object. - Call
.draw() to render the diff.
const targetElement = document.getElementById('destination-elem-id');
const configuration = { drawFileList: true, matching: 'lines' };
const diff2htmlUi = new Diff2HtmlUI(targetElement, diffString, configuration);
diff2htmlUi.draw();
const targetElement = document.getElementById('destination-elem-id');
const configuration = { drawFileList: true, matching: 'lines' };
const diff2htmlUi = new Diff2HtmlUI(targetElement, diffString, configuration);
// or
const diff2htmlUi = new Diff2HtmlUI(targetElement, diffJson, configuration);
diff2htmlUi.draw();