@mdxld/diff
Git-style diffing, patching, and 3-way merge for text and structured data
@mdxld/diff
Git-style text diffing, unified patches, 3-way merge, and structured object comparison. Essential for version control in MDX editors and collaborative editing.
Installation
Quick Start
Text Diffing
diffLines(oldStr, newStr)
Diff two strings line by line. Most common for documents.
diffWords(oldStr, newStr)
Diff word by word. Useful for inline changes.
diffChars(oldStr, newStr)
Character-level diff. Fine-grained comparison.
Options
Git-Style Patches
createPatch(fileName, oldStr, newStr)
Create a unified diff patch (like git diff).
Output:
Patch Options
applyPatch(source, patch)
Apply a patch to source content.
parsePatch(patchString)
Parse a patch string into structured data.
structuredPatch(oldFile, newFile, oldStr, newStr)
Get structured patch data for programmatic access.
reversePatch(patch)
Create a patch that undoes the original.
3-Way Merge
merge3way(base, ours, theirs)
Perform a 3-way merge like git merge.
Conflict Markers
When conflicts occur, the merged content contains markers:
resolveConflict(content, resolution)
Resolve all conflicts by choosing a side.
Object Diffing
diffObjects(oldObj, newObj)
Get structured changes between objects.
diffJSON(oldObj, newObj)
Create a unified diff of JSON representations.
diffArrays(oldArr, newArr)
Track additions and removals in arrays.
Utilities
formatChanges(changes)
Convert changes to a human-readable string.
countChanges(changes)
Count additions and deletions.
canApplyPatch(source, patch)
Check if a patch can be applied cleanly.
getPatchStats(patch)
Get statistics from a patch.
Use Cases
Version History for MDX Editor
Collaborative Editing
Config File Comparison
Database Sync
@mdxld/extract re-exports these as diff, applyExtract and mergeExtract for the
template-based round trip; @mdxld/markdown exports no diff of its own.
Related Packages
| Package | Description |
|---|---|
@mdxld/markdown | Markdown conversion with diff support |
@mdxld/extract | Template-based extraction |
mdxdb | Document storage |