Skip to main content

diff_markdown

Function diff_markdown 

Source
pub fn diff_markdown(old: &str, new: &str) -> String
Expand description

The two text snapshots, old and new, as one Markdown document whose CriticMarkup holds the changes: accepting them all gives new, rejecting them all gives old. Critically, the block markers (heading #, list -, quote >, table |, footnote [^…]:, …) stay outside the changes on lines both versions share; when the marker itself changed — a renumbered item, # becoming ##, a task box ticked — or a whole block came and went, the block joins the change so the marked-up document rebuilds either version exactly and the Word writer reads it as a block changed whole.

§How changes are scoped

Lines are aligned first, then the words within the lines that pair up. On lines both versions share, the marker stays before the change (- {~~old~>new~~}); a marker that itself changed takes the whole block with it — both versions of the line in one substitution — and a block one version alone has goes into one change with its marker and its line breaks, so accepting and rejecting rebuild the documents exactly and markdown_to_docx writes the change as Word structure: a deleted heading is a deleted Heading paragraph. A line added or removed inside a paragraph takes the line break with it (first line{++\nnew line++}). Numbers ($5,000,000, 04/20/26, 3.5%), emphasis and strikethrough delimiters, and link syntax are read whole; a code span, an image, an equation, an autolink and an HTML comment are one atom, so a change that touches any of them covers all of it (*Buyer* → Buyer is one change, not two). CriticMarkup delimiters already in the text are escaped — outside what the reader reads verbatim — so they stay text.

§Inputs

  • old, new — line-oriented Markdown (\n line ends, no carriage returns — CRLF is normalized to LF).

§Examples

use jubarte_markdown::markdown::diff_markdown;

assert_eq!(
    diff_markdown("Payment in 30 days.\n", "Payment in 45 days.\n"),
    "Payment in {~~30~>45~~} days.\n"
);