Overview
This node compares two text inputs and analyzes their differences using various methods. It is useful for scenarios such as:
- Tracking changes between document versions.
- Generating unified diff patches for code or text files.
- Highlighting specific differences at the character, word, sentence, or line level.
For example, you can use it to create a patch file showing changes between two source code versions or to get a detailed list of word-level differences between two paragraphs.
The "Create Patch" operation specifically generates a unified diff patch string that can be used with version control systems or patch utilities.
Properties
| Name | Meaning |
|---|---|
| Original Text | The original text to compare from. |
| New Text | The new text to compare to. |
| File Name (Original) | Name for the original file in the patch (default: "original.txt"). Used only in Create Patch operation. |
| File Name (New) | Name for the new file in the patch (default: "new.txt"). Used only in Create Patch operation. |
| Context Lines | Number of context lines to include in the patch (default: 3). Used only in Create Patch operation. |
| 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. |
Output
The output JSON structure depends on the selected operation and output format.
For the Create Patch operation, the output includes:
patch: A string containing 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.
Example snippet of output JSON for Create Patch:
{
"patch": "@@ -1,3 +1,4 @@\n ...",
"originalFileName": "original.txt",
"newFileName": "new.txt",
"contextLines": 3
}
For other diff operations (character, word, line, sentence), the output varies based on the chosen output format:
- Detailed Changes: Includes full diff array with added, removed, and unchanged parts plus summary counts.
- Summary Only: Contains summary statistics like total changes, counts of added, removed, and unchanged parts.
- Changes Only: Lists only the changed parts with type ("added" or "removed"), value, and count.
Example fields in detailed output:
changes: Array of diff parts with metadata.addedParts: Array of strings representing added segments.removedParts: Array of strings representing removed segments.summary: Object with counts of changes.
Dependencies
- Uses the external library
difffor computing text differences and generating patches. - Requires no special API keys or external services.
- No additional environment variables or n8n credentials are needed.
Troubleshooting
Error: Both original and new text cannot be empty
This error occurs if both input texts are empty. Ensure at least one of the texts contains content.Invalid input types
Inputs must be strings. Non-string inputs may cause unexpected errors.Patch generation issues
If the patch output looks incorrect, verify that the file names and context lines parameters are set appropriately.Large text inputs
Very large texts might impact performance or memory usage.
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.