jubarte-edit 0.20.0

The edit product of the jubarte engine: edit plans as tracked changes, text diff views, capabilities and staged publication
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
// SPDX-FileCopyrightText: 2026 Jandira Technologies, LLC
//
// SPDX-License-Identifier: Apache-2.0

//! A Markdown edit of a Word document, applied to the document itself.
//!
//! The Markdown is read as the document's text after an edit, however it
//! was made (pandoc, `jubarte text`, a converter, an agent). Its blocks
//! are aligned with the body's paragraphs by their text, and only what
//! differs is edited, through an [edit plan](crate::edit): a paragraph
//! whose text changed is rewritten word by word, so its runs,
//! formatting, fields and properties stay; a block the document lacks
//! becomes a paragraph next to its neighbours, taking the properties of
//! a neighbour of its kind; a paragraph the Markdown lacks is deleted.
//! Empty paragraphs, which Markdown cannot hold, are left alone, and
//! typed numbering (`1.`, `(a)`) that the Markdown writes as a list
//! marker is kept.
//!
//! Backs `jubarte edit FILE EDITIONS.md`, the `PATCH.md` edit-plan
//! companion path (a Markdown sketch of the same edits), and the
//! Python / WASM [`apply_markdown`] binding.

use std::collections::HashSet;

use pulldown_cmark::{Event, HeadingLevel, Options, Parser, Tag, TagEnd};
use similar::{Algorithm, DiffTag, capture_diff_slices};

use super::MarkdownError;
use super::diff::similarity;
use crate::edit::{
    EditPlan, ExistingRevisions, Operation, OperationKind, RunSpec, Selector, Side, apply_plan,
};

/// Paragraphs that share less than this share of their words pair only when
/// nothing better is left between two pairs.
const PAIR_SIMILARITY: f64 = 0.3;

/// Pairing compares every paragraph of a changed stretch with every block
/// of it; above this many comparisons they pair by position.
const MAX_PAIRINGS: usize = 2_500;

/// A Word document the Markdown edits landed on.
///
/// Returned by [`apply_markdown`]; writes a clean `.docx` (no tracked
/// changes) where the Markdown rewrote, inserted or deleted body
/// paragraphs, with anything the document could not take reported in
/// [`Self::warnings`].
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Patched {
    /// The edited `.docx`, without tracked changes.
    pub docx: Vec<u8>,
    /// What the Markdown changed that the document could not take: a
    /// footnote whose text is on the Markdown side but not applied, a
    /// table cell that would have needed row and column work the engine
    /// does not attempt, a paragraph the engine could not edit for the
    /// reason the first failure gave.
    pub warnings: Vec<String>,
}

/// The kind of a block or paragraph, for pairing and for the properties a
/// new paragraph copies.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum Kind {
    Heading(u8),
    List,
    Cell,
    Plain,
}

/// A Markdown block: its kind, its plain text and its runs.
#[derive(Clone, Debug)]
struct Block {
    kind: Kind,
    runs: Vec<RunSpec>,
}

impl Block {
    fn text(&self) -> String {
        self.runs.iter().map(|r| r.text.as_str()).collect()
    }
}

/// A body paragraph: its index in the body, text and kind.
#[derive(Clone, Debug)]
struct Paragraph {
    index: usize,
    text: String,
    kind: Kind,
}

/// Edits `docx` so its body reads as `markdown`, keeping the
/// paragraphs' own runs, formatting, fields and properties, and
/// reporting what the document could not take in
/// [`Patched::warnings`]. A document with tracked changes is read with
/// them accepted; the result is a clean `.docx`.
///
/// Backs `jubarte edit FILE EDITIONS.md` and the Python / WASM
/// `apply_markdown` binding.
///
/// # Errors
///
/// [`MarkdownError::Package`] when:
/// - the source fails to admit, list its tracked changes, or list its
///   body paragraphs (covered by `InspectError`);
/// - the edit-plan engine refuses an edit the engine cannot represent
///   after four retries at falling back to whole-paragraph replacements
///   and dropping the failing edits.
///
/// Footnote text, which the body-only editor does not apply, is a warning
/// in [`Patched::warnings`], not an error.
///
/// ```
/// use jubarte_edit::markdown::{DocxOptions, apply_markdown, markdown_to_docx};
///
/// let original = markdown_to_docx("Pay in 30 days.\n\nSigned.\n", &DocxOptions::default())
///     .unwrap()
///     .docx;
/// let edited = apply_markdown(&original, "Pay in 45 days.\n\nSigned.\n").unwrap();
/// assert!(edited.warnings.is_empty());
/// ```
pub fn apply_markdown(docx: &[u8], markdown: &str) -> Result<Patched, MarkdownError> {
    apply_markdown_exact(docx, markdown, false)
}

