How to compare 2 versions of the same document

#1

I’m looking to obtain the text differences between 2 versions of the same document. I currently save each version as an object which contains the steps in JSON format. I want a functionality where I can select a saved version of the document and it would highlight all of the differences from the current version. I tried following the track changes example and adapt it to my codebase, but I wasn’t successful due to a few incompatibilities. I was wondering if there is some easier way of obtaining those changesets between versions

0 Likes

#2

It is usually preferable (because cheaper and more predictable) to use the data from the steps that were applied to the document to figure out which parts changed.

However, when that’s not enough, you can narrow the changes down to a single range with findDiffStart and findDiffEnd methods.

If you want a proper, minimal diff, you’ll have to run an actual diffing algorithm. There’s an implementation in prosemirror-changeset, but that one has some hard-coded quirks (such as ignoring attribute values) that might not be appropriate to your use case. It shouldn’t be all too hard to fork it and adjust it to do what you need.

1 Like

#3

There is also https://gitlab.com/mpapp-public/prosemirror-recreate-steps which lets you create steps to get from document1 to document2. Those will never be as good as the original steps, if you have access to those. For example, it won’t be able to detect “replace around” steps. It will all just be expressed in terms of replace, add mark and remove mark steps.

1 Like