GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — GitHub - deadusr/html-diff-ts: Typescript port of htmldiff.net (http://github.com/Rohland/htmldiff.net)

BranchesTags
Open more actions menu
 
 

Latest commit

 

History

53 Commits
53 Commits

Folders and files

NameName
Last commit message
Last commit date
.github/workflows
.github/workflows
 
 
configs
configs
 
 
src
src
 
 
.gitignore
.gitignore
 
 
LICENSE
LICENSE
 
 
README.md
README.md
 
 
jest.config.ts
jest.config.ts
 
 
package-lock.json
package-lock.json
 
 
package.json
package.json
 
 
webpack.config.js
webpack.config.js
 
 

Repository files navigation

TypeScript port of HtmlDiff.NET

Installation

npm install html-diff-ts --save

Project Description

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.

API

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

Usage

Basic

import diff from 'html-diff-ts';

let oldHtml = '<p>Some <em>old</em> html here</p>';
let newHtml = '<p>Some <b>new</b> html goes here</p>';

let result = 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 here

With blocksExpression

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.

import diff from 'html-diff-ts';

let oldHtml = '<p>12.11.2022</p>';
let newHtml = '<p>15.12.2022</p>';
let dateRegepx = /\d\d\.\d\d\.\d\d\d\d/gm;

let result = (oldHtml, newHtml, {blocksExpression: [{exp: dateRegepx}]});

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>

With blocksExpression and compareBy

No diff

import diff from 'html-diff-ts';

let oldHtml = '<img src="./old.png" title="title-1" />'; // "src" attr is different but "title" - is the same
let newHtml = '<img src="./new.png" title="title-1" />'; // "src" attr is different but "title" - is the same

let result =
    (oldHtml,
    newHtml,
    {
        blocksExpression: [
            {
                exp: /<img[\w\W]+?\/>/g, // match <img/> 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

<img src="./new.png" title="title-1" />

Has diff

import diff from 'html-diff-ts';

let oldHtml = '<img src="./old.png" title="old-title" />'; // "title" attr is different
let newHtml = '<img src="./new.png" title="new-title" />'; // "title" attr is different

let result =
    (oldHtml,
    newHtml,
    {
        blocksExpression: [
            {
                exp: /<img[\w\W]+?\/>/g, // match <img/> 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

<del>
    <img src="./old.png" title="old-title" />
</del>
<ins>
    <img src="./new.png" title="new-title" />
</ins>

Visualization:

<p>
- <img src="./old.png" title="old-title" />
+ <img src="./new.png" title="new-title" />
</p>

About

Typescript port of htmldiff.net (http://github.com/Rohland/htmldiff.net)

www.npmjs.com/package/html-diff-ts

Resources

Readme
License
Activity

Stars

15 stars

Watchers

0 watching

Forks

7 forks
Report repository

Releases

Packages

Used by

Contributors

Languages

You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.