/// Edits `docx` so its body reads as `markdown`, and returns an error
/// with no document when any edit cannot be applied as written.
/// A refused rewrite stays refused: it is not replaced by a whole
/// paragraph, and it is not left out of an otherwise applied result.
///
/// Footnote text is outside the body-only editor in both paths, so it
/// stays the warning [`apply_markdown`] gives, not a refusal.
///
/// # Errors
///
/// [`MarkdownError::Package`] when the source fails to admit or list its
/// paragraphs, when the edit-plan engine refuses any edit, or when the
/// Markdown adds a table row or cell, which the editor cannot add.
pub fn apply_markdown_strict(docx: &[u8], markdown: &str) -> Result<Patched, MarkdownError> {
    apply_markdown_exact(docx, markdown, true)
}

fn apply_markdown_exact(
    docx: &[u8],
    markdown: &str,
    strict: bool,
) -> Result<Patched, MarkdownError> {
    let invalid = |e: &dyn std::fmt::Display| MarkdownError::Package(e.to_string());
    let summary = crate::inspect::summary(docx).map_err(|e| invalid(&e))?;
    // A plan that accepts existing revisions addresses the accepted text.
    let base = if summary.revisions > 0 {
        crate::document_comparer::accept_revisions(docx).map_err(|e| invalid(&e))?
    } else {
        docx.to_vec()
    };
    let paragraphs: Vec<Paragraph> = crate::inspect::paragraphs(&base)
        .map_err(|e| invalid(&e))?
        .into_iter()
        .filter(|p| !p.text.trim().is_empty())
        .map(|p| Paragraph {
            index: p.index,
            kind: match p.style.as_deref().and_then(heading_level) {
                Some(level) => Kind::Heading(level),
                None if p.in_table => Kind::Cell,
                None if p.numbered => Kind::List,
                None => Kind::Plain,
            },
            text: p.text,
        })
        .collect();
    let (blocks, notes) = blocks(markdown);
    let mut warnings = Vec::new();
    if notes {
        warnings.push("footnote text is not applied: only the body is edited".to_string());
    }
    let edits = align(&paragraphs, &blocks);
    let styles = paragraph_styles(&base);
    for edit in &edits {
        if let Edit::Insert(b, _) = edit
            && blocks[*b].kind == Kind::Cell
        {
            let left_out = format!(
                "table cell {:?} not applied: rows and columns are not added",
                blocks[*b].text()
            );
            // A strict patch leaves nothing out: the row is the refusal.
            if strict {
                return Err(MarkdownError::Package(left_out));
            }
            warnings.push(left_out);
        }
    }

    // Rewrites the engine refuses (text inside a link or a field) become
    // whole-paragraph replacements, and what still fails is left out.
    // The strict path does neither: the first refusal is the result.
    let mut whole: HashSet<usize> = HashSet::new();
    let mut dropped: HashSet<usize> = HashSet::new();
    let attempts = if strict { 1 } else { 4 };
    for _ in 0..attempts {
        let operations = operations(&paragraphs, &blocks, &edits, &styles, &whole, &dropped);
        if operations.is_empty() {
            return Ok(Patched {
                docx: base,
                warnings,
            });
        }
        let plan = EditPlan {
            schema_version: crate::inspect::SCHEMA_VERSION,
            source_sha256: None,
            author: "jubarte".to_string(),
            date: None,
            initials: None,
            removed_anchor_comments: Default::default(),
            existing_revisions: ExistingRevisions::Accept,
            resolve_revisions: None,
            operations,
            update_fields: false,
            rewrite_others_comments: false,
            others: crate::edit::Others::Allow,
        };
        match apply_plan(docx, &plan) {
            Ok(result) => {
                return Ok(Patched {
                    docx: result.clean,
                    warnings,
                });
            }
            Err(error) => {
                if strict {
                    return Err(MarkdownError::Package(error.to_string()));
                }
                // Each failed edit once, with the first reason given.
                let failed: std::collections::BTreeMap<usize, String> = error
                    .outcomes
                    .iter()
                    .filter(|o| o.status == "failed")
                    .chain(
                        // A conflict names its operation but leaves it "ok".
                        error
                            .operation
                            .iter()
                            .filter_map(|id| error.outcomes.iter().find(|o| &o.id == id)),
                    )
                    .filter_map(|o| {
                        let entry = o.id.split(':').nth(1)?.parse::<usize>().ok()?;
                        let why = o.message.clone().unwrap_or_else(|| error.message.clone());
                        Some((entry, why))
                    })
                    .rev()
                    .collect();
                if failed.is_empty() {
                    return Err(MarkdownError::Package(error.to_string()));
                }
                for (entry, why) in failed {
                    let rewrite = matches!(edits[entry], Edit::Pair(..));
                    if rewrite && whole.insert(entry) {
                        continue;
                    }
                    if dropped.insert(entry) {
                        warnings.push(format!(
                            "{} not applied: {why}",
                            describe(&edits[entry], &paragraphs, &blocks)
                        ));
                    }
                }
            }
        }
    }
    Err(MarkdownError::Package(
        "the Markdown's changes could not be applied".to_string(),
    ))
}

