Skip to content

feat: replace custom LCS word-diff with @sanity/diff-match-patch#348

Merged
tomasz-tomczyk merged 1 commit intomainfrom
migrate-to-sanity-diff-match-patch
Apr 25, 2026
Merged

feat: replace custom LCS word-diff with @sanity/diff-match-patch#348
tomasz-tomczyk merged 1 commit intomainfrom
migrate-to-sanity-diff-match-patch

Conversation

@tomasz-tomczyk
Copy link
Copy Markdown
Owner

Summary

  • Replaces the custom tokenize + computeTokenLCS (O(m×n) DP) word-diff implementation with @sanity/diff-match-patch
  • Uses makeDiff + cleanupSemantic for word-boundary-aligned highlights
  • Since the package is ESM-only, copy-deps.js uses esbuild to bundle it into a browser-ready IIFE (8.9KB)
  • applyWordDiffToHtml and all guard clauses unchanged — return type { oldRanges, newRanges } preserved

Why: Google's diff-match-patch is archived. @sanity/diff-match-patch is a maintained TypeScript rewrite (~492K weekly npm downloads) that merges bugfixes Google never accepted.

Companion PR: tomasz-tomczyk/crit-web#101 (pending)

Test plan

  • E2E word-diff.spec.ts passes
  • E2E suggestion-diff.spec.ts passes
  • Visual check: word-level highlights in diff view look correct (whole-word, not character-level)
  • Visual check: suggestion diffs render correctly

🤖 Generated with Claude Code

The custom tokenize+computeTokenLCS implementation used O(m*n) dynamic
programming on word tokens. Replace it with @sanity/diff-match-patch's
diff algorithm + semantic cleanup, which produces better word-boundary
highlights with less code. The return type ({ oldRanges, newRanges })
and all guard clauses are preserved.

Since the package is ESM-only, copy-deps.js now uses esbuild to bundle
it into a browser-ready IIFE (8.9KB) at frontend/diff-match-patch.min.js.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@tomasz-tomczyk tomasz-tomczyk force-pushed the migrate-to-sanity-diff-match-patch branch from 36c4ef2 to b93d612 Compare April 25, 2026 19:43
@codecov
Copy link
Copy Markdown

codecov Bot commented Apr 25, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 66.90%. Comparing base (3361f14) to head (b93d612).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #348      +/-   ##
==========================================
+ Coverage   66.86%   66.90%   +0.03%     
==========================================
  Files          18       18              
  Lines        7807     7807              
==========================================
+ Hits         5220     5223       +3     
+ Misses       2194     2192       -2     
+ Partials      393      392       -1     
Flag Coverage Δ
e2e 35.01% <ø> (+0.12%) ⬆️
unit 62.72% <ø> (+0.03%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@tomasz-tomczyk tomasz-tomczyk changed the title Replace custom LCS word-diff with @sanity/diff-match-patch feat: replace custom LCS word-diff with @sanity/diff-match-patch Apr 25, 2026
@tomasz-tomczyk tomasz-tomczyk merged commit 580c214 into main Apr 25, 2026
7 checks passed
@tomasz-tomczyk tomasz-tomczyk deleted the migrate-to-sanity-diff-match-patch branch April 25, 2026 19:53
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.

1 participant