diffWithHtml

tcmts.share.diffWithHtml · #7ribmmr2 · term · runs anywhere

Two versions' lines as a line diff, each line with its HTML: a removed line's as it was, an added or unchanged line's as it is now.

import { lineDiff } from "#codebase/tcmts/cli/diff";

export function diffWithHtml(
  a: { lines: string[]; html: string[] },
  b: { lines: string[]; html: string[] },
): { lines: [" " | "+" | "-", string][]; html: string[] } {
  const lines = lineDiff(a.lines, b.lines);
  let i = 0;
  let j = 0;
  const html = lines.map(
    ([op]) => (op === "+" ? b.html[j++] : op === "-" ? a.html[i++] : (i++, b.html[j++])) ?? "",
  );
  return { lines, html };
}

uses

used by