/// The paragraph styles a document defines, by lowercase name.
fn paragraph_styles(docx: &[u8]) -> HashSet<String> {
    let Ok(package) = crate::opc::PartFs::open(docx) else {
        return HashSet::new();
    };
    let Some(xml) = package
        .main_document_part()
        .and_then(|main| {
            let rels = package.read_rels_for(&main)?;
            let rel = rels
                .items
                .iter()
                .find(|r| r.rel_type.ends_with("/styles"))?;
            Some(package.resolve_rel_target(&main, &rel.target))
        })
        .and_then(|part| package.part_string(&part))
    else {
        return HashSet::new();
    };
    let mut dom = crate::xmllinq::Dom::new();
    let document = dom.parse_xdocument(&xml);
    let Some(root) = dom.root(document) else {
        return HashSet::new();
    };
    use crate::namespaces::W;
    dom.elements(root, Some(&W::name("style")))
        .into_iter()
        // No `w:type` is a paragraph style (ECMA-376 `w:style`).
        .filter(|&style| {
            dom.attribute(style, &W::type_())
                .is_none_or(|t| t == "paragraph")
        })
        .filter_map(|style| {
            let name = dom.element(style, &W::name("name"))?;
            Some(dom.attribute(name, &W::val())?.to_lowercase())
        })
        .collect()
}

/// One step of the alignment of paragraphs (by position in `paragraphs`)
/// and blocks (by position in `blocks`).
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum Edit {
    /// A paragraph and the block it became; rewritten when they differ.
    Pair(usize, usize),
    /// A block the document lacks, inserted after paragraph `after` (or
    /// before the first when `None`).
    Insert(usize, Option<usize>),
    /// A paragraph the Markdown lacks.
    Delete(usize),
}

fn describe(edit: &Edit, paragraphs: &[Paragraph], blocks: &[Block]) -> String {
    let excerpt = |text: &str| {
        let short: String = text.chars().take(40).collect();
        if short.len() < text.len() {
            format!("\"{short}…\"")
        } else {
            format!("\"{short}\"")
        }
    };
    match *edit {
        Edit::Pair(p, b) => format!(
            "rewriting paragraph {} as {}",
            paragraphs[p].index,
            excerpt(&blocks[b].text())
        ),
        Edit::Insert(b, _) => format!("inserting {}", excerpt(&blocks[b].text())),
        Edit::Delete(p) => format!(
            "deleting paragraph {} {}",
            paragraphs[p].index,
            excerpt(&paragraphs[p].text)
        ),
    }
}

/// Text compared for alignment: whitespace collapsed, symbols and leading
/// typed numbering left out.
fn key(text: &str) -> String {
    let spaced = text.replace('\u{FFFC}', " ");
    let collapsed = crate::util::collapse_whitespace(&spaced);
    collapsed[enumerator_len(&collapsed)..].to_string()
}

/// The length of typed numbering at the start of `text` with the whitespace
/// after it: `1.`, `1.2`, `(a)`, `b)`, `iv.`, `A.`. Zero when there is none.
fn enumerator_len(text: &str) -> usize {
    let bytes = text.as_bytes();
    let mut at = 0;
    let open = bytes.first() == Some(&b'(');
    if open {
        at += 1;
    }
    let body_start = at;
    let digits = |from: usize| {
        bytes[from..]
            .iter()
            .take_while(|b| b.is_ascii_digit())
            .count()
    };
    let mut inner_dot = false;
    if digits(at) > 0 {
        at += digits(at);
        while bytes.get(at) == Some(&b'.') && digits(at + 1) > 0 {
            inner_dot = true;
            at += 1 + digits(at + 1);
        }
    } else {
        let letters = bytes[at..]
            .iter()
            .take_while(|b| b.is_ascii_alphabetic())
            .count();
        let roman = bytes[at..at + letters]
            .iter()
            .all(|b| b"ivxlcdmIVXLCDM".contains(b));
        if letters == 1 || (roman && (1..=6).contains(&letters)) {
            at += letters;
        } else {
            return 0;
        }
    }
    if at == body_start || at > 12 {
        return 0;
    }
    let close = match bytes.get(at) {
        Some(b')') => {
            at += 1;
            true
        }
        Some(b'.') if !open => {
            at += 1;
            true
        }
        _ => false,
    };
    if !(close || (inner_dot && !open)) || (open && bytes.get(at - 1) != Some(&b')')) {
        return 0;
    }
    let space = bytes[at..]
        .iter()
        .take_while(|b| b.is_ascii_whitespace())
        .count();
    if space == 0 || at + space >= bytes.len() {
        return 0;
    }
    at + space
}

