@ali-tas/htmldiff-js

中文文档
2.0.1发布于 2024/11/12ISCTypeScriptESM + CJS零依赖近 30 天下载 3.6万

JavaScript port of HtmlDiff.Net which is itself a C# port of HtmlDiff

原始使用文档
文档目录3 节
  1. Project Description
  2. Usage
  3. Html

README

Typescript port of htmldiff-js, which is the JavaScript port of HtmlDiff.NET which is itself a C# port of the Ruby implementation, HtmlDiff.

Project Description

Diffs two HTML blocks, and returns a meshing of the two that includes <ins> and <del> elements. The classes of these elements are ins.diffins for new code, del.diffdel for removed code, and del.diffmod and ins.diffmod for sections of code that have been changed.

For "special tags" (primarily style tags such as <em> and <strong>), ins.mod elements are inserted with the new styles.

Further description can be found at this blog post written by Rohland, the author of HtmlDiff.NET.

Note: The diffing algorithm isn't perfect. One example is that if a new <p> ends in the same string as the previous <p> tag did, two <ins> tags will be created: one starting at the beginning of the common string in the first <p> and one in the second <p> containing all the content up to the point the trailing common string begins. It's a little frustrating, but I didn't write the algorithm (and honestly don't really understand it); I only ported it.

Usage

Html

<html>
  <body>
    <div id="oldHtml">
      <p>Some <em>old</em> html here</p>
    </div>

    <div id="newHtml">
      <p>Some <b>new</b> html goes here</p>
    </div>

    <div id="diffHtml"></div>
  </body>
</html>

JavaScript

import HtmlDiff from 'htmldiff-js';

const oldHtml = document.getElementById('oldHtml');
const newHtml = document.getElementById('newHtml');
const diffHtml = document.getElementById('diffHtml');

diffHtml.innerHTML = HtmlDiff.execute(oldHtml.innerHTML, newHtml.innerHTML);

版本与发布记录

@ali-tas/htmldiff-js 共发布 9 个版本,首次发布于 2020/10/23;过去 12 个月发布了 0 个版本。

版本发布日期距今
2.0.1latest2024/11/122 年前
2.0.02024/07/132 年前
1.1.32021/07/225 年前
1.1.22020/11/105 年前
1.1.12020/11/036 年前
1.1.02020/11/036 年前

关于 @ali-tas/htmldiff-js 的常见问题

@ali-tas/htmldiff-js 是什么?

JavaScript port of HtmlDiff.Net which is itself a C# port of HtmlDiff

如何安装 @ali-tas/htmldiff-js?

在项目目录运行 npm install @ali-tas/htmldiff-js,或使用 pnpm add / yarn add / bun add。

@ali-tas/htmldiff-js 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@ali-tas/htmldiff-js 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@ali-tas/htmldiff-js 可以商用吗?

@ali-tas/htmldiff-js 使用 ISC 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@ali-tas/htmldiff-js 的下载量有多少?

根据 npm 官方下载统计,@ali-tas/htmldiff-js 在 2026/10/06 – 2026/11/04 这 30 天内被下载 35,902 次,日均约 1,197 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@ali-tas/htmldiff-js 还在维护吗?

最新版本 2.0.1 发布于 2024/11/12(2 年前);至今共发布 9 个版本,其中过去 12 个月发布了 0 个;首次发布于 2020/10/23。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。