Overview
The Text Diff node compares two text inputs and analyzes their differences. It supports multiple comparison operations such as character-by-character, word-by-word, line-by-line, sentence-by-sentence diffs, and also creating unified diff patches. This node is useful for scenarios like:
- Comparing versions of documents or code snippets to identify changes.
- Generating summaries of textual differences for review workflows.
- Creating patch files for source control or deployment processes.
For example, you can use it to compare an original article with its edited version to highlight added or removed sentences, or generate a patch file showing line-level changes between two code files.
Properties
| Name | Meaning |
|---|---|
| Original Text | The original text to compare from. |
| New Text | The new text to compare to. |
| Ignore Whitespace | Whether to ignore leading and trailing whitespace when comparing lines (only for Line Diff). |
| Output Format | How to format the output differences: • 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: • 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 Diff and Word Diff). |
| File Name (Original) | Name for the original file in the patch (only for Create Patch). |
| File Name (New) | Name for the new file in the patch (only for Create Patch). |
| Context Lines | Number of context lines to include in the patch (only for Create Patch). |
Output
The node outputs JSON data containing the diff results structured according to the selected operation and output format:
For detailed output formats, the output includes:
changes: An array of change objects describing each part of the diff with metadata such as type (added,removed, or unchanged), value, and count.addedParts: Array of strings representing all added parts.removedParts: Array of strings representing all removed parts.summary: Statistics including total changes, counts of added, removed, and unchanged parts, and a boolean indicating if there are any changes.
For summary only, only the
summaryobject is included.For changes only, an array of changed parts with their types and values is provided.
When using the Create Patch operation, the output contains:
patch: A string representing the unified diff patch.originalFileName: The name of the original file used in the patch.newFileName: The 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
difflibrary to perform text comparisons and generate patches. - Requires no special API keys or external services.
- No additional environment variables or n8n configurations are necessary 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 Parameter: Selecting an unsupported operation may cause unexpected behavior; use only the provided options.
- Large Texts: Very large text inputs might impact performance or memory usage.
- Output Format Mismatch: Some output formats are not available for certain operations (e.g., "Output Format" is hidden for "Create Patch"). Make sure to select compatible options.
If errors occur, check the error message returned in the output JSON under the error field when "Continue On Fail" is enabled.
Links and References
- diff npm package — The underlying library used for text comparison and patch creation.
- Unified Diff Format — Explanation of the patch format generated by the Create Patch operation.