fn heading_level(style: &str) -> Option<u8> {
    let level = style.strip_prefix("Heading")?.parse::<u8>().ok()?;
    (1..=9).contains(&level).then_some(level)
}

/// Whether a paragraph and a block can be the same: cells pair with cells.
fn compatible(paragraph: &Paragraph, block: &Block) -> bool {
    (paragraph.kind == Kind::Cell) == (block.kind == Kind::Cell)
}

/// Aligns paragraphs and blocks: equal text first, then, in each stretch
/// that differs, the most similar compatible pairs in order, then leftovers
/// by position.
fn align(paragraphs: &[Paragraph], blocks: &[Block]) -> Vec<Edit> {
    let old_keys: Vec<String> = paragraphs.iter().map(|p| key(&p.text)).collect();
    let new_keys: Vec<String> = blocks.iter().map(|b| key(&b.text())).collect();
    let mut edits = Vec::new();
    let mut last: Option<usize> = None;
    for op in capture_diff_slices(Algorithm::Patience, &old_keys, &new_keys) {
        let (tag, olds, news) = op.as_tag_tuple();
        if tag == DiffTag::Equal {
            for (p, b) in olds.zip(news) {
                edits.push(Edit::Pair(p, b));
                last = Some(p);
            }
            continue;
        }
        let olds: Vec<usize> = olds.collect();
        let news: Vec<usize> = news.collect();
        let matched = pair(&olds, &news, |p, b| {
            if !compatible(&paragraphs[p], &blocks[b]) {
                return None;
            }
            let s = similarity(&old_keys[p], &new_keys[b]);
            (s >= PAIR_SIMILARITY).then_some(s)
        });
        let (mut i, mut j) = (0, 0);
        for (mi, mj) in matched
            .into_iter()
            .chain(std::iter::once((olds.len(), news.len())))
        {
            while i < mi && j < mj && compatible(&paragraphs[olds[i]], &blocks[news[j]]) {
                edits.push(Edit::Pair(olds[i], news[j]));
                last = Some(olds[i]);
                i += 1;
                j += 1;
            }
            for &p in &olds[i..mi] {
                edits.push(Edit::Delete(p));
            }
            for &b in &news[j..mj] {
                edits.push(Edit::Insert(b, last));
            }
            if mi < olds.len() && mj < news.len() {
                edits.push(Edit::Pair(olds[mi], news[mj]));
                last = Some(olds[mi]);
            }
            i = mi + 1;
            j = mj + 1;
        }
    }
    edits
}

/// Weighted longest common subsequence of `olds` and `news` under `score`.
fn pair(
    olds: &[usize],
    news: &[usize],
    score: impl Fn(usize, usize) -> Option<f64>,
) -> Vec<(usize, usize)> {
    let (k, m) = (olds.len(), news.len());
    if k == 0 || m == 0 || k * m > MAX_PAIRINGS {
        return Vec::new();
    }
    let scores: Vec<Vec<Option<f64>>> = olds
        .iter()
        .map(|&p| news.iter().map(|&b| score(p, b)).collect())
        .collect();
    let mut best = vec![vec![0.0f64; m + 1]; k + 1];
    for i in (0..k).rev() {
        for j in (0..m).rev() {
            let take = scores[i][j].map_or(f64::MIN, |s| s + best[i + 1][j + 1]);
            best[i][j] = take.max(best[i + 1][j]).max(best[i][j + 1]);
        }
    }
    let mut out = Vec::new();
    let (mut i, mut j) = (0, 0);
    while i < k && j < m {
        if let Some(s) = scores[i][j]
            && (best[i][j] - (s + best[i + 1][j + 1])).abs() < 1e-9
        {
            out.push((i, j));
            i += 1;
            j += 1;
        } else if (best[i][j] - best[i + 1][j]).abs() < 1e-9 {
            i += 1;
        } else {
            j += 1;
        }
    }
    out
}

/// The paragraph's new text: the block's, after the paragraph's own typed
/// numbering when it has some.
fn rewritten(paragraph: &Paragraph, block: &Block) -> String {
    let text = block.text();
    let own = enumerator_len(&paragraph.text);
    if own == 0 {
        return text;
    }
    let theirs = enumerator_len(&text);
    format!("{}{}", &paragraph.text[..own], &text[theirs..])
}

