diff2html

美化 diff 为 HTML 的 javascript 库(diff2html)。「Pretty diff to html javascript library (diff2html)」

Github stars Tracking Chart

diff2html

diff2html 从 git diff 或统一的 diff 输出生成漂亮的 HTML diff。

特性

  • 支持git和统一差异
  • 逐行和并排差异
  • 新旧行号
  • 插入和删除的行
  • GitHub 的视觉风格
  • 代码语法重点
  • 线相似度匹配
  • 轻松选择代码

在线示例

前往 diff2html

发行版

用法

Diff2Html 可以按发行部分中列出的各种方式使用。两种主要方法是:

  • Diff2Html:直接从库中使用解析器和 html 生成器,可以完全控制使用生成的 json 或 html 可以做什么。
  • Diff2HtmlUI:使用此包装器可以轻松将 html 注入 DOM 并为 diff 添加一些不错的功能,例如语法突出显示。

在下面,您可以找到有关每个选项的更多详细信息和示例。

Diff2Html 用法

为了在浏览器中正确加载,您始终需要在最终 HTML 中包含样式表。

导入样式表

<!-- CSS -->
<link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/npm/diff2html/bundles/css/diff2html.min.css" />

Diff2Html API

差异的 JSON 表示

function parse(diffInput: string, configuration: Diff2HtmlConfig = {}): DiffFile[];

差异的漂亮的 HTML 表示

function html(diffInput: string | DiffFile[], configuration: Diff2HtmlConfig = {}): string;

Diff2Html 配置

HTML输出接受带有配置的Javascript对象。可能的选择:

  • outputFormat:输出数据的格式:“逐行”或“并排”,默认为“逐行”
  • drawFileList:在 diff 之前显示文件列表:true 或 false,默认为 true
  • diffStyle:显示每行的差异级别:单词或字符,默认为单词
  • matching:匹配级别:“行”用于匹配行,“单词”用于匹配行和单词或“无”,默认为无
  • matchWordsThreshold:单词匹配的相似度阈值,默认为0.25
  • matchingMaxComparisons:最多执行一次这样的比较,以匹配更改块的行,默认值为2500
  • maxLineSizeInBlockForComparison:进行比较的块中最大行的最大字符数,默认为200
  • maxLineLengthHighlight:仅当行小于此值时才执行差异更改高亮显示,默认值为10000
  • renderNothingWhenEmpty:如果差异在比较中没有变化,则不呈现任何内容:true 或 false,默认为 false
  • createdTemplates:具有以前编译的模板的对象,以替换 html 的某些部分
  • rawTemplates:具有未编译模板的原始对象的对象,用于替换 html 的某些部分(有关可能的模板的更多信息,请查看 src/templates

Diff2Html 浏览器

导入样式表和库代码

<!-- CSS -->
<link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/npm/diff2html/bundles/css/diff2html.min.css" />
<!-- Javascripts -->
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/diff2html/bundles/js/diff2html.min.js"></script>

它现在可以作为一个名为Diff2Html的全局变量使用

document.addEventListener('DOMContentLoaded', () => {
  var diffHtml = global.Diff2Html.html('<Unified Diff String>', {
    drawFileList: true,
    matching: 'lines',
    outputFormat: 'side-by-side',
  });
  document.getElementById('destination-elem-id').innerHTML = diffHtml;
});

Diff2Html NPM / Node.js

const Diff2html = require('diff2html');
const diffJson = Diff2html.parse('<Unified Diff String>');
const diffHtml = Diff2html.html(diffJson, { drawFileList: true });
document.getElementById('destination-elem-id').innerHTML = diffHtml;

Diff2Html 示例

(恕删略。请查看源自述文件)

许可

版权所有 2014 年至今,Rodrigo Fernandes。根据 MIT 许可条款发布。

致谢

该项目的灵感来自 Scott Gonzálezpretty-diff

(The first version translated by vz on 2020.08.06)

Main metrics

Overview
Name With Ownerrtfpessoa/diff2html
Primary LanguageTypeScript
Program languageJavaScript (Language Count: 7)
PlatformLinux, Mac, Web browsers, Windows
License:MIT License
所有者活动
Created At2014-08-30 02:51:49
Pushed At2024-12-23 23:19:48
Last Commit At2024-12-23 23:17:59
Release Count181
Last Release Name3.4.51 (Posted on )
First Release Namev0.0.1 (Posted on )
用户参与
Stargazers Count3.1k
Watchers Count37
Fork Count282
Commits Count779
Has Issues Enabled
Issues Count275
Issue Open Count15
Pull Requests Count223
Pull Requests Open Count4
Pull Requests Close Count53
项目设置
Has Wiki Enabled
Is Archived
Is Fork
Is Locked
Is Mirror
Is Private

diff2html

Codacy Quality Badge
Codacy Coverage Badge
CircleCI

npm
Dependency Status
devDependency Status
cdnjs

node npm
npm
All Contributors
Gitter

diff2html generates pretty HTML diffs from git diff or unified diff output.

NPM

Table of Contents

Features

  • 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

Online Example

Go to diff2html

Distributions

Usage

Diff2Html can be used in various ways as listed in the distributions section. The two main ways are:

  • Diff2Html: using the parser and html generator directly from the library gives you complete
    control about what you can do with the json or html generated.
  • Diff2HtmlUI: using this wrapper makes it easy to inject the html in the DOM and adds some nice
    features to the diff, like syntax highlight.

Bellow you can find more details and exemples about each option.

Diff2Html Usage

To load correctly in the Browser you always need to include the stylesheet in the final HTML.

Import the stylesheet

<!-- CSS -->
<link rel="stylesheet" type="text/css" href="bundles/css/diff2html.min.css" />

You can also refer to it from a CDN like CDNJS.

Diff2Html API

JSON representation of the diff

function parse(diffInput: string, configuration: Diff2HtmlConfig = {}): DiffFile[];

Pretty HTML representation of the diff

function html(diffInput: string