This is originally a fork of html-diff-ts, which is a TypeScript port of HtmlDiff.NET which is itself a C# port of the Ruby implementation, HtmlDiff.
The purpose of this fork was to fix the handling of overlapping blocks. For example, if
diffing URLs, I want them to not be broken up, but also I want img tags to not be broken
up. However, imgs contain URLs in the href, which throws an error. This fixes that.
This will also fix the weird word-by-word result that this package gives. It's technically correct, but a better user experience would be collapsing sequential changes.
npm i diff-htmls
Comparing 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.
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 itselfcompareBy- Regular Expression for part of the token by which will be comparison made
importdifffrom"diff-htmls"constoldHtml="<p>Some <em>old</em> html here</p>"constnewHtml="<p>Some <b>new</b> html goes here</p>"constresult=diff(oldHtml,newHtml)Result:
<p>
Some <del><em>old</em></del><ins><b>new</b></ins> html here
</p>Visualization:
Some
- <em>old</em>+ <b>new</b>
html hereThe 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.
importdifffrom"diff-htmls"constoldHtml="<p>12.11.2022</p>"constnewHtml="<p>15.12.2022</p>"constdateRegexp=/\d\d\.\d\d\.\d\d\d\d/gmconstresult=(oldHtml,newHtml,{blocksExpression: [{exp: dateRegexp}]})Result:
<p><p><del>12.11.2022</del><ins>15.12.2022</ins></p></p>Visualization:
<p>
- <em>12.11.2022</em>+ <b>15.12.2022</b>
</p>No diff
importdifffrom"diff-htmls"// "src" attr is different but "title" - is the sameconstoldHtml='<img src="./old.png" title="title-1" />'// "src" attr is different but "title" - is the sameconstnewHtml='<img src="./new.png" title="title-1" />'constresult=(oldHtml,newHtml,{blocksExpression: [{// match <img/> tagexp: /<img[\w\W]+?\/>/g,// compare only by title="" attributecompareBy: /title="[\w\W]+?"/g}]})Result:
Will return the new string without comparison to old one - because title attribute is the
same
<imgsrc="./new.png" title="title-1" />Has diff
importdifffrom"diff-htmls"// "title" attr is differentconstoldHtml='<img src="./old.png" title="old-title" />'// "title" attr is differentconstnewHtml='<img src="./new.png" title="new-title" />'constresult=(oldHtml,newHtml,{blocksExpression: [{// match <img/> tagexp: /<img[\w\W]+?\/>/g,// compare only by title="" attributecompareBy: /title="[\w\W]+?"/g}]})Result:
Will return the new string with diff to old one - because title attribute has changed
<del><imgsrc="./old.png" title="old-title" /></del><ins><imgsrc="./new.png" title="new-title" /></ins>Visualization:
<p>
- <img src="./old.png" title="old-title" />+ <img src="./new.png" title="new-title" />
</p>