/// The plan's operations. Operation ids are `edit:N`, N the edit's index.
fn operations(
    paragraphs: &[Paragraph],
    blocks: &[Block],
    edits: &[Edit],
    styles: &HashSet<String>,
    whole: &HashSet<usize>,
    dropped: &HashSet<usize>,
) -> Vec<Operation> {
    let at = |p: usize| Selector::Index {
        index: paragraphs[p].index,
        story: None,
    };
    // Paragraphs the plan deletes: a new paragraph cannot anchor on them.
    let gone: HashSet<usize> = edits
        .iter()
        .enumerate()
        .filter(|(entry, _)| !dropped.contains(entry))
        .filter_map(|(entry, edit)| match *edit {
            Edit::Delete(p) if paragraphs[p].kind != Kind::Cell => Some(p),
            Edit::Pair(p, _) if whole.contains(&entry) => Some(p),
            _ => None,
        })
        .collect();
    let stays = |p: &usize| !gone.contains(p);
    // A paragraph replaced whole still anchors: its new paragraph sits
    // right after it, so what the Markdown adds after it follows
    // (#1670, round 8).
    let replaced: HashSet<usize> = edits
        .iter()
        .enumerate()
        .filter(|(entry, _)| !dropped.contains(entry) && whole.contains(entry))
        .filter_map(|(_, edit)| match *edit {
            Edit::Pair(p, _) => Some(p),
            _ => None,
        })
        .collect();
    let anchors = |p: &usize| stays(p) || replaced.contains(p);
    // A paragraph for `block` between paragraph positions `after` and
    // `next`: placed next to a staying neighbour, with the properties of the
    // nearest staying paragraph of the block's kind.
    let insert = |block: &Block, after: Option<usize>, next: Option<usize>| {
        let after = after
            .filter(anchors)
            .or_else(|| after.and_then(|a| (0..a).rev().find(|p| anchors(p))));
        let next = next
            .filter(anchors)
            .or_else(|| next.and_then(|n| (n + 1..paragraphs.len()).find(|p| anchors(p))));
        let (anchor, side) = match (after, next) {
            (Some(a), _) => (a, Side::After),
            (None, Some(n)) => (n, Side::Before),
            (None, None) => return None,
        };
        let nearest = |fits: &dyn Fn(Kind) -> bool| {
            (0..paragraphs.len())
                .filter(|p| stays(p) && fits(paragraphs[*p].kind))
                .min_by_key(|p| p.abs_diff(anchor))
        };
        let same = nearest(&|kind| kind == block.kind);
        let (like, style) = match (same, block.kind) {
            (Some(like), _) => (like, None),
            (None, Kind::Heading(level)) => {
                let name = format!("heading {level}");
                let style = styles.contains(&name).then_some(name);
                let like = nearest(&|kind| matches!(kind, Kind::Heading(_)))
                    .filter(|_| style.is_some())
                    .or_else(|| nearest(&|kind| kind == Kind::Plain))
                    .unwrap_or(anchor);
                (like, style)
            }
            (None, _) => (nearest(&|kind| kind == Kind::Plain).unwrap_or(anchor), None),
        };
        Some(OperationKind::InsertParagraph {
            paragraph: at(anchor),
            position: side,
            runs: block.runs.clone(),
            like: (like != anchor).then(|| at(like)),
            style,
            comment: None,
        })
    };
    let mut operations = Vec::new();
    for (entry, edit) in edits.iter().enumerate() {
        if dropped.contains(&entry) {
            continue;
        }
        let id = Some(format!("edit:{entry}"));
        match *edit {
            Edit::Pair(p, b) => {
                let paragraph = &paragraphs[p];
                let block = &blocks[b];
                if key(&paragraph.text) == key(&block.text()) {
                    continue;
                }
                if !whole.contains(&entry) {
                    operations.push(Operation {
                        id,
                        group: None,
                        kind: OperationKind::Rewrite {
                            paragraph: at(p),
                            text: rewritten(paragraph, block),
                            comment: None,
                        },
                    });
                    continue;
                }
                // The paragraph replaced whole: the new one right after it,
                // with its properties, then the old one deleted. A new
                // paragraph beside a deleted one takes its place, in its
                // cell or story (#1668).
                operations.push(Operation {
                    id: Some(format!("edit:{entry}:new")),
                    group: None,
                    kind: OperationKind::InsertParagraph {
                        paragraph: at(p),
                        position: Side::After,
                        runs: block.runs.clone(),
                        like: None,
                        style: None,
                        comment: None,
                    },
                });
                operations.push(Operation {
                    id,
                    group: None,
                    kind: OperationKind::DeleteParagraph {
                        paragraph: at(p),
                        comment: None,
                    },
                });
            }
            Edit::Delete(p) => {
                let kind = if paragraphs[p].kind == Kind::Cell {
                    // A cell keeps its paragraph; its text goes.
                    OperationKind::Rewrite {
                        paragraph: at(p),
                        text: String::new(),
                        comment: None,
                    }
                } else {
                    OperationKind::DeleteParagraph {
                        paragraph: at(p),
                        comment: None,
                    }
                };
                operations.push(Operation {
                    id,
                    group: None,
                    kind,
                });
            }
            Edit::Insert(b, after) => {
                let block = &blocks[b];
                if block.kind == Kind::Cell {
                    continue;
                }
                let next = edits[entry..].iter().find_map(|e| match e {
                    Edit::Pair(p, _) | Edit::Delete(p) => Some(*p),
                    Edit::Insert(..) => None,
                });
                if let Some(kind) = insert(block, after, next) {
                    operations.push(Operation {
                        id,
                        group: None,
                        kind,
                    });
                }
            }
        }
    }
    operations
}

