Overview
The "Text Diff" node compares two text inputs and analyzes their differences. It supports multiple granularities of comparison, including character-by-character, word-by-word, line-by-line, and sentence-by-sentence diffs, as well as generating unified diff patches. This node is useful for scenarios such as tracking changes in documents, code reviews, content updates, or any situation where understanding textual differences is important.
For example, you can use it to:
- Compare two versions of a contract to highlight modifications.
- Detect changes between two code snippets at the word or line level.
- Generate a patch file representing differences between two text files.
Properties
| Name | Meaning |
|---|---|
| Original Text | The original text to compare from. |
| New Text | The new text to compare to. |
| Output Format | How to format the output. Options: - Detailed Changes: Detailed list of changes with metadata. - Summary Only: Only summary statistics. - Changes Only: Only the actual changes. |
| Operation | The type of diff operation to perform. Options: - Character Diff: Compare texts character by character. - Create Patch: Create a unified diff patch. - Line Diff: Compare texts line by line. - Sentence Diff: Compare texts sentence by sentence. - Word Diff: Compare texts word by word. |
| Ignore Case | Whether to ignore case differences when comparing (only for Character and Word Diff). |
| Ignore Whitespace | Whether to ignore leading and trailing whitespace (only for Line Diff). |
| File Name (Original) | Name for the original file in the patch (only for Create Patch operation). |
| File Name (New) | Name for the new file in the patch (only for Create Patch operation). |
| Context Lines | Number of context lines to include in the patch (only for Create Patch operation). |
Output
The node outputs JSON data containing the results of the diff operation. The structure depends on the selected operation and output format:
For diff operations (character, word, line, sentence):
operation: The chosen diff operation.originalText: The original input text.newText: The new input text.summary: An object summarizing the diff:totalChanges: Total number of change segments.addedCount: Count of added elements.removedCount: Count of removed elements.unchangedCount: Count of unchanged elements.hasChanges: Boolean indicating if there are any changes.
- Depending on
Output Format:detailed: Includeschanges(full diff array),addedParts(array of added text segments), andremovedParts(array of removed text segments).summary: Only thesummaryobject.changesOnly: IncludeschangedParts, an array of objects each describing a change withtype("added" or "removed"),value, andcount.
For Create Patch operation:
patch: A string containing the unified diff patch.originalFileName: Name of the original file used in the patch.newFileName: Name of the new file used in the patch.contextLines: Number of context lines included in the patch.
The node does not output binary data.
Dependencies
- Uses the external library
difffor computing text differences. - Requires no special API keys or external services.
- No specific environment variables or n8n configurations are needed beyond standard node setup.
Troubleshooting
- Empty Inputs: If both original and new texts are empty, the node throws an error stating that both cannot be empty. Ensure at least one input has content.
- Invalid Operation: Selecting an unsupported operation will cause failure; ensure the operation parameter matches one of the supported options.
- Large Texts: Very large inputs might impact performance or memory usage.
- Output Format Mismatch: The "Output Format" property is hidden for the "Create Patch" operation; setting it for other operations only.
If errors occur, check the error message returned in the output JSON under the error field (if "Continue On Fail" is enabled).
Links and References
- diff npm package – The underlying library used for text difference calculations.
- Unified Diff Format – Explanation of the patch format generated by the "Create Patch" operation.