Show deletions and insertions in the same paragraph. Try before.txt and after.txt from the zip.
dotnet add package RecursiveAI.OneDiff.DocumentsRequires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.
using OneDiff.Documents; var comparison = Comparison.Of( "before.txt", "after.txt"); comparison.SaveAs("redline.html", RedlineForm.Html); Console.WriteLine("Saved redline.html");
Unzip the example, then dotnet run.
Writes a self-contained redline.html and prints:
Saved redline.htmlFull program
The zip contains these two inputs, each ending with a newline:
before.txt | after.txt |
|---|---|
| Please submit the draft on Monday. | Please submit the draft on Friday. |
The program writes both words inline, then prints the comparison change ID and the opening tags of the corresponding marks in this sample.
using OneDiff.Documents; using System.Text.RegularExpressions; var comparison = Comparison.Of("before.txt", "after.txt"); comparison.SaveAs("redline.html", RedlineForm.Html); Console.WriteLine("Saved redline.html"); foreach (var change in comparison.Changes) Console.WriteLine($"Change {change.Id}"); var html = File.ReadAllText("redline.html"); foreach (Match tag in Regex.Matches(html, "<(ins|del) data-change=\"([0-9]+)\"")) Console.WriteLine($"<{tag.Groups[1]} data-change=\"{tag.Groups[2]}\">");
Saved redline.html Change 0 <del data-change="0"> <ins data-change="0">
Open redline.html in a browser: “Monday” is struck through and “Friday” is underlined. In the element inspector, data-change="0" links both marks back to comparison.Changes by ID. The program’s regular expression only inspects this example’s ins and del tags; it isn’t a parser for every kind of change.
If an input path doesn’t exist, Comparison.Of throws before the program calls SaveAs; use the paths to your two input files in old-to-new order.
Variations
Replace the SaveAs call in the snippet at the top:
Use highlights and +/- marks while keeping the inline layout. Writes review.html; update the filename in Console.WriteLine to match.
comparison.SaveAs("review.html", RedlineForm.Html, RedlineStyle.Developer);
Save the comparison data alongside the HTML. Writes both redline.html and changes.json.
comparison.SaveAs("redline.html", RedlineForm.Html); comparison.SaveAs("changes.json");
Related recipes
- Render a text comparison as a PDF redline
- Create a side-by-side HTML line diff
- Export a document comparison to JSON
The short answer
Use Comparison.Of to compare the old and new documents and SaveAs with RedlineForm.Html to write one HTML file. The inline layout follows the revised document’s structure and marks removed text where it belonged. The report includes its CSS and doesn’t run scripts or fetch assets, so you can open it in a browser offline. Match a mark’s data-change to Change.Id to link a visible edit back to the comparison that produced it.
How do you show inserted and deleted text in HTML?
HTML has an ins element for added text and a del element for removed text. OneDiff uses both: a replacement can produce a del and an ins with the same ID, as the weekday example shows. Leave unchanged words unmarked. They give the reviewer a place to start reading without looking up the original paragraph.
Those two elements don’t cover every kind of edit. The inline format uses separate markup for formatting changes and for moves. A move has an old-place and a new-place marker, with in-page links between mvf-N and mvt-N, where N is the change ID. In contrast, a data-change attribute is a reference to the comparison object, not itself a browser fragment ID. The “Differences not shown” section summarizes changes that the comparison settings leave out of the marked text.
What’s the difference between an inline diff and a side-by-side diff?
The unit of review here is a document block, such as a paragraph or list item, not a pair of physical lines in a text file. A changed word can appear with the rest of its paragraph, and a moved paragraph can link its two locations. The containing block has data-new and data-old references for its revised and original nodes; data-change on the edit tells you which change you are looking at. A side-by-side line diff puts the file lines in separate columns. Use that view when line position is the thing you care about. Use a document redline when the reader needs to follow edits in reading order, and keep the JSON alongside the HTML when another program needs the comparison data rather than the display.