/// The Markdown's blocks in order, and whether it has footnotes.
fn blocks(markdown: &str) -> (Vec<Block>, bool) {
    let options = Options::ENABLE_TABLES
        | Options::ENABLE_FOOTNOTES
        | Options::ENABLE_STRIKETHROUGH
        | Options::ENABLE_TASKLISTS
        | Options::ENABLE_YAML_STYLE_METADATA_BLOCKS;
    let mut out: Vec<Block> = Vec::new();
    let mut open: Option<Block> = None;
    let mut notes = false;
    let mut skip = 0u32;
    let mut lists = 0u32;
    let mut heading: Option<u8> = None;
    let mut cell = false;
    let mut code = false;
    let (mut bold, mut italic) = (0u32, 0u32);
    let close = |open: &mut Option<Block>, out: &mut Vec<Block>| {
        if let Some(block) = open.take()
            && !block.text().trim().is_empty()
        {
            out.push(block);
        }
    };
    for event in Parser::new_ext(markdown, options) {
        if skip > 0 {
            match event {
                Event::Start(
                    Tag::FootnoteDefinition(_) | Tag::MetadataBlock(_) | Tag::Image { .. },
                ) => {
                    skip += 1;
                }
                Event::End(
                    TagEnd::FootnoteDefinition | TagEnd::MetadataBlock(_) | TagEnd::Image,
                ) => {
                    skip -= 1;
                }
                _ => {}
            }
            continue;
        }
        let kind = || match heading {
            Some(level) => Kind::Heading(level),
            None if cell => Kind::Cell,
            None if lists > 0 => Kind::List,
            None => Kind::Plain,
        };
        match event {
            Event::Start(Tag::FootnoteDefinition(_)) => {
                close(&mut open, &mut out);
                notes = true;
                skip = 1;
            }
            Event::Start(Tag::MetadataBlock(_) | Tag::Image { .. }) => skip = 1,
            Event::Start(Tag::Heading { level, .. }) => {
                close(&mut open, &mut out);
                heading = Some(match level {
                    HeadingLevel::H1 => 1,
                    HeadingLevel::H2 => 2,
                    HeadingLevel::H3 => 3,
                    HeadingLevel::H4 => 4,
                    HeadingLevel::H5 => 5,
                    HeadingLevel::H6 => 6,
                });
            }
            Event::End(TagEnd::Heading(_)) => {
                close(&mut open, &mut out);
                heading = None;
            }
            Event::Start(Tag::List(_)) => {
                close(&mut open, &mut out);
                lists += 1;
            }
            Event::End(TagEnd::List(_)) => {
                close(&mut open, &mut out);
                lists = lists.saturating_sub(1);
            }
            Event::Start(Tag::TableCell) => {
                close(&mut open, &mut out);
                cell = true;
                open = Some(Block {
                    kind: Kind::Cell,
                    runs: Vec::new(),
                });
            }
            Event::End(TagEnd::TableCell) => {
                close(&mut open, &mut out);
                cell = false;
            }
            Event::Start(Tag::CodeBlock(_)) => {
                close(&mut open, &mut out);
                code = true;
            }
            Event::End(TagEnd::CodeBlock) => {
                close(&mut open, &mut out);
                code = false;
            }
            Event::Start(
                Tag::Paragraph | Tag::Item | Tag::BlockQuote(_) | Tag::Table(_) | Tag::HtmlBlock,
            )
            | Event::End(
                TagEnd::Paragraph
                | TagEnd::Item
                | TagEnd::BlockQuote(_)
                | TagEnd::Table
                | TagEnd::HtmlBlock,
            )
            | Event::Rule => close(&mut open, &mut out),
            Event::Start(Tag::Strong) => bold += 1,
            Event::End(TagEnd::Strong) => bold = bold.saturating_sub(1),
            Event::Start(Tag::Emphasis) => italic += 1,
            Event::End(TagEnd::Emphasis) => italic = italic.saturating_sub(1),
            Event::Text(text) | Event::Code(text) => {
                let pieces: Vec<&str> = if code {
                    text.split('\n').collect()
                } else {
                    vec![&text]
                };
                for (n, piece) in pieces.iter().enumerate() {
                    if code && n > 0 {
                        close(&mut open, &mut out);
                    }
                    let block = open.get_or_insert_with(|| Block {
                        kind: kind(),
                        runs: Vec::new(),
                    });
                    push_run(
                        block,
                        &piece.replace(['\t', '\r'], " "),
                        bold > 0,
                        italic > 0,
                    );
                }
            }
            Event::SoftBreak | Event::HardBreak => {
                if let Some(block) = &mut open {
                    push_run(block, " ", bold > 0, italic > 0);
                }
            }
            Event::InlineHtml(html) if html.to_ascii_lowercase().starts_with("<br") => {
                if let Some(block) = &mut open {
                    push_run(block, " ", bold > 0, italic > 0);
                }
            }
            _ => {}
        }
    }
    close(&mut open, &mut out);
    (out, notes)
}

