Skip to content

Conversation

@ricokahler
Copy link
Contributor

@ricokahler ricokahler commented Jun 4, 2025

This PR is the first in a series of changes for the upcoming v6 release of @sanity/diff-patch. It introduces a comprehensive overhaul of the README.md to better reflect the library's capabilities, particularly its focus on generating conflict-resistant patches for collaborative editing environments, and to provide clearer, more developer-friendly documentation.

Key Changes to the README:

  • Updated Core Messaging & New "Objectives" Section:
    • The README now leads with the library's primary goal: generating Sanity patch mutations by comparing two documents/values, creating conflict-resistant patches designed for collaborative editing.
    • A new "Objectives" section clearly outlines the library's design principles:
      • Conflict-resistant patches
      • Performance (optimized for real-time, per-keystroke generation)
      • Intent preservation
      • Reliability
  • Comprehensive API Reference:
    • Updated documentation for diffPatch(source, target, options?), clarifying parameters (using source and target) and options.
    • Introduces and documents the new diffValue(source, target, basePath?) function, allowing patch generation for arbitrary values without a document wrapper. Includes examples for both.
  • New "Collaborative Editing Example":
    • A detailed example illustrates how diffPatch preserves user intent and minimizes conflicts in a scenario where two users simultaneously edit different aspects of the same document (one reorders paragraphs and fixes a typo, while the other improves text content). The example shows how both sets of changes can be successfully merged.
  • New "Technical Details" Section: This section provides deeper insights into:
    • String Diffing with diff-match-patch:
      • Explains the automatic selection criteria for using diff-match-patch vs. a simple set (document size limit, change ratio threshold, small document optimization, system key protection).
      • Includes performance rationale based on testing of the underlying @sanity/diff-match-patch library.
      • Migration from v5: Clearly states that the v5 lengthThresholdAbsolute and lengthThresholdRelative options for diffMatchPatch have been removed in v6 in favor of new, tested defaults that provide consistent performance.
    • Array Handling: Details how keyed arrays (using _key) and index-based arrays are diffed, and how undefined values in arrays are converted to null.
    • System Keys: Lists top-level document keys (_id, _type, _createdAt, _updatedAt, _rev) that are ignored during diffing.
    • Error Handling: Summarizes common error scenarios (missing document ID, immutable _type, multi-dimensional arrays, invalid revision).
  • Improved Structure and Readability: The entire README has been reorganized for better clarity, flow, and to serve as a more robust reference document.

Motivation:

The primary motivation for this overhaul is to prepare for the v6 release by:

  • Clearly communicating the library's strengths in collaborative editing.
  • Providing up-to-date and comprehensive API documentation for both existing and new functionalities.
  • Offering practical examples and technical details to help developers understand and effectively use the library.

This updated README aims to be a valuable resource for anyone using @sanity/diff-patch, especially as we move towards v6.

@ricokahler ricokahler marked this pull request as ready for review June 4, 2025 22:09
@ricokahler ricokahler requested a review from rexxars June 4, 2025 22:24
@ricokahler ricokahler force-pushed the v6_rename-to-source-target branch from 30d5dc1 to 9a6d1b2 Compare June 6, 2025 21:57
@ricokahler ricokahler force-pushed the v6_updated-readme branch from 9a3863d to a946a6a Compare June 6, 2025 21:57
@ricokahler ricokahler force-pushed the v6_rename-to-source-target branch from 9a6d1b2 to e226529 Compare June 6, 2025 22:00
@ricokahler ricokahler force-pushed the v6_updated-readme branch 2 times, most recently from c43b27c to 1dd9b82 Compare June 6, 2025 22:16
@ricokahler ricokahler force-pushed the v6_rename-to-source-target branch from e226529 to 5e84e17 Compare June 6, 2025 22:16
README.md Outdated
- If the target string is below 30 characters long, don't use DMP
- If the generated DMP is greater than 1.2 times larger than the target string, don't use DMP
- **Document size limit**: Documents larger than 1MB use `set` operations
- **Change ratio threshold**: If >40% of text changes, uses `set` (indicates replacement vs. editing)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Had to read the PR to figure this one out; this isn't about how much a string has changed, but about how much the difference in length the string has. Did you determine that this was significant enough to warrant this behavior? I'm curious on prepend/append operations (eg paste something new at the end) whether it would be preferable to use a DMP, if performance is reasonable enough.

Suggested change
- **Change ratio threshold**: If >40% of text changes, uses `set` (indicates replacement vs. editing)
- **Change ratio threshold**: If length of text changes >40%, uses `set` (indicates replacement vs. editing)

@ricokahler ricokahler force-pushed the v6_rename-to-source-target branch from 5e84e17 to 86994be Compare June 13, 2025 19:40
@ricokahler ricokahler force-pushed the v6_rename-to-source-target branch 2 times, most recently from f825413 to 3749d93 Compare June 13, 2025 19:55
@ricokahler ricokahler changed the base branch from v6_rename-to-source-target to graphite-base/45 June 13, 2025 19:57
…ting focus

- Add objectives section highlighting conflict-resistant patches and performance
- Add comprehensive API documentation for diffPatch() and diffValue()
- Add collaborative editing example showing 3-way merge scenarios
- Add technical details section covering diff-match-patch selection criteria
- Document performance rationale based on @sanity/diff-match-patch testing
- Remove v5 configuration options, add migration notes
- Restructure as developer-friendly reference documentation
@graphite-app graphite-app bot changed the base branch from graphite-base/45 to main June 13, 2025 19:57
removes the mislead "document size" terminology as this is wrong. strings are not the full document and that isn't considered
@ricokahler ricokahler merged commit bab2eb3 into main Jun 13, 2025
7 checks passed
Copy link
Contributor Author

Merge activity

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants