Text Diff

Compare and analyze differences between two text inputs

Overview

The "Text Diff" node compares two text inputs and analyzes their differences using various granularities: characters, words, lines, or sentences. It can also generate a unified diff patch between the texts. This node is useful for tracking changes in documents, code snippets, or any textual data where you want to identify additions, deletions, or modifications.

Common scenarios include:

  • Comparing versions of source code or configuration files.
  • Detecting changes in user-submitted content or documents.
  • Generating summaries of textual differences for reporting or auditing.

For example, you might use this node to compare an original article with its edited version to highlight what was added or removed, or to create a patch file representing changes between two code files.

Properties

Name Meaning
Operation The type of comparison to perform:
- Character Diff (compare character by character)
- Create Patch (generate unified diff patch)
- Line Diff (compare line by line)
- Sentence Diff (compare sentence by sentence)
- Word Diff (compare word by word)
Original Text The original text input to compare from.
New Text The new text input to compare to.
Ignore Case Whether to ignore case differences when comparing (only for Character Diff and Word Diff).
Ignore Whitespace Whether to ignore leading and trailing whitespace differences (only for Line Diff).
File Name (Original) Name to assign to the original file in the generated patch (only for Create Patch operation).
File Name (New) Name to assign to the new file in the generated patch (only for Create Patch operation).
Context Lines Number of context lines to include around changes in the patch (only for Create Patch operation).
Output Format How to format the output (not applicable for Create Patch):
- Detailed Changes (list with metadata)
- Summary Only (statistics summary)
- Changes Only (only actual changes)

Output

The node outputs JSON data containing the results of the diff operation:

  • For Character, Word, Line, and Sentence diffs:

    • operation: The selected 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 the chosen output format:
      • changes: Full detailed list of changes with metadata (if "Detailed Changes").
      • addedParts: Array of added text parts (if "Detailed Changes").
      • removedParts: Array of removed text parts (if "Detailed Changes").
      • changedParts: Array of only changed parts with type and value (if "Changes Only").
  • 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 diff library to perform text comparisons and generate patches.
  • Requires no special API keys or external services.
  • No additional environment variables or n8n configurations are needed beyond standard node setup.

Troubleshooting

  • Error: Both original and new text cannot be empty
    This error occurs if both input texts are empty strings. Ensure at least one of the texts contains content before running the node.

  • Empty or unexpected output
    Verify that the input texts are correctly provided and that the selected operation matches your intended comparison granularity.

  • Incorrect diff results when ignoring case or whitespace
    Make sure the "Ignore Case" and "Ignore Whitespace" options are set appropriately for the chosen operation, as they only apply to certain diff types.

  • Patch generation issues
    When using the "Create Patch" operation, ensure valid file names and context lines are provided. Invalid values may cause malformed patches.

Links and References

Discussion