/// Appends text to the block, joining the last run when its formatting is
/// the same.
fn push_run(block: &mut Block, text: &str, bold: bool, italic: bool) {
    if text.is_empty() {
        return;
    }
    let (bold, italic) = (bold.then_some(true), italic.then_some(true));
    match block.runs.last_mut() {
        Some(last) if last.bold == bold && last.italic == italic => last.text.push_str(text),
        _ => block.runs.push(RunSpec {
            text: text.to_string(),
            bold,
            italic,
            ..RunSpec::default()
        }),
    }
}

#[cfg(test)]
#[cfg_attr(all(coverage_nightly, test), coverage(off))]
mod tests {
    use super::*;

    #[test]
    fn typed_numbering_is_found() {
        for (text, number) in [
            ("1. Definitions", "1. "),
            ("1.\tDefinitions", "1.\t"),
            ("1.2 Scope", "1.2 "),
            ("(a) the Buyer", "(a) "),
            ("b) the Seller", "b) "),
            ("iv. Remedies", "iv. "),
            ("IV. Remedies", "IV. "),
            ("A. Parties", "A. "),
            ("A cat sat.", ""),
            ("I am here.", ""),
            ("2024 was a year.", ""),
            ("(see below) text", ""),
            ("1.", ""),
            ("Intro", ""),
        ] {
            assert_eq!(&text[..enumerator_len(text)], number, "{text}");
        }
    }

    #[test]
    fn keys_ignore_numbering_spacing_and_symbols() {
        assert_eq!(key("1.\tDefinitions  apply"), "Definitions apply");
        assert_eq!(key("x \u{FFFC} y"), "x y");
    }

    #[test]
    fn blocks_have_kinds_and_runs() {
        let (blocks, notes) = blocks(
            "# Title\n\nSome **bold** text.[^1]\n\n- item\n\n| a | b |\n|---|---|\n| 1 | 2 |\n\n```\nx\ny\n```\n\n![alt](i.png)\n\n[^1]: note\n",
        );
        let summary: Vec<(Kind, String)> = blocks.iter().map(|b| (b.kind, b.text())).collect();
        assert_eq!(
            summary,
            [
                (Kind::Heading(1), "Title".to_string()),
                (Kind::Plain, "Some bold text.".to_string()),
                (Kind::List, "item".to_string()),
                (Kind::Cell, "a".to_string()),
                (Kind::Cell, "b".to_string()),
                (Kind::Cell, "1".to_string()),
                (Kind::Cell, "2".to_string()),
                (Kind::Plain, "x".to_string()),
                (Kind::Plain, "y".to_string()),
            ]
        );
        assert_eq!(blocks[1].runs[1].bold, Some(true));
        assert!(notes);
    }

    #[test]
    fn a_rewrite_keeps_the_paragraphs_own_numbering() {
        let paragraph = Paragraph {
            index: 0,
            text: "3.\tPayment terms".to_string(),
            kind: Kind::Plain,
        };
        let block = |text: &str| Block {
            kind: Kind::List,
            runs: vec![RunSpec {
                text: text.to_string(),
                ..RunSpec::default()
            }],
        };
        assert_eq!(
            rewritten(&paragraph, &block("Payment and terms")),
            "3.\tPayment and terms"
        );
        assert_eq!(rewritten(&paragraph, &block("4. Payment")), "3.\tPayment");
    }
}
#[cfg(test)]
#[cfg_attr(all(coverage_nightly, test), coverage(off))]
mod markdown_pairing_boundary_tests {
    use super::*;

    fn block(text: &str, kind: Kind) -> Block {
        Block {
            kind,
            runs: vec![RunSpec {
                text: text.to_string(),
                ..RunSpec::default()
            }],
        }
    }

    fn edit_view(edits: &[Edit]) -> Vec<(char, usize, Option<usize>)> {
        edits
            .iter()
            .map(|e| match e {
                Edit::Pair(p, b) => ('P', *p, Some(*b)),
                Edit::Insert(b, after) => ('I', *b, *after),
                Edit::Delete(p) => ('D', *p, None),
            })
            .collect()
    }

    #[test]
    fn ordered_pairing_boundaries_keep_every_authored_index_once() {
        let a = (0..50)
            .map(|i| Paragraph {
                index: i,
                text: format!("old-{i}"),
                kind: Kind::Plain,
            })
            .collect::<Vec<_>>();
        for count in [0, 49, 50, 51] {
            let b = (0..count)
                .map(|i| block(&format!("new-{i}"), Kind::Plain))
                .collect::<Vec<_>>();
            let edits = align(&a, &b);
            let mut expected = (0..count.min(50))
                .map(|i| ('P', i, Some(i)))
                .collect::<Vec<_>>();
            if count < 50 {
                expected.extend((count..50).map(|i| ('D', i, None)));
            }
            if count > 50 {
                expected.push(('I', 50, Some(49)));
            }
            assert_eq!(edit_view(&edits), expected, "count {count}");
            assert_eq!(
                a.iter().map(|p| p.text.as_str()).collect::<Vec<_>>(),
                (0..50)
                    .map(|i| format!("old-{i}"))
                    .collect::<Vec<_>>()
                    .iter()
                    .map(String::as_str)
                    .collect::<Vec<_>>()
            );
        }
        assert_eq!(
            edit_view(&align(&[], &[block("new", Kind::Plain)])),
            vec![('I', 0, None)]
        );
        let paragraphs = vec![
            Paragraph {
                index: 0,
                text: "old cell".to_string(),
                kind: Kind::Cell,
            },
            Paragraph {
                index: 1,
                text: "anchor".to_string(),
                kind: Kind::Plain,
            },
        ];
        assert_eq!(
            edit_view(&align(
                &paragraphs,
                &[block("new body", Kind::Plain), block("anchor", Kind::Plain)]
            )),
            vec![('D', 0, None), ('I', 0, None), ('P', 1, Some(1))]
        );
    }

    #[test]
    fn weighted_pairing_skips_weaker_conflicts_without_crossing_source_owners() {
        let indices = [0, 1, 2];
        let score = |a: usize, b: usize| match (a, b) {
            (0, 0) => Some(0.4),
            (0, 1) => Some(0.9),
            (1, 1) => Some(0.2),
            (2, 2) => Some(1.0),
            _ => None,
        };
        assert_eq!(pair(&indices, &indices, score), vec![(0, 1), (2, 2)]);
        assert_eq!(
            pair(&indices, &indices, |a, b| if a == 1 && b == 0 {
                Some(1.0)
            } else {
                None
            }),
            vec![(1, 0)]
        );
        assert_eq!(
            pair(&indices, &indices, |_, _| None),
            Vec::<(usize, usize)>::new()
        );
        assert_eq!(pair(&[], &indices, score), Vec::<(usize, usize)>::new());
        assert_eq!(pair(&indices, &[], score), Vec::<(usize, usize)>::new());
    }

    #[test]
    fn malformed_numbering_remains_literal_and_normalized_line_breaks_keep_run_formatting() {
        for text in [
            "1234567890123. text",
            "1.x",
            "1. ",
            "(a. text",
            "(1.2 text",
            "vvvvvvv. text",
        ] {
            assert_eq!(enumerator_len(text), 0, "{text}");
            let p = Paragraph {
                index: 0,
                text: text.to_string(),
                kind: Kind::Plain,
            };
            assert_eq!(
                rewritten(&p, &block("replacement", Kind::Plain)),
                "replacement"
            );
        }
        for (source, expected) in [
            ("left  \nright", "left right"),
            ("left<br>right", "left right"),
            ("**left<br>right**", "left right"),
        ] {
            let (parsed, notes) = blocks(source);
            assert!(!notes);
            assert_eq!(parsed.len(), 1);
            assert_eq!(parsed[0].kind, Kind::Plain);
            assert_eq!(parsed[0].text(), expected);
            if source.starts_with("**") {
                assert!(parsed[0].runs.iter().all(|run| run.bold == Some(true)));
            }
        }
    }
}