npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

diff-htmls

v0.0.1

Published

Fork of [html-diff-ts](https://github.com/ericmakesapps/html-diff-ts), which is a TypeScript 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/htmldif

Downloads

11

Readme

Diff HTML

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.

Roadmap

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.

Installation

npm i diff-htmls

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 "diff-htmls"

const oldHtml = "<p>Some <em>old</em> html here</p>"
const newHtml = "<p>Some <b>new</b> html goes here</p>"

const 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 "diff-htmls"

const oldHtml = "<p>12.11.2022</p>"
const newHtml = "<p>15.12.2022</p>"
const dateRegexp = /\d\d\.\d\d\.\d\d\d\d/gm

const result = (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>

With blocksExpression and compareBy

No diff

import diff from "diff-htmls"

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

const result =
	(oldHtml,
	newHtml,
	{
		blocksExpression: [
			{
				// match <img/> tag
				exp: /<img[\w\W]+?\/>/g,
				// compare only by title="" attribute
				compareBy: /title="[\w\W]+?"/g
			}
		]
	})

Result:
Will return the new string without comparison to old one - because title attribute is the same

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

Has diff

import diff from "diff-htmls"

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

const result =
	(oldHtml,
	newHtml,
	{
		blocksExpression: [
			{
				// match <img/> tag
				exp: /<img[\w\W]+?\/>/g,
				// compare only by title="" attribute
				compareBy: /title="[\w\W]+?"/g
			}
		]
	})

Result:
Will return the new string with diff to old one - because title attribute 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>