Compare documents
Compare two documents in real time and show the diff in the editor.
Works in non-AI use cases
This feature does not need AI to work. It can compare any two documents, regardless of whether they are AI-generated or not. A common use case is to compare documents before and after an AI edits them. See the Review changes guide.
For a step-by-step guide on comparing documents, see the Compare documents guide.
API Reference
startComparingDocuments
Starts real-time comparison with another document. Differences are displayed as suggestions in an inline diff view where the old and the new content are displayed in the editor.
Parameters (StartComparingDocumentsOptions)
otherDoc?(Node): The document to compare against. It is a ProseMirror Node object. Defaults to a snapshot of the current document if omitteddisplayOptions?(DisplayOptions<{ suggestion: Suggestion }>): Controls how differences are rendered as suggestionsshowAsDiff?(boolean): Show alternative content as a diff widget. Default:truein comparison flowsdiffPosition?('before' | 'after'): Where to display the diff relative to the suggestion. Default:'before'attributes?(Record<string, any>): Attributes added to rendered elementsrenderDecorations?(RenderDecorations<{ suggestion: Suggestion }>): Custom rendering function
debounceTimeout?(number): Milliseconds to wait before recomputing diffs. A low value will make the app seem more responsive, but it might cause performance issues in large documents. Default:500diffUtilityConfig?(DiffUtilityConfig): Options for comparing the documents with the diff utilitysimplifyChanges?(boolean): Whether to simplify the changes. Iftrue, the changes will be simplified so that, for example, two changes in the same word are merged into a single change. Only applies when mode is 'detailed'. Default:trueignoreAttributes?(string[]): The attributes to ignore. Default:['id', 'data-thread-id']ignoreMarks?(string[]): The marks to ignore. Default:['inlineThread']changeMergeDistance?(number | null): Maximum distance (in document positions) between changes to merge them. If two adjacent changes have a distance less than or equal to this value in either rangeA or rangeB, they will be merged into a single change. Set tonullorundefinedto disable merging. Only applies when mode is 'detailed'. Default:nullmode?('detailed' | 'block'): The diff mode to use for comparison.'detailed'performs character-level diff that identifies precise changes within text (default).'block'performs block-level diff that treats each top-level node as a single unit. Block mode is useful when you only need to know which paragraphs, headings, or other block-level elements have changed, without character-level detail. In block mode,simplifyChangesandchangeMergeDistanceoptions are ignored. Default:'detailed'
Returns
void
Example
// Start comparing changes with another document
toolkit.startComparingDocuments({
otherDoc,
})stopComparingDocuments
Stops the real-time comparison and clears all suggestions, so the diff view is no longer visible.
Returns
void
Example
// Stop comparison and clear decorations
toolkit.stopComparingDocuments()Accepting and rejecting changes
When comparing documents, changes are displayed as suggestions. To accept or reject individual changes or all changes at once, use the suggestion methods:
acceptSuggestion: Accept a specific changerejectSuggestion: Reject a specific changeacceptAllSuggestions: Accept all changesrejectAllSuggestions: Reject all changes
These methods work with both regular suggestions and changes from the Compare Documents feature.
Example
// Start comparing documents
toolkit.startComparingDocuments({
otherDoc: otherDoc,
})
// Get all changes (as suggestions)
const suggestions = toolkit.getSuggestions()
// Accept the first change and get feedback
if (suggestions.length > 0) {
const result = toolkit.acceptSuggestion(suggestions[0].id)
console.log(result.aiFeedback.events)
}
// Or reject all changes at once
const result = toolkit.rejectAllSuggestions()
console.log(result.aiFeedback.events)
toolkit.stopComparingDocuments()