JavaScript port of [HtmlDiff.NET](https://github.com/Rohland/htmldiff.net) which is itself a C# port of the Ruby implementation, [HtmlDiff](https://github.com/myobie/htmldiff/).
npm install html-diff-tsJavaScript port of HtmlDiff.NET
npm install html-diff-ts --save
Comparing two HTML blocks, and returns a meshing of the two that includes and 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 and ), ins.mod elements are inserted with the new styles.
Options:
- blocksExpression - list of Regular Expressions which will be countes as one block (token) instead of dividing it on parts by default mechanism (better see example)
- exp - Regular Expression for token itself
- compareBy - Regular Expression for part of the token by which will be comparison made
``javascript
import diff from 'html-diff-ts';
let oldHtml = '
Some old html here
';Some new html goes here
';let result = diff(oldHtml, newHtml);
`
Result:
`html`
Some old new html here
Visualization:
`diff`
Some
- old
+ new
html here
The tokenizer works by running the diff on words, but sometimes this isn't ideal. For example, it may look clunky when a date is edited from 12 Jan 2022 to 14 Feb 2022. It might be neater to treat the diff on the entire date rather than the independent tokens.
You can achieve this using AddBlockExpression. Note, the Regex example is not meant to be exhaustive to cover all dates. If text matches the expression, the entire phrase is included as a single token to be compared, and that results in a much neater output.
`javascript
import diff from 'html-diff-ts';
let oldHtml = '
12.11.2022
';15.12.2022
';let result = (oldHtml, newHtml, {blocksExpression: [{exp: dateRegepx}]});
`
Result:
`html
12.11.2022 15.12.2022
Visualization:
`diff
- 12.11.2022
+ 15.12.2022
`$3
No diff
`javascript
import diff from 'html-diff-ts';let oldHtml = '
'; // "src" attr is different but "title" - is the same
let newHtml = '
'; // "src" attr is different but "title" - is the same
let result =
(oldHtml,
newHtml,
{
blocksExpression: [
{
exp: /
/g, // match
tag
compareBy: /title="[\w\W]+?"/g, // compare only by title="" attribute
},
],
});
`Result:
Will return the new string without comparison to old one - because title attrubite is the same
`html

`Has diff
`javascript
import diff from 'html-diff-ts';let oldHtml = '
'; // "title" attr is different
let newHtml = '
'; // "title" attr is different
let result =
(oldHtml,
newHtml,
{
blocksExpression: [
{
exp: /
/g, // match
tag
compareBy: /title="[\w\W]+?"/g, // compare only by title="" attribute
},
],
});
`Result:
Will return the new string with diff to old one - because title attrubite has changed
`html


`Visualization:
`diff
- 
+ 
``