Skip to content

Fix text diffing by replacing 'diff-match-patch' with '@dmsnell/diff-match-patch' #378

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
atsjo opened this issue Jan 21, 2025 · 4 comments

Comments

@atsjo
Copy link

atsjo commented Jan 21, 2025

'diff-match-patch' have issues with surrogate pairs when diffing, you can test it in the live demo by changing:
left

...
"summary": "South America😀👍...
...

right

...
"summary": "South America👍...
...

Then the diff below disappears and you can see this error in console:

URIError: URI malformed
    at encodeURI (<anonymous>)
    at diff_match_patch.patch_obj.toString (demo.js:1476:30)
    at Array.join (<anonymous>)
    at diff_match_patch.patch_toText (demo.js:1377:21)
    at diff (demo.js:2457:28)
    at textsDiffFilter (demo.js:2490:24)
    at Pipe.process (demo.js:1570:9)
    at Processor.process (demo.js:1537:18)
    at DiffPatcher.diff (demo.js:2560:29)
    at compare2 (demo.js:3470:30)

In my own use of jsondiffpatch I have replaced diff-match-patch like this:

import type { Options } from 'jsondiffpatch';
import { diff_match_patch } from '@dmsnell/diff-match-patch';
/** replacing diff-match-patch with version from dmsnell which fixes surrogate pair problems  */
export const CustomDMP = diff_match_patch as unknown as NonNullable<Options['textDiff']>['diffMatchPatch'];

and then importing CustomDMP into jsondiffpatch via options...

As a bonus @dmsnell/diff-match-patch is also exported as esm...

@atsjo
Copy link
Author

atsjo commented Jan 21, 2025

And many thanks for a powerful library with an api that actually allowed me to replace this dependency, and also create my own custom formatter with support for text diff patches👍

@benjamine
Copy link
Owner

hi @atsjo thank you!
thanks for sharing and finding the alternative, I'll take a look, we've always debated removing the dependency completely (and make it always an opt-in) but now it's delicate because of backwards compat concerns.

is @dmsnell fork published to npm?
would be ideal if we can merge fixed back to @JackuB 's or agree on a fork to maintain.

@atsjo
Copy link
Author

atsjo commented Mar 24, 2025

yes it's on npm, so this is a very simple fix: "@dmsnell/diff-match-patch": "^1.1.0"
I use it, and it works perfect, beside from the hacky import... the repo is at https://github.com/dmsnell/diff-match-patch. This is the only maintained repo with an actual fix I could find, and this is also where the people fixing the issue agreed to maintain it: dmsnell/diff-match-patch#8

benjamine added a commit that referenced this issue Mar 28, 2025
fix: switch to @dmsnell/diff-match-patch fix #378
@atsjo
Copy link
Author

atsjo commented Mar 28, 2025

Thanx for the fix @benjamine, and the other fixes as well, I can now also enable moves again:-) Regarding my custom formatter implementation I have opened another issue with an associated PR for a easy way to extend JSONFormatter...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants