Gathering detailed insights and metrics for laddiff2html
Gathering detailed insights and metrics for laddiff2html
Gathering detailed insights and metrics for laddiff2html
Gathering detailed insights and metrics for laddiff2html
npm install laddiff2html
3.4.24
Published on 06 Jan 2023
3.4.23
Published on 06 Jan 2023
Sticky file headers
Published on 01 Nov 2022
Add support for overriding highlight languages base on file extension
Published on 23 Oct 2022
Add language to extension mappings to highlight
Published on 15 Oct 2021
Improve semantic code highlight
Published on 03 Sept 2021
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
2,972 Stars
766 Commits
283 Forks
38 Watching
9 Branches
46 Contributors
Updated on 28 Nov 2024
TypeScript (88.07%)
Handlebars (5.72%)
CSS (4.1%)
HCL (0.97%)
Mustache (0.78%)
JavaScript (0.35%)
Shell (0.01%)
Cumulative downloads
Total Downloads
Last day
-80%
2
Compared to previous day
Last week
-70.5%
26
Compared to previous week
Last month
52.1%
321
Compared to previous month
Last year
767.1%
633
Compared to previous year
3
51
1
diff2html generates pretty HTML diffs from git diff or unified diff output.
Supports git and unified diffs
Line by line and Side by side diff
New and old line numbers
Inserted and removed lines
GitHub like visual style
Code syntax highlight
Line similarity matching
Easy code selection
Go to diff2html
highlight.js
supported languageshighlight.js
supported languageshighlight.js
implementation. You can use it without
syntax highlight or by passing your own implementation with the languages you preferhighlight.js
supported languageshighlight.js
supported languageshighlight.js
supported languageshighlight.js
supported languageshighlight.js
implementation. You can use it without
syntax highlight or by passing your own implementation with the languages you preferDiff2Html can be used in various ways as listed in the distributions section. The two main ways are:
Below you can find more details and examples about each option.
diff2html accepts the text contents of a unified diff or the superset format git diff (https://git-scm.com/docs/git-diff) (not combined or word diff). To provide multiples files as input just concatenate the diffs (just like the output of git diff).
Simple wrapper to ease simple tasks in the browser such as: code highlight and js effects
Create a Diff2HtmlUI instance
1constructor(target: HTMLElement, diffInput?: string | DiffFile[]) // diff2html-ui, diff2html-ui-slim 2constructor(target: HTMLElement, diffInput?: string | DiffFile[], config: Diff2HtmlUIConfig = {}, hljs?: HighlightJS) // diff2html-ui-base
Generate and inject in the document the Pretty HTML representation of the diff
1draw(): void
Enable extra features
1synchronisedScroll(): void 2fileListToggle(startVisible: boolean): void 3highlightCode(): void 4stickyFileHeaders(): void
synchronisedScroll
: scroll both panes in side-by-side mode: true
or false
, default is true
highlight
: syntax highlight the code on the diff: true
or false
, default is true
fileListToggle
: allow the file summary list to be toggled: true
or false
, default is true
fileListStartVisible
: choose if the file summary list starts visible: true
or false
, default is false
fileContentToggle
: allow each file contents to be toggled: true
or false
, default is true
stickyFileHeaders
: make file headers sticky: true
or false
, default is true
1<!-- CSS --> 2<link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/npm/diff2html/bundles/css/diff2html.min.css" /> 3 4<!-- Javascripts --> 5<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/diff2html/bundles/js/diff2html-ui.min.js"></script>
1const targetElement = document.getElementById('destination-elem-id');
2const configuration = { drawFileList: true, matching: 'lines' };
3
4const diff2htmlUi = new Diff2HtmlUI(targetElement, diffString, configuration);
5// or
6const diff2htmlUi = new Diff2HtmlUI(targetElement, diffJson, configuration);
1diff2htmlUi.draw();
NOTE: The highlight.js css should come before the diff2html css
1<!-- Stylesheet --> 2<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.8.0/styles/github.min.css" /> 3<link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/npm/diff2html/bundles/css/diff2html.min.css" /> 4 5<!-- Javascripts --> 6<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/diff2html/bundles/js/diff2html-ui.min.js"></script>
Pass the option
highlight
with value true or invokediff2htmlUi.highlightCode()
afterdiff2htmlUi.draw()
.
1document.addEventListener('DOMContentLoaded', () => { 2 const diffString = `diff --git a/sample.js b/sample.js 3 index 0000001..0ddf2ba 4 --- a/sample.js 5 +++ b/sample.js 6 @@ -1 +1 @@ 7 -console.log("Hello World!") 8 +console.log("Hello from Diff2Html!")`; 9 const targetElement = document.getElementById('myDiffElement'); 10 const configuration = { drawFileList: true, matching: 'lines', highlight: true }; 11 const diff2htmlUi = new Diff2HtmlUI(targetElement, diffString, configuration); 12 diff2htmlUi.draw(); 13 diff2htmlUi.highlightCode(); 14});
When using the auto
color scheme, you will need to specify both the light and dark themes for highlight.js to use.
1<link 2 rel="stylesheet" 3 href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.8.0/styles/github.min.css" 4 media="screen and (prefers-color-scheme: light)" 5/> 6<link 7 rel="stylesheet" 8 href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.8.0/styles/github-dark.min.css" 9 media="screen and (prefers-color-scheme: dark)" 10/>
Add the dependencies.
1<!-- Javascripts --> 2<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/diff2html/bundles/js/diff2html-ui.min.js"></script>
Invoke the Diff2HtmlUI helper Pass the option
fileListToggle
with value true or invokediff2htmlUi.fileListToggle()
afterdiff2htmlUi.draw()
.
1document.addEventListener('DOMContentLoaded', () => {
2 const targetElement = document.getElementById('myDiffElement');
3 var diff2htmlUi = new Diff2HtmlUI(targetElement, lineDiffExample, { drawFileList: true, matching: 'lines' });
4 diff2htmlUi.draw();
5 diff2htmlUi.fileListToggle(false);
6});
1<!doctype html> 2<html lang="en-us"> 3 <head> 4 <meta charset="utf-8" /> 5 <!-- Make sure to load the highlight.js CSS file before the Diff2Html CSS file --> 6 <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.7.1/styles/github.min.css" /> 7 <link 8 rel="stylesheet" 9 type="text/css" 10 href="https://cdn.jsdelivr.net/npm/diff2html/bundles/css/diff2html.min.css" 11 /> 12 <script type="text/javascript" src="https://cdn.jsdelivr.net/npm/diff2html/bundles/js/diff2html-ui.min.js"></script> 13 </head> 14 <script> 15 const diffString = `diff --git a/sample.js b/sample.js 16index 0000001..0ddf2ba 17--- a/sample.js 18+++ b/sample.js 19@@ -1 +1 @@ 20-console.log("Hello World!") 21+console.log("Hello from Diff2Html!")`; 22 23 document.addEventListener('DOMContentLoaded', function () { 24 var targetElement = document.getElementById('myDiffElement'); 25 var configuration = { 26 drawFileList: true, 27 fileListToggle: false, 28 fileListStartVisible: false, 29 fileContentToggle: false, 30 matching: 'lines', 31 outputFormat: 'side-by-side', 32 synchronisedScroll: true, 33 highlight: true, 34 renderNothingWhenEmpty: false, 35 }; 36 var diff2htmlUi = new Diff2HtmlUI(targetElement, diffString, configuration); 37 diff2htmlUi.draw(); 38 diff2htmlUi.highlightCode(); 39 }); 40 </script> 41 <body> 42 <div id="myDiffElement"></div> 43 </body> 44</html>
1import { Controller } from '@hotwired/stimulus'; 2 3import { Diff2HtmlUI, Diff2HtmlUIConfig } from 'diff2html/lib/ui/js/diff2html-ui-slim.js'; 4 5// Requires `npm install highlight.js` 6import 'highlight.js/styles/github.css'; 7import 'diff2html/bundles/css/diff2html.min.css'; 8 9export default class extends Controller { 10 connect(): void { 11 const diff2htmlUi = new Diff2HtmlUI(this.diffElement, this.unifiedDiff, this.diffConfiguration); 12 13 diff2htmlUi.draw(); 14 } 15 16 get unifiedDiff(): string { 17 return this.data.get('unifiedDiff') || ''; 18 } 19 20 get diffElement(): HTMLElement { 21 return this.element as HTMLElement; 22 } 23 24 get diffConfiguration(): Diff2HtmlUIConfig { 25 return { 26 drawFileList: true, 27 matching: 'lines', 28 }; 29 } 30}
JSON representation of the diff
1function parse(diffInput: string, configuration: Diff2HtmlConfig = {}): DiffFile[];
Pretty HTML representation of the diff
1function html(diffInput: string | DiffFile[], configuration: Diff2HtmlConfig = {}): string;
The HTML output accepts a Javascript object with configuration. Possible options:
outputFormat
: the format of the output data: 'line-by-line'
or 'side-by-side'
, default is 'line-by-line'
drawFileList
: show a file list before the diff: true
or false
, default is true
srcPrefix
: add a prefix to all source (before changes) filepaths, default is ''
. Should match the prefix used when
generating the diff.dstPrefix
: add a prefix to all destination (after changes) filepaths, default is ''
. Should match the prefix used
when generating the diffdiffMaxChanges
: number of changed lines after which a file diff is deemed as too big and not displayed, default is
undefined
diffMaxLineLength
: number of characters in a diff line after which a file diff is deemed as too big and not
displayed, default is undefined
diffTooBigMessage
: function allowing to customize the message in case of file diff too big (if diffMaxChanges
or
diffMaxLineLength
is set). Will be given a file index as a number and should return a string.matching
: matching level: 'lines'
for matching lines, 'words'
for matching lines and words or 'none'
, default
is none
matchWordsThreshold
: similarity threshold for word matching, default is 0.25
maxLineLengthHighlight
: only perform diff changes highlight if lines are smaller than this, default is 10000
diffStyle
: show differences level in each line: 'word'
or 'char'
, default is 'word'
renderNothingWhenEmpty
: render nothing if the diff shows no change in its comparison: true
or false
, default is
false
matchingMaxComparisons
: perform at most this much comparisons for line matching a block of changes, default is
2500
maxLineSizeInBlockForComparison
: maximum number of characters of the bigger line in a block to apply comparison,
default is 200
compiledTemplates
: object (Hogan.js template values) with previously
compiled templates to replace parts of the html, default is {}
. For example:
{ "tag-file-changed": Hogan.compile("<span class="d2h-tag d2h-changed d2h-changed-tag">MODIFIED</span>") }
rawTemplates
: object (string values) with raw not compiled templates to replace parts of the html, default is {}
.
For example: { "tag-file-changed": "<span class="d2h-tag d2h-changed d2h-changed-tag">MODIFIED</span>" }
For more information regarding the possible templates look into src/templates
highlightLanguages
: Map of extension to language name, used for highlighting. This overrides the default language
detection based on file extensions.colorScheme
: color scheme to use for the diff, default is light
. Possible values are light
, dark
, and auto
which will use the browser's preferred color scheme.Import the stylesheet and the library code.
To load correctly in the Browser you need to include the stylesheet in the final HTML.
1<!-- CSS --> 2<link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/npm/diff2html/bundles/css/diff2html.min.css" /> 3 4<!-- Javascripts --> 5<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/diff2html/bundles/js/diff2html.min.js"></script>
It will now be available as a global variable named Diff2Html
.
1document.addEventListener('DOMContentLoaded', () => { 2 var diffHtml = Diff2Html.html('<Unified Diff String>', { 3 drawFileList: true, 4 matching: 'lines', 5 outputFormat: 'side-by-side', 6 }); 7 document.getElementById('destination-elem-id').innerHTML = diffHtml; 8});
1const Diff2html = require('diff2html'); 2const diffJson = Diff2html.parse('<Unified Diff String>'); 3const diffHtml = Diff2html.html(diffJson, { drawFileList: true }); 4console.log(diffHtml);
1import * as Diff2Html from 'diff2html'; 2import { Component, OnInit } from '@angular/core'; 3 4export class AppDiffComponent implements OnInit { 5 outputHtml: string; 6 constructor() { 7 this.init(); 8 } 9 10 ngOnInit() {} 11 12 init() { 13 let strInput = 14 '--- 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'; 15 let outputHtml = Diff2Html.html(strInput, { drawFileList: true, matching: 'lines' }); 16 this.outputHtml = outputHtml; 17 } 18}
1<!doctype html> 2<html> 3 <head> 4 <title>diff2html</title> 5 </head> 6 <body> 7 <div [innerHtml]="outputHtml"></div> 8 </body> 9</html>
.angular-cli.json
- Add styles1"styles": [ 2 "diff2html.min.css" 3]
1<template> 2 <div v-html="prettyHtml" /> 3</template> 4 5<script> 6import * as Diff2Html from 'diff2html'; 7import 'diff2html/bundles/css/diff2html.min.css'; 8 9export default { 10 data() { 11 return { 12 diffs: 13 '--- 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', 14 }; 15 }, 16 computed: { 17 prettyHtml() { 18 return Diff2Html.html(this.diffs, { 19 drawFileList: true, 20 matching: 'lines', 21 outputFormat: 'side-by-side', 22 }); 23 }, 24 }, 25}; 26</script>
{"matching": "none"}
when invoking diff2htmlThis is a developer friendly project, all the contributions are welcome. To contribute just send a pull request with
your changes following the guidelines described in CONTRIBUTING.md
. I will try to review them as soon as possible.
Thanks goes to these wonderful people (emoji key):
This project follows the all-contributors specification. Contributions of any kind welcome!
Copyright 2014-present Rodrigo Fernandes. Released under the terms of the MIT license.
This project is inspired in pretty-diff by Scott González.
No vulnerabilities found.
Reason
no dangerous workflow patterns detected
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
Found 3/5 approved changesets -- score normalized to 6
Reason
security policy file detected
Details
Reason
0 commit(s) and 1 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
Reason
project is not fuzzed
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Reason
18 existing vulnerabilities detected
Details
Score
Last Scanned on 2024-11-18
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More