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 methods such as character-by-character, word-by-word, line-by-line, or sentence-by-sentence comparison. It can also generate a unified diff patch between the texts. This node is useful for scenarios like tracking changes in documents, comparing code snippets, or analyzing textual updates.

Practical examples:

  • Comparing two versions of a contract to highlight modifications.
  • Generating a patch file for source code changes.
  • Summarizing differences between two paragraphs or articles.

Properties

Name Meaning
Operation The type of comparison to perform: Character Diff, Create Patch, Line Diff, Sentence Diff, Word Diff
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 (available for Character Diff and Word Diff)
Ignore Whitespace Whether to ignore leading and trailing whitespace when comparing (available for Line Diff)
File Name (Original) Name assigned to the original file in the patch output (used only with Create Patch)
File Name (New) Name assigned to the new file in the patch output (used only with Create Patch)
Context Lines Number of context lines to include in the patch output (used only with Create Patch)
Output Format How to format the output for diff operations except Create Patch: Detailed Changes, Summary Only, Changes Only

Output

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

  • For diffChars, diffWords, diffLines, and diffSentences operations, the output includes:

    • operation: The chosen diff operation.
    • originalText and newText: The input texts.
    • summary: An object summarizing the diff with counts of added, removed, unchanged parts, and total changes.
    • Depending on the selected output format:
      • Detailed Changes: Full list of changes with metadata (changes), arrays of added parts (addedParts), and removed parts (removedParts).
      • Summary Only: Only the summary statistics.
      • Changes Only: Only the actual changed parts with their types and values (changedParts).
  • For the createPatch operation, the output includes:

    • patch: A unified diff patch string representing the differences.
    • originalFileName, newFileName, and contextLines: Metadata about the patch.

If an error occurs during processing, the output JSON will contain an error field with the error message.

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
    Occurs if both input texts are empty. Ensure at least one text input contains content.

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

  • Incorrect diff results when ignoring case or whitespace
    Confirm that the "Ignore Case" or "Ignore Whitespace" options are set appropriately for your use case.

  • If the node fails but "Continue On Fail" is enabled, errors will be included in the output JSON per item.

Links and References

  • diff npm package — The underlying library used for text comparison and patch generation.

Discussion