Line-level diff of two text blocks separated by ---
Aa TextCompare two text blocks line by line
$ echo "the quick brown fox
jumps over
the lazy dog
---
the quick re..." | diff (input) => {
const sep = input.indexOf("\n---\n");
if (sep === -1)
return "Error: separate two text blocks with a line containing only ---";
const oldText = input.slice(0, sep);
const newText = input.slice(sep + 5);
const changes = diffLines(oldText, newText);
const result = [];
for (const part of changes) {
const prefix = part.added ? "+ " : part.removed ? "- " : " ";
// diffLines includes trailing newlines in each part; split and mark each line
const lines = part.value.replace(/\n$/, "").split("\n");
for (const line of lines) result.push(prefix + line);
}
return result.join("\n");
}