jubarte-markdown 0.20.0

Markdown in and out of Word documents for the jubarte engine: CriticMarkup as tracked changes, the agent view, and .doc reading
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
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
// SPDX-FileCopyrightText: 2026 Jandira Technologies, LLC
// SPDX-FileCopyrightText: 2024-2026 SylphxAI
//
// SPDX-License-Identifier: Apache-2.0 AND LicenseRef-anymd-MIT

//! CriticMarkup for tracked changes and comments.
//!
//! Converters record insertions, deletions, highlights and comments as flat
//! tokens in document order. [`Critic::into_inline`] then turns them into
//! markup that nests cleanly, so the simple regular expressions CriticMarkup is
//! designed for can parse it:
//!
//! - a span that crosses another is split so every span closes inside its parent;
//! - a span inside another of the same change, or of the same author, is merged
//!   into it (`{--{--x--}--}` would end at the first `--}`); another author's
//!   substitution stays its own span, and that author's other text merges;
//! - spans with no text are dropped, keeping any comments they held;
//! - untagged neighbouring spans of the same kind are joined;
//! - comments inside a highlight move to right after it (`{==text==}{>>note<<}`);
//! - a deletion next to an insertion becomes a substitution (`{~~old~>new~~}`).
//!
//! An insertion or deletion carries its attribution after the closer in
//! braces: `{++new++}{r1 Alice (2026-09-29T14:05:00Z)}`. Distinct revision
//! ids keep separate marks. Only a deletion then a live insertion of one
//! change (as `jubarte changes` lists it) becomes a substitution, with old
//! ids before new ids; two changes print apart (#1363).
//!
//! Document text that happens to contain a CriticMarkup delimiter is escaped
//! with a Markdown backslash (`{\++`), which renders as the original characters
//! but no longer matches the delimiter.

use super::ooxml::{Inline, Marks};

/// A CriticMarkup span kind. Moves are an insertion at their destination and a
/// deletion at their source.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum Mark {
    Insertion,
    Deletion,
    /// A comment's scope, `{==…==}`.
    Scope,
}

impl Mark {
    pub(crate) fn delimiters(self) -> (&'static str, &'static str) {
        match self {
            Self::Insertion => ("{++", "++}"),
            Self::Deletion => ("{--", "--}"),
            Self::Scope => ("{==", "==}"),
        }
    }

    fn index(self) -> usize {
        self as usize
    }
}

/// A tracked change: its kind and who made it when, as the inside of its
/// brace tag (`r1 Alice (2026-09-29T14:05:00Z)`), already escaped.
pub(crate) type Change = (Mark, Option<String>);

/// Agent-view attribution: an internal tag (`0@AC`, `1+2@AC`,
/// `header1#1@AC`, optionally `0@AC 2026-10-03T14:05:00Z`) behind this
/// sentinel, so that it is never
/// mistaken for an author's note. Printed through `agent::format_tag`;
/// never written to the output.
pub(crate) const TAG: &str = "\u{E000}";

/// Plain-view attribution: the change's ids and its `Author (date)` note
/// as an internal tag (`agent::plain_tag_of`) behind this sentinel, so that
/// distinct neighbouring ids remain separate. Printed through
/// `agent::format_plain_tag`: `r1+2 Ana Lima (2026-09-29T14:05:00Z)`.
pub(crate) const PLAIN: &str = "\u{E001}";

/// The internal tag of an attribution, agent or plain.
fn tag(by: Option<&str>) -> Option<&str> {
    let by = bare(by?);
    by.strip_prefix(TAG)
        .or_else(|| by.strip_prefix(PLAIN))
        // A note that names the change it sits inside (`3@BO` re `1@AL`)
        // carries that one after [`ON`]: the tag is the change's own.
        .map(|tagged| tagged.split_once(ON).map_or(tagged, |(own, _)| own))
}

/// The change a nested note names after [`ON`], as its tag reads (no
/// sentinel): the change the note's own sits inside.
fn outer_of(by: Option<&str>) -> Option<&str> {
    let by = bare(by?);
    by.strip_prefix(TAG)
        .or_else(|| by.strip_prefix(PLAIN))?
        .split_once(ON)
        .map(|(_, outer)| outer)
}

/// Whether `new_by`'s change is the one `old_by`'s sits in: a deletion
/// in one mark of an insertion never pairs with the insertion's next mark
/// (`{--old--}{r5 @B re r3+4 @B}{++new++}{r3+4 @B}`), for the
/// substitution's new side would be its own parent.
fn holds(new_by: Option<&str>, old_by: Option<&str>) -> bool {
    fn first(tagged: &str) -> &str {
        tag_parts(tagged.split(ON).next().unwrap_or(tagged)).0
    }
    match (tag(new_by), outer_of(old_by)) {
        (Some(own), Some(outer)) => super::agent::same_change(first(own), first(outer)),
        _ => false,
    }
}

/// The changes a note names sit behind this sentinel after the change's
/// own tag (`\u{E000}3@BO\u{E002}1@AL`), with each tag's optional
/// timestamp before it, exactly as [`display`] prints them: `r3 @BO re
/// r1 @AL`.
const ON: &str = "\u{E002}";

/// Marks an attribution the container carries (a row's or cell's own
/// mark, opened around each paragraph it covers) rather than a change on
/// the content: a sole change under one is not folded into a note, for
/// the writer rebuilds container marks itself, not as a wrap.
pub(crate) const CARRIED: &str = "\u{E003}";

/// An attribution without [`CARRIED`].
fn bare(by: &str) -> &str {
    by.strip_suffix(CARRIED).unwrap_or(by)
}

/// A move's range name sits behind this sentinel, on the change's own tag
/// and before its date or the change it nests in. The printed note says
/// ` move` and, when the range has one, the name (`r3 @B move move1`).
pub(super) const MOVE: char = '\u{E004}';

/// The sentinel an attribution's tag sits behind.
fn sentinel(by: &str) -> &'static str {
    if bare(by).starts_with(PLAIN) {
        PLAIN
    } else {
        TAG
    }
}

/// The tag proper and its optional inline timestamp.
fn tag_parts(tagged: &str) -> (&str, Option<&str>) {
    match tagged.split_once(' ') {
        Some((t, date)) => (t, Some(date)),
        None => (tagged, None),
    }
}

/// A note's text: the formatted tag, or the plain attribution. A note
/// that names the changes it sits inside prints ` re ` between them,
/// one part per level, its own change first. Each plain part's author
/// spells its parentheses percent-wise (`Ana %28consultant%29`), so its
/// date's are the only bare ones and the writer's parser can split the
/// note.
pub(super) fn display(by: &str) -> String {
    let by = bare(by);
    let Some(tagged) = by.strip_prefix(PLAIN).or_else(|| by.strip_prefix(TAG)) else {
        return by.to_string();
    };
    let fold = tagged.contains(ON);
    let plain = by.starts_with(PLAIN);
    let one = |part: &str| {
        let (part, move_name) = strip_move_mark(part);
        let printed = if plain {
            let printed = super::agent::format_plain_tag(&part);
            let printed = if fold {
                escaped_author(&printed)
            } else {
                printed
            };
            // Folded or not: an author that itself holds ` re ` before a
            // revision id spells that space, so the note's own ` re ` stays
            // the only separator.
            spell_re_in_author(&printed)
        } else {
            let (t, date) = tag_parts(&part);
            let mut out = super::agent::format_tag(t);
            if let Some(date) = date {
                out.push(' ');
                out.push_str(date);
            }
            out
        };
        append_move(printed, move_name)
    };
    tagged.split(ON).map(one).collect::<Vec<_>>().join(" re ")
}

/// The part without [`MOVE`], and the range name that sentinel held.
fn strip_move_mark(part: &str) -> (String, Option<&str>) {
    let Some(at) = part.find(MOVE) else {
        return (part.to_string(), None);
    };
    let rest = &part[at + MOVE.len_utf8()..];
    let end = rest.find([' ', '\u{E002}']).unwrap_or(rest.len());
    let mut clean = String::with_capacity(part.len());
    clean.push_str(&part[..at]);
    clean.push_str(&rest[end..]);
    (clean, Some(&rest[..end]))
}

/// ` printed` plus the move clause the writer reads back (` move`, or
/// ` move move1` when the range is named).
fn append_move(mut printed: String, name: Option<&str>) -> String {
    let Some(name) = name else {
        return printed;
    };
    printed.push_str(" move");
    if !name.is_empty() {
        printed.push(' ');
        printed.push_str(name);
    }
    printed
}

/// A printed plain part (`r1+2 Name (date)`) with the name's parentheses,
/// backslashes and `%` percent-spelled (`%28` and its kin, `%` itself
/// `%25`, the spelling the internal tags use): only the date's stay bare.
pub(in crate::markdown) fn escaped_author(part: &str) -> String {
    let Some((ids, rest)) = part.split_once(' ') else {
        return part.to_string();
    };
    let author_end = super::super::view_notes::trailing_bare_group(rest)
        .filter(|&at| super::super::write::is_date(&rest[at + 1..rest.len() - 1]))
        .unwrap_or(rest.len());
    let mut out = String::with_capacity(part.len());
    out.push_str(ids);
    out.push(' ');
    for c in rest[..author_end].chars() {
        match c {
            '%' => out.push_str("%25"),
            '(' => out.push_str("%28"),
            ')' => out.push_str("%29"),
            '\\' => out.push_str("%5C"),
            c => out.push(c),
        }
    }
    out.push_str(&rest[author_end..]);
    out
}

/// A printed plain part whose author holds ` re ` before a revision id
/// (`r3 Bo re r1 Kim (date)` → `r3 Bo%20re r1 Kim (date)`). The space is
/// the same percent-spelling the internal tags use, and the date stays
/// bare. A fold joins its own ` re ` after this, so that separator is
/// untouched.
pub(in crate::markdown) fn spell_re_in_author(part: &str) -> String {
    let Some((ids, rest)) = part.split_once(' ') else {
        return part.to_string();
    };
    let author_end = super::super::view_notes::trailing_bare_group(rest)
        .filter(|&at| super::super::write::is_date(&rest[at + 1..rest.len() - 1]))
        .unwrap_or(rest.len());
    let author = &rest[..author_end];
    let spelled = spell_separator_spaces(author);
    if spelled == author {
        return part.to_string();
    }
    let mut out = String::with_capacity(part.len() + 2);
    out.push_str(ids);
    out.push(' ');
    out.push_str(&spelled);
    out.push_str(&rest[author_end..]);
    out
}

/// ` re ` in `author` whose next word is a revision id, with that leading
/// space spelled `%20`. Any other ` re ` stays, so a name such as
/// `Bo re Kim` is unchanged.
fn spell_separator_spaces(author: &str) -> String {
    if !author.contains(" re ") {
        return author.to_string();
    }
    let mut out = String::with_capacity(author.len());
    let mut rest = author;
    while let Some(at) = rest.find(" re ") {
        let after = &rest[at + " re ".len()..];
        let token = after.split_whitespace().next().unwrap_or("");
        if revision_token(token) {
            out.push_str(&rest[..at]);
            out.push_str("%20re ");
            rest = after;
        } else {
            let keep = at + " re ".len();
            out.push_str(&rest[..keep]);
            rest = &rest[keep..];
        }
    }
    out.push_str(rest);
    out
}

/// Whether `token` is a revision id a plain note would accept as its own.
fn revision_token(token: &str) -> bool {
    !token.starts_with(|c: char| c.is_ascii_digit())
        && !token.starts_with('#')
        && (token == "noid"
            || token.strip_suffix(".noid").is_some()
            || crate::ids::parse_change_ids(token).is_ok())
}

/// Two attributions can pair: equal notes, or two tags of one author in
/// one story with equal inline dates (their ids differ by design). A single
/// substitution tag cannot represent two different timestamps.
fn pairable_attribution(a: Option<&str>, b: Option<&str>) -> bool {
    use super::agent::{handle_of, split_tag};
    match (tag(a), tag(b)) {
        (Some(ta), Some(tb)) if a.map(sentinel) == b.map(sentinel) => {
            let (own_a, own_b) = (tag_parts(ta).0, tag_parts(tb).0);
            let same = if a.is_some_and(|by| sentinel(by) == PLAIN) {
                let attribution = |tag| {
                    crate::markdown::view_notes::plain_revision(&super::agent::format_plain_tag(
                        tag,
                    ))
                    .map(|part| (part.author, part.date))
                };
                attribution(own_a)
                    .zip(attribution(own_b))
                    .is_some_and(|(a, b)| a == b)
            } else {
                handle_of(own_a)
                    .zip(handle_of(own_b))
                    .is_some_and(|(a, b)| a == b)
            };
            same && tag_parts(ta).1 == tag_parts(tb).1 && split_tag(own_a).0 == split_tag(own_b).0
        }
        _ => a == b,
    }
}

/// Whether two sides' changes are one: a substitution prints one change,
/// as `jubarte changes` lists it (#1363). A deletion and the insertion
/// after it with content between them, printed or not, are two.
fn one_change(old_by: Option<&str>, new_by: Option<&str>) -> bool {
    match (tag(old_by), tag(new_by)) {
        (Some(old), Some(new)) => super::agent::one_replacement(tag_parts(old).0, tag_parts(new).0),
        _ => true,
    }
}

/// The new side pairs when it sits in no change, or in the same change as
/// the old side. A parent on the new side alone would make that side its
/// own parent, so those stay two marks.
fn same_nest(old_by: Option<&str>, new_by: Option<&str>) -> bool {
    match outer_of(new_by) {
        None => true,
        Some(new) => outer_of(old_by) == Some(new),
    }
}

/// The tags of two neighbours or of a substitution's two sides (whole
/// attributions, sentinel and all), joined, keeping their shared inline
/// timestamp. The pairing guard requires equal dates.
fn join_tagged(a: &str, b: &str) -> String {
    let kind = sentinel(a);
    let (ta, da) = tag_parts(tag(Some(a)).unwrap_or(a));
    let (tb, db) = tag_parts(tag(Some(b)).unwrap_or(b));
    let mut out = format!(
        "{kind}{}",
        super::agent::join_tags(&[ta.to_string(), tb.to_string()])
    );
    if let Some(date) = da.or(db) {
        out.push(' ');
        out.push_str(date);
    }
    if let Some(outer) = outer_of(Some(a)) {
        out.push_str(ON);
        out.push_str(outer);
    }
    out
}

#[derive(Clone, Debug, PartialEq, Eq)]
enum Leaf {
    /// Document text: escaped when rendered.
    Text {
        text: String,
        marks: Marks,
        link: Option<String>,
    },
    /// Markdown built by the converter (images, math, note references).
    Raw(String),
    /// The inside of a `{>>…<<}` comment, in each of the forms its
    /// contexts print.
    Comment(Note),
}

/// A comment's note, as each of its contexts prints it: the prose around
/// its anchor, and the code block it is anchored in, whose fence unwinds
/// only the escape that form's text uses (#518).
#[derive(Clone, Debug, PartialEq, Eq)]
pub(crate) struct Note {
    pub(crate) prose: String,
    pub(crate) code: String,
}

impl Note {
    /// A note whose text no context changes: it holds nothing either
    /// escape touches.
    #[cfg(test)]
    pub(crate) fn same(text: &str) -> Self {
        Self {
            prose: text.to_string(),
            code: text.to_string(),
        }
    }
}

#[derive(Clone, Debug, PartialEq, Eq)]
enum Token {
    Leaf(Leaf),
    Open(Mark, Option<String>),
    Close(Mark),
}

#[derive(Clone, Debug, PartialEq, Eq)]
enum Node {
    Leaf(Leaf),
    Span(Mark, Option<String>, Vec<Node>),
    /// A change's pieces around the changes it held, with the outer
    /// change's mark and attribution: each piece keeps the attribution
    /// too. CriticMarkup cannot nest a change in a change, so an insertion
    /// holding a deletion prints `{++a++}…{++b++}`, and the pieces never
    /// pair as a substitution.
    Split(Mark, Option<String>, Vec<Node>),
}

impl Node {
    fn has_content(&self) -> bool {
        match self {
            Self::Leaf(Leaf::Comment(_)) => false,
            Self::Leaf(_) => true,
            Self::Span(_, _, children) | Self::Split(.., children) => {
                children.iter().any(Self::has_content)
            }
        }
    }
}

/// Inline content of one paragraph with CriticMarkup spans.
#[derive(Debug, Default)]
pub(crate) struct Critic {
    tokens: Vec<Token>,
    /// The paragraph is joined to more text on the same line (a flattened
    /// table's cells): see [`space_into_last_change`].
    inline_end: bool,
    /// Agent view: runs of spaces stay as the file holds them.
    agent: bool,
    /// A code block's paragraph: its text goes in a fence, its runs of
    /// spaces kept and its CriticMarkup delimiters escaped for the writer
    /// to unwind there.
    code_block: bool,
    /// Source dates, kept separately from the optional printed timestamps.
    source_dates: std::collections::HashMap<String, Option<String>>,
}

impl Critic {
    /// Agent view: keeps runs of spaces as the file holds them.
    pub(crate) fn set_agent(&mut self) {
        self.agent = true;
    }

    /// A code block's paragraph (`SourceCode`): its text goes in a fence
    /// with its runs of spaces, its CriticMarkup delimiters escaped.
    pub(crate) fn set_code_block(&mut self) {
        self.code_block = true;
    }

    /// Marks the paragraph as joined to more text on the same line.
    pub(crate) fn set_inline_end(&mut self) {
        self.inline_end = true;
    }

    pub(crate) fn push(&mut self, text: &str, bold: bool, italic: bool, link: Option<&str>) {
        let marks = Marks {
            bold,
            italic,
            ..Marks::default()
        };
        self.push_styled(text, marks, link);
    }

    /// [`Critic::push`] with underline, which only the agent view sets.
    pub(crate) fn push_styled(&mut self, text: &str, marks: Marks, link: Option<&str>) {
        if !text.is_empty() {
            self.tokens.push(Token::Leaf(Leaf::Text {
                text: text.to_string(),
                marks,
                link: link.map(str::to_string),
            }));
        }
    }

    pub(crate) fn raw(&mut self, markdown: &str) {
        if !markdown.is_empty() {
            self.tokens
                .push(Token::Leaf(Leaf::Raw(markdown.to_string())));
        }
    }

    /// Converter-built Markdown at token position `at` (see [`Critic::len`]),
    /// as the start of the paragraph: text right after it loses its leading
    /// space, up to the first marker, as the paragraph's own start would.
    pub(crate) fn insert_raw(&mut self, at: usize, markdown: &str) {
        for token in &mut self.tokens[at..] {
            match token {
                Token::Leaf(Leaf::Text { text, .. }) => {
                    let trimmed = text.trim_start().to_string();
                    let done = !trimmed.is_empty();
                    *text = trimmed;
                    if done {
                        break;
                    }
                }
                _ => break,
            }
        }
        self.tokens
            .insert(at, Token::Leaf(Leaf::Raw(markdown.to_string())));
    }

    /// Drops the last token when it is the raw `markdown`: a tag the next
    /// run reopens (`</sup><sup>`) stays open across both instead.
    pub(crate) fn reopen(&mut self, markdown: &str) -> bool {
        let last =
            matches!(self.tokens.last(), Some(Token::Leaf(Leaf::Raw(raw))) if raw == markdown);
        if last {
            self.tokens.pop();
        }
        last
    }

    /// The number of tokens so far, a position for [`Critic::insert_raw`].
    pub(crate) fn len(&self) -> usize {
        self.tokens.len()
    }

    /// Drop the tokens pushed since [`Critic::len`] was `len`.
    pub(crate) fn truncate(&mut self, len: usize) {
        self.tokens.truncate(len);
    }

    pub(crate) fn comment(&mut self, note: &Note) {
        self.tokens.push(Token::Leaf(Leaf::Comment(note.clone())));
    }

    /// Opens a span; `by` is the attribution of an insertion or deletion.
    pub(crate) fn open(&mut self, mark: Mark, by: Option<&str>) {
        self.tokens.push(Token::Open(mark, by.map(str::to_string)));
    }

    /// Open a source revision without letting timestamp elision affect pairing.
    pub(crate) fn open_dated(&mut self, mark: Mark, by: Option<&str>, date: Option<String>) {
        if let Some(by) = by {
            self.source_dates.insert(by.to_string(), date);
        }
        self.open(mark, by);
    }

    pub(crate) fn close(&mut self, mark: Mark) {
        self.tokens.push(Token::Close(mark));
    }

    /// Drops every attribution. Inside a comment's own text an attribution would
    /// put a `{>>…<<}` inside another, and the outer one would end at the inner
    /// closer.
    pub(crate) fn unattributed(mut self) -> Self {
        for token in &mut self.tokens {
            if let Token::Open(_, by) = token {
                *by = None;
            }
        }
        self
    }

    /// True when there is no visible text and no comment. Text inside an
    /// insertion or deletion counts even when it is only a space: Word shows
    /// it (`{++ ++}`), and dropping it would lose the change's run. So do
    /// spaces carrying underline, highlight or strike, which show on a
    /// space: a signature line of underlined spaces is not a blank
    /// paragraph. Other whitespace (a tab) renders to nothing the marks
    /// could hold, so it stays blank.
    pub(crate) fn is_blank(&self) -> bool {
        let mut changed = 0usize;
        self.tokens.iter().all(|token| match token {
            Token::Leaf(Leaf::Text { text, marks, .. }) => {
                let marked = marks.underline || marks.highlight.is_some() || marks.strike;
                let spaces = !text.is_empty() && text.chars().all(|c| c == ' ');
                let visible = !text.trim().is_empty() || (spaces && marked);
                changed == 0 && !visible
            }
            Token::Leaf(Leaf::Raw(text)) => changed == 0 && text.trim().is_empty(),
            Token::Leaf(Leaf::Comment(_)) => false,
            Token::Open(Mark::Insertion | Mark::Deletion, _) => {
                changed += 1;
                true
            }
            Token::Close(Mark::Insertion | Mark::Deletion) => {
                changed = changed.saturating_sub(1);
                true
            }
            Token::Open(..) | Token::Close(_) => true,
        })
    }

    /// A header/footer scalar: fold revisions as in the body, preserving
    /// the story walker's escaped text, fields and drawings byte for byte.
    pub(crate) fn into_story(self) -> String {
        let nodes = tidy(unnest(tree(self.tokens)));
        let mut pieces = Vec::new();
        render(
            &nodes,
            &mut pieces,
            Escaping {
                code_block: false,
                story: true,
                source_dates: &self.source_dates,
            },
            &mut 0,
        );
        pieces.into_iter().map(|piece| piece.text).collect()
    }

    pub(crate) fn into_inline(self) -> Inline {
        let mut nodes = tidy(unnest(tree(self.tokens)));
        if self.inline_end {
            space_into_last_change(&mut nodes);
        }
        let escaping = Escaping {
            code_block: self.code_block,
            story: false,
            source_dates: &self.source_dates,
        };
        let mut pieces = Vec::new();
        render(&nodes, &mut pieces, escaping, &mut 0);
        wrap_markers(&mut pieces);
        let mut inline = Inline::default();
        if self.code_block {
            inline.verbatim();
        } else if self.agent {
            inline.keep_spaces();
        }
        for piece in pieces {
            inline.push_styled(&piece.text, piece.marks, piece.link.as_deref());
        }
        inline
    }
}

/// A span still open while the tree is built.
type Open = (Mark, Option<String>, Vec<Node>);

/// Whether `inner` is the open change: neither note names anyone, the notes
/// match, the tags are one logical change, or both name the same author.
/// Another author's span is not. A note's own marks are that author's, so
/// they stay in the reference's span; a substitution's new side is not.
fn same_change_note(outer: Option<&str>, inner: Option<&str>) -> bool {
    match (outer, inner) {
        (None, None) => true,
        (Some(a), Some(b)) if a == b => true,
        (Some(a), Some(b)) => match (tag(Some(a)), tag(Some(b))) {
            (Some(left), Some(right)) => {
                let (left, right) = (tag_parts(left).0, tag_parts(right).0);
                super::agent::same_change(left, right) || same_handle(left, right)
            }
            _ => false,
        },
        _ => false,
    }
}

/// Both tags name one author, and it is the same one. A tag with no handle
/// names nobody, so it does not match another.
fn same_handle(left: &str, right: &str) -> bool {
    match (
        super::agent::handle_of(left),
        super::agent::handle_of(right),
    ) {
        (Some(outer), Some(inner)) if outer != "??" => outer == inner,
        _ => false,
    }
}

/// Whether this open is one side of a substitution that pairs, so it must
/// keep its own span. Another author's other text still joins the open one.
fn substitution_side(children: &[Node], mark: Mark, by: &Option<String>, rest: &[Token]) -> bool {
    match mark {
        Mark::Insertion => {
            last_deletion(children).is_some_and(|old| pairs(old.as_deref(), by.as_deref()))
        }
        Mark::Deletion => {
            following_insertion(rest).is_some_and(|new| pairs(by.as_deref(), new.as_deref()))
        }
        _ => false,
    }
}

/// The deletion a new insertion would pair with, when it is the last thing
/// already in the open span.
fn last_deletion(children: &[Node]) -> Option<&Option<String>> {
    let mut rest = children;
    loop {
        match rest.split_last() {
            Some((Node::Leaf(Leaf::Text { text, .. }), head)) if text.is_empty() => rest = head,
            Some((Node::Leaf(Leaf::Comment(_)), head)) => rest = head,
            _ => break,
        }
    }
    match rest.last() {
        Some(Node::Span(Mark::Deletion, by, _)) => Some(by),
        _ => None,
    }
}

/// The insertion that follows this deletion once the deletion itself closes,
/// when nothing with text sits between them.
fn following_insertion(rest: &[Token]) -> Option<&Option<String>> {
    let mut depth = 1i32;
    let mut closed = false;
    for token in rest {
        if !closed {
            match token {
                Token::Open(Mark::Insertion | Mark::Deletion, _) => depth += 1,
                Token::Close(Mark::Insertion | Mark::Deletion) => {
                    depth -= 1;
                    if depth == 0 {
                        closed = true;
                    }
                }
                _ => {}
            }
            continue;
        }
        match token {
            Token::Open(Mark::Insertion, by) => return Some(by),
            // The insertion of a substitution inside a deletion is written
            // after that deletion closes: text under a deletion is refused.
            Token::Close(_) => {}
            Token::Leaf(Leaf::Text { text, .. }) if text.is_empty() => {}
            Token::Leaf(Leaf::Comment(_)) => {}
            _ => return None,
        }
    }
    None
}

/// The two sides render would pair.
fn pairs(old_by: Option<&str>, new_by: Option<&str>) -> bool {
    same_nest(old_by, new_by)
        && !holds(new_by, old_by)
        && one_change(old_by, new_by)
        && pairable_attribution(old_by, new_by)
}

/// A same-kind span joins the open one unless it is another author's
/// substitution, which has to keep both sides.
fn merges(
    outer_by: Option<&str>,
    children: &[Node],
    mark: Mark,
    by: &Option<String>,
    rest: &[Token],
) -> bool {
    same_change_note(outer_by, by.as_deref()) || !substitution_side(children, mark, by, rest)
}

/// Builds well-nested spans from flat tokens. A span of a kind that is already
/// open merges into it when it is that same change or that author's, which
/// keeps its attribution. Another author's substitution stays open so both
/// sides print. A close that would cross other spans closes them first and
/// opens them again after it, with the same attribution. Spans still open at
/// the end close.
fn tree(tokens: Vec<Token>) -> Vec<Node> {
    let mut root = Vec::new();
    let mut open: Vec<Open> = Vec::new();
    let mut merged = [0usize; 3];
    // Consumed one token at a time; `as_slice` is what follows, for the
    // substitution lookahead, without cloning a leaf.
    let mut tokens = tokens.into_iter();
    while let Some(token) = tokens.next() {
        match token {
            Token::Leaf(leaf) => children(&mut root, &mut open).push(Node::Leaf(leaf)),
            Token::Open(mark, by) => {
                let merges_into = open
                    .iter()
                    .rposition(|(m, ..)| *m == mark)
                    .is_some_and(|at| {
                        open[at + 1..]
                            .iter()
                            .all(|(m, ..)| matches!(m, Mark::Scope))
                            && merges(
                                open[at].1.as_deref(),
                                &open[at].2,
                                mark,
                                &by,
                                tokens.as_slice(),
                            )
                    });
                if merges_into {
                    merged[mark.index()] += 1;
                } else {
                    open.push((mark, by, Vec::new()));
                }
            }
            Token::Close(mark) if merged[mark.index()] > 0 => merged[mark.index()] -= 1,
            Token::Close(mark) => {
                if let Some(at) = open.iter().rposition(|(m, ..)| *m == mark) {
                    let reopen: Vec<(Mark, Option<String>)> = open[at + 1..]
                        .iter()
                        .map(|(m, by, _)| (*m, by.clone()))
                        .collect();
                    close_from(&mut root, &mut open, at);
                    open.extend(reopen.into_iter().map(|(m, by)| (m, by, Vec::new())));
                }
            }
        }
    }
    close_from(&mut root, &mut open, 0);
    root
}

fn children<'a>(root: &'a mut Vec<Node>, open: &'a mut [Open]) -> &'a mut Vec<Node> {
    match open.last_mut() {
        Some((_, _, nodes)) => nodes,
        None => root,
    }
}

/// Closes the open spans from index `at` up, innermost first, into their parent.
fn close_from(root: &mut Vec<Node>, open: &mut Vec<Open>, at: usize) {
    let closed = open.split_off(at);
    let outermost = closed
        .into_iter()
        .rev()
        .fold(None, |inner, (mark, by, mut nodes)| {
            nodes.extend(inner);
            Some(Node::Span(mark, by, nodes))
        });
    children(root, open).extend(outermost);
}

/// Splits a change around every change inside it: CriticMarkup cannot nest
/// one, so `w:ins` holding `w:del` prints `{++a++}…{--x--}…{++b++}` and the
/// writer puts the marks back one inside the other. Each piece keeps the
/// outer change's attribution. A change is a span's direct child; inside a
/// highlight or a comment it may stay.
/// A node is a change, holds one (a comment's scope), or is the pieces
/// one split into: anything the outer change must break around.
fn is_change(node: &Node) -> bool {
    match node {
        Node::Split(..) => true,
        Node::Span(Mark::Scope, _, children) => children.iter().any(is_change),
        Node::Span(..) => true,
        _ => false,
    }
}

/// A piece of a split `mark` made is one of the changes it held, not a
/// piece of the change itself: the writer nests it back inside, so the
/// outer may fold into its note. A same-kind span is the outer's own
/// piece when it is that change, and a held change otherwise.
fn held_change(piece: &Node, outer_mark: Mark, outer_by: Option<&str>) -> bool {
    match piece {
        Node::Split(..) => true,
        Node::Span(Mark::Scope, _, children) => children.iter().any(is_change),
        Node::Span(mark, by, _) => {
            outer_of(by.as_deref()).is_some()
                || *mark != outer_mark
                || !same_change_note(outer_by, by.as_deref())
        }
        _ => false,
    }
}

/// Splits a change around every change inside it: CriticMarkup cannot nest
/// one, so `w:ins` holding `w:del` prints `{++a++}…{--x--}…{++b++}` and the
/// writer puts the marks back one inside the other. Each piece keeps the
/// outer change's attribution. A change is a span's direct child; inside a
/// highlight or a comment it may stay.
fn unnest(nodes: Vec<Node>) -> Vec<Node> {
    fn pieces(mark: Mark, by: &Option<String>, children: Vec<Node>) -> Vec<Node> {
        let mut out = Vec::new();
        let mut live = Vec::new();
        for child in children {
            if is_change(&child) {
                if !live.is_empty() {
                    out.push(Node::Span(mark, by.clone(), std::mem::take(&mut live)));
                }
                out.push(match child {
                    Node::Span(Mark::Scope, note, children) => {
                        Node::Span(Mark::Scope, note, pieces(mark, by, children))
                    }
                    child => child,
                });
            } else {
                live.push(child);
            }
        }
        if !live.is_empty() {
            out.push(Node::Span(mark, by.clone(), live));
        }
        out
    }
    nodes
        .into_iter()
        .map(|node| {
            let Node::Span(mark, by, children) = node else {
                return node;
            };
            let children = unnest(children);
            if matches!(mark, Mark::Insertion | Mark::Deletion) && children.iter().any(is_change) {
                Node::Split(mark, by.clone(), pieces(mark, &by, children))
            } else {
                Node::Span(mark, by, children)
            }
        })
        .collect()
}

/// Names `outer` in the note of the change `node` is: `A` inserted what
/// `B` deleted, and the note reads `r3 @B re r1 @A`, so the writer nests
/// them again. Both must be tagged attributions of one view.
fn fold_on(node: &mut Node, outer: Option<&str>) {
    let Some(outer) = outer else {
        return;
    };
    // A row's or cell's own mark wraps the content the container covers;
    // the writer rebuilds it there, not as a wrap around the sole change.
    if outer.ends_with(CARRIED) {
        return;
    }
    let Some(tagged) = outer
        .strip_prefix(TAG)
        .or_else(|| outer.strip_prefix(PLAIN))
    else {
        return;
    };
    match node {
        Node::Span(Mark::Insertion | Mark::Deletion, by, _) => {
            if let Some(own) = by.as_ref()
                && sentinel(own) == sentinel(outer)
                && tag(Some(own)) != tag(Some(outer))
            {
                *by = Some(format!("{own}{ON}{tagged}"));
            }
        }
        Node::Span(Mark::Scope, _, children) | Node::Split(_, _, children) => {
            for child in children {
                fold_on(child, Some(outer));
            }
        }
        _ => {}
    }
}

/// Drops empty spans, joins neighbours of the same kind and attribution, and
/// moves comments out of highlights, at every level.
fn tidy(nodes: Vec<Node>) -> Vec<Node> {
    let mut out: Vec<Node> = Vec::new();
    for node in nodes {
        let mut comments = Vec::new();
        let node = if let Node::Split(mark, outer, pieces) = node {
            // The pieces tidy as one list: what emptied out of a child change
            // leaves its neighbours adjacent, and they join again. A piece
            // with nothing in it can only hold comments: they move out and
            // the piece goes, so where the outer's changes stand decides
            // its fate, not where a comment chanced to sit.
            let mut pieces = tidy(pieces);
            pieces.retain(|piece| {
                if piece.has_content() {
                    true
                } else {
                    lift_comments(std::slice::from_ref(piece).to_vec(), &mut comments);
                    false
                }
            });
            if pieces.is_empty() {
                out.extend(comments);
                continue;
            }
            // Each held change names its outer, even when live pieces of
            // that outer remain beside it.
            for piece in &mut pieces {
                if held_change(piece, mark, outer.as_deref()) {
                    fold_on(piece, outer.as_deref());
                }
            }
            if pieces.len() == 1 {
                pieces.remove(0)
            } else {
                out.push(Node::Split(mark, outer, pieces));
                out.extend(comments);
                continue;
            }
        } else {
            node
        };
        let Node::Span(mark, by, nodes) = node else {
            out.push(node);
            out.extend(comments);
            continue;
        };
        let nodes = if mark == Mark::Scope {
            lift_comments(nodes, &mut comments)
        } else {
            nodes
        };
        if !nodes.iter().any(Node::has_content) {
            lift_comments(nodes, &mut out);
        } else if let Some(Node::Span(_, _, before)) = out
            .last_mut()
            .filter(|last| matches!(last, Node::Span(m, b, _) if *m == mark && b == &by && (mark == Mark::Scope || tag(b.as_deref()).is_none())))
        {
            before.extend(nodes);
        } else {
            out.push(Node::Span(mark, by, nodes));
        }
        out.extend(comments);
    }
    join_text(
        out.into_iter()
            .map(|node| match node {
                Node::Span(mark, by, nodes) => Node::Span(mark, by, tidy(nodes)),
                leaf => leaf,
            })
            .collect(),
    )
}

/// Joins neighbouring text of the same formatting. Word splits runs anywhere,
/// so a delimiter can arrive in two pieces (`a -` and `-} b`); escaping the
/// joined text sees it whole.
fn join_text(nodes: Vec<Node>) -> Vec<Node> {
    let mut out: Vec<Node> = Vec::with_capacity(nodes.len());
    for node in nodes {
        match (out.last_mut(), node) {
            (
                Some(Node::Leaf(Leaf::Text {
                    text: before,
                    marks: m,
                    link: l,
                })),
                Node::Leaf(Leaf::Text { text, marks, link }),
            ) if *m == marks && *l == link => {
                before.push_str(&text);
            }
            (_, node) => out.push(node),
        }
    }
    out
}

/// Moves the space before an insertion or deletion that ends the paragraph
/// into it: `in{++ nested++}`, not `in {++nested++}`. Both read the same once
/// accepted, but rejected the second leaves a trailing space, which shows
/// when more text follows on the same line (`in ; gone` in a flattened table).
fn space_into_last_change(nodes: &mut [Node]) {
    let [
        ..,
        Node::Leaf(Leaf::Text { text, .. }),
        Node::Span(Mark::Insertion | Mark::Deletion, by, inner),
    ] = nodes
    else {
        return;
    };
    // A tagged change (agent view) keeps its text exactly as the file holds it.
    if by.as_deref().is_some_and(|by| by.starts_with(TAG)) {
        return;
    }
    // Only into text the change starts with, so the space keeps a format.
    let Some(Node::Leaf(Leaf::Text { text: first, .. })) = inner.first_mut() else {
        return;
    };
    let kept = text.trim_end().len();
    if kept == text.len() || kept == 0 {
        return;
    }
    first.insert_str(0, &text[kept..]);
    text.truncate(kept);
}

/// Removes every comment below `nodes` into `comments`, in order.
fn lift_comments(nodes: Vec<Node>, comments: &mut Vec<Node>) -> Vec<Node> {
    let mut kept = Vec::new();
    for node in nodes {
        match node {
            Node::Leaf(Leaf::Comment(_)) => comments.push(node),
            Node::Span(mark, by, nodes) => {
                kept.push(Node::Span(mark, by, lift_comments(nodes, comments)));
            }
            Node::Split(mark, outer, pieces) => {
                kept.push(Node::Split(mark, outer, lift_comments(pieces, comments)));
            }
            leaf => kept.push(leaf),
        }
    }
    kept
}

/// One piece of rendered inline text. Markers are CriticMarkup delimiters and
/// notes, which take a link only from the text around them.
struct Piece {
    text: String,
    marks: Marks,
    link: Option<String>,
    /// For a marker, the change or comment it belongs to: all of one change's
    /// markers join a link, or none do, so brackets never cross.
    marker: Option<usize>,
}

/// How [`render`] escapes document text.
#[derive(Clone, Copy)]
struct Escaping<'a> {
    /// A code block: its delimiters are broken the way its fence unwinds
    /// ([`escape_code`]), and nothing else is, since a fence holds the rest
    /// of the text as written.
    code_block: bool,
    /// Story pieces are already escaped and carry complete field/drawing syntax.
    story: bool,
    source_dates: &'a std::collections::HashMap<String, Option<String>>,
}

impl Escaping<'_> {
    fn pairable(self, a: Option<&str>, b: Option<&str>) -> bool {
        let date = |by: Option<&str>| {
            by.and_then(|by| {
                self.source_dates
                    .get(bare(by).split(ON).next().unwrap_or_default())
            })
        };
        pairable_attribution(a, b)
            && match (date(a), date(b)) {
                (Some(a), Some(b)) => a == b,
                _ => true,
            }
    }

    /// `text` escaped: Markdown's marks and CriticMarkup's delimiters both,
    /// so nothing reads back as what it is not. A code span's text is
    /// written as is, since a backslash shows there, and a code block's
    /// delimiters are broken the way its fence unwinds.
    fn text(self, text: &str, marks: Marks) -> String {
        if self.code_block {
            escape_code(text)
        } else if marks.code {
            text.to_string()
        } else {
            escape(&super::agent::escape_markdown(text))
        }
    }
}

fn render(nodes: &[Node], out: &mut Vec<Piece>, escaping: Escaping<'_>, ids: &mut usize) {
    fn flatten(nodes: &[Node], out: &mut Vec<Node>) {
        for node in nodes {
            if let Node::Split(.., pieces) = node {
                flatten(pieces, out);
            } else {
                out.push(node.clone());
            }
        }
    }
    let mut flat = Vec::new();
    flatten(nodes, &mut flat);
    let nodes = flat.as_slice();
    let mut index = 0;
    while index < nodes.len() {
        match (&nodes[index], nodes.get(index + 1)) {
            // Pair a deletion then a live insertion of one change.
            // The old side keeps its parent. The new side may share that
            // parent; a different parent on the new side prevents pairing.
            // Insertion then deletion stays in order.
            // A rendered bookmark, comment range or field (w:fldChar)
            // between the marks separates them; proofing, unreferenced
            // bookmarks and empty runs are transparent.
            (
                Node::Span(Mark::Deletion, old_by, old),
                Some(Node::Span(Mark::Insertion, new_by, new)),
            ) if same_nest(old_by.as_deref(), new_by.as_deref())
                && !holds(new_by.as_deref(), old_by.as_deref())
                && one_change(old_by.as_deref(), new_by.as_deref())
                && escaping.pairable(old_by.as_deref(), new_by.as_deref())
                && new.iter().any(Node::has_content) =>
            {
                *ids += 1;
                let id = *ids;
                marker(out, "{~~", id);
                render(old, out, escaping, ids);
                marker(out, "~>", id);
                render(new, out, escaping, ids);
                marker(out, "~~}", id);
                match (old_by.as_deref(), new_by.as_deref()) {
                    (Some(a), Some(b))
                        if tag(Some(a)).is_some()
                            && escaping.pairable(old_by.as_deref(), new_by.as_deref()) =>
                    {
                        let joined = join_tagged(a, b);
                        attribution(out, Some(&joined), id);
                    }
                    _ => {
                        attribution(out, old_by.as_deref(), id);
                        if new_by != old_by {
                            attribution(out, new_by.as_deref(), id);
                        }
                    }
                }
                index += 2;
                continue;
            }
            (Node::Span(mark, by, nodes), _) => {
                *ids += 1;
                let id = *ids;
                let (open, close) = mark.delimiters();
                marker(out, open, id);
                render(nodes, out, escaping, ids);
                marker(out, close, id);
                attribution(out, by.as_deref(), id);
            }
            // The pieces of a split change render side by side, never
            // paired as a substitution: their order is the document's.
            (Node::Split(.., pieces), _) => {
                for piece in pieces {
                    render(std::slice::from_ref(piece), out, escaping, ids);
                }
            }
            (Node::Leaf(Leaf::Text { text, marks, link }), _) => {
                out.push(Piece {
                    text: escaping.text(text, *marks),
                    marks: *marks,
                    link: link.clone(),
                    marker: None,
                });
            }
            (Node::Leaf(Leaf::Raw(markdown)), _) => out.push(Piece {
                text: if escaping.story {
                    markdown.clone()
                } else {
                    math_changes(&if escaping.code_block {
                        escape_code(markdown)
                    } else {
                        defuse(markdown)
                    })
                },
                marks: Marks::default(),
                link: None,
                marker: None,
            }),
            (Node::Leaf(Leaf::Comment(comment)), _) => {
                *ids += 1;
                let inner = if escaping.code_block {
                    &comment.code
                } else {
                    &comment.prose
                };
                marker(out, &note(inner), *ids);
            }
        }
        index += 1;
    }
}

fn marker(out: &mut Vec<Piece>, text: &str, id: usize) {
    out.push(Piece {
        text: text.to_string(),
        marks: Marks::default(),
        link: None,
        marker: Some(id),
    });
}

/// Markers between two pieces of the same link or emphasis take it on, so a
/// change inside a link's text or a bold run keeps one link or one run around
/// it: `[the {++new ++}page](url)`, `**around {++this++} inside**`.
/// CriticMarkup is read before Markdown, so the link and the emphasis still
/// resolve. A change joins only if all its markers do; one that starts inside
/// the link and ends after it stays outside.
/// Code spans are literal to the reader: their marks must stay outside.
fn wrap_markers(pieces: &mut [Piece]) {
    type Format = (Marks, Option<String>);
    let format = |piece: &Piece| -> Format {
        // A marker can remain inside the shared link, but never inside
        // code: the label is parsed Markdown and code contents are literal.
        let marks = if piece.marks.code {
            Marks::default()
        } else {
            piece.marks
        };
        (marks, piece.link.clone())
    };
    let mut joined: Vec<Option<Format>> = vec![None; pieces.len()];
    let mut start = 0;
    while start < pieces.len() {
        if pieces[start].marker.is_none() {
            start += 1;
            continue;
        }
        let end = (start..pieces.len())
            .find(|&at| pieces[at].marker.is_none())
            .unwrap_or(pieces.len());
        let before = start.checked_sub(1).map(|at| format(&pieces[at]));
        let after = pieces.get(end).map(format);
        if let Some(around) =
            before.filter(|f| *f != (Marks::default(), None) && Some(f) == after.as_ref())
        {
            joined[start..end].fill(Some(around));
        }
        start = end;
    }
    let mut refused: std::collections::HashSet<usize> = pieces
        .iter()
        .zip(&joined)
        .filter(|(piece, around)| around.is_none() && piece.marker.is_some())
        .filter_map(|(piece, _)| piece.marker)
        .collect();
    // Markers next to each other join all or none: one that stays outside
    // (`==}` of a highlight begun before the bold) closes the emphasis, and
    // a note after it would reopen it on text that reads back plain.
    loop {
        let mut more = false;
        let mut start = 0;
        while start < pieces.len() {
            let end = (start..pieces.len())
                .find(|&at| pieces[at].marker.is_none())
                .unwrap_or(pieces.len());
            let run = || pieces[start..end].iter().filter_map(|piece| piece.marker);
            if run().any(|id| refused.contains(&id)) {
                for id in run() {
                    more |= refused.insert(id);
                }
            }
            start = end + 1;
        }
        if !more {
            break;
        }
    }
    for (piece, around) in pieces.iter_mut().zip(joined) {
        if let Some((marks, link)) =
            around.filter(|_| piece.marker.is_some_and(|id| !refused.contains(&id)))
        {
            piece.marks = marks;
            piece.link = link;
        }
    }
}

fn attribution(out: &mut Vec<Piece>, by: Option<&str>, id: usize) {
    if let Some(by) = by {
        marker(out, &attribution_note(&display(by)), id);
    }
}

pub(super) fn attribution_note(inner: &str) -> String {
    let mut out = String::from("{");
    let mut escaped = false;
    for c in inner.chars() {
        if matches!(c, '{' | '}') && !escaped {
            out.push('\\');
        }
        out.push(c);
        escaped = c == '\\' && !escaped;
    }
    out.push('}');
    out
}

fn note(inner: &str) -> String {
    format!("{{>>{inner}<<}}")
}

/// Breaks every CriticMarkup delimiter in document text with a Markdown
/// backslash escape, so it renders unchanged but cannot open or close a span.
pub(crate) fn escape(text: &str) -> String {
    const DELIMITERS: [(&str, &str); 11] = [
        ("{++", "{\\++"),
        ("{--", "{\\--"),
        ("{~~", "{\\~~"),
        ("{>>", "{\\>>"),
        ("{==", "{\\=="),
        ("++}", "++\\}"),
        ("--}", "--\\}"),
        ("~~}", "~~\\}"),
        ("<<}", "<<\\}"),
        ("==}", "==\\}"),
        ("~>", "~\\>"),
    ];
    if !text.contains(['{', '}', '~']) {
        return text.to_string();
    }
    let text = DELIMITERS
        .iter()
        .fold(text.to_string(), |text, (from, to)| text.replace(from, to));
    let mut out = String::new();
    for (at, c) in text.char_indices() {
        if c == '{' && crate::markdown::critic::attribution_tag(&text[at..]).is_some() {
            out.push('\\');
        }
        out.push(c);
    }
    out
}

/// The text of a code block, as its fence holds it: a delimiter of
/// [`escape`] with a run of backslashes at its break point (`{` + the
/// run + `++`, `++` + the run + `}`, `~` + the run + `>`) gains enough of
/// them that the run is odd, and every other backslash is left alone. A
/// fence reads no Markdown escape, so the writer unwinds exactly those
/// odd runs there (see `crate::markdown::critic`), and hand-written code
/// (`C:\path`, a regex `\\d+`) passes through as written.
pub(crate) fn escape_code(text: &str) -> String {
    /// What can follow the backslash run an opener holds (`{` + run).
    const OPENER: [&str; 5] = ["++", "--", "~~", ">>", "=="];
    /// What can hold the run a closer escapes (`++` + run + `}`).
    const CLOSER: [&str; 5] = ["++", "--", "~~", "<<", "=="];
    let bytes = text.as_bytes();
    // The delimiter shape at `at`, if one sits there: its head, the
    // backslash run at its break point, and its tail.
    let shape = |at: usize| -> Option<(usize, usize, usize)> {
        let shape = |head: usize, tails: &[&str]| {
            let mut run = at + head;
            while bytes.get(run) == Some(&b'\\') {
                run += 1;
            }
            let tail = tails
                .iter()
                .find(|tail| text[run..].starts_with(*tail))?
                .len();
            Some((head, run - (at + head), tail))
        };
        if bytes.get(at) == Some(&b'{') {
            shape(1, &OPENER)
        } else if CLOSER.iter().any(|head| text[at..].starts_with(head)) {
            shape(2, &["}"])
        } else if bytes.get(at) == Some(&b'~') {
            shape(1, &[">"])
        } else {
            None
        }
    };
    let mut out = String::with_capacity(text.len());
    let mut at = 0;
    while at < text.len() {
        // Tags are metadata only after a closer or a pilcrow, but a code
        // fence must preserve the same literal text in either position.
        let mut tag_at = at;
        while bytes.get(tag_at) == Some(&b'\\') {
            tag_at += 1;
        }
        if let Some((_, width)) = crate::markdown::critic::attribution_tag(&text[tag_at..]) {
            out.push_str(&"\\".repeat((tag_at - at) * 2 + 1));
            out.push_str(&text[tag_at..tag_at + width]);
            at = tag_at + width;
            continue;
        }

        if let Some((head, run, tail)) = shape(at) {
            out.push_str(&text[at..at + head]);
            for _ in 0..run * 2 + 1 {
                out.push('\\');
            }
            out.push_str(&text[at + head + run..at + head + run + tail]);
            at += head + run + tail;
        } else {
            let c = text[at..].chars().next().unwrap_or_default();
            out.push(c);
            at += c.len_utf8();
        }
    }
    out
}

/// Breaks every CriticMarkup delimiter in Markdown the converter built
/// (equations, image descriptions). Outside an equation a space does it, and
/// an image description reads the same. Inside one, a space would be part of
/// the equation's text and the next write would draw it. An empty group
/// breaks the delimiter and the writer drops it (`$x_{--}$` is `$x_{{}--{}}$`,
/// still the text `--`).
fn defuse(markdown: &str) -> String {
    const SPACED: [(&str, &str); 11] = [
        ("{++", "{ ++"),
        ("{--", "{ --"),
        ("{~~", "{ ~~"),
        ("{>>", "{ >>"),
        ("{==", "{ =="),
        ("++}", "++ }"),
        ("--}", "-- }"),
        ("~~}", "~~ }"),
        ("<<}", "<< }"),
        ("==}", "== }"),
        ("~>", "~ >"),
    ];
    // Openers before closers, so `{--` broken as `{{}--` still has a `--}`
    // for the closer to break.
    const GROUPED: [(&str, &str); 11] = [
        ("{++", "{{}++"),
        ("{--", "{{}--"),
        ("{~~", "{{}~~"),
        ("{>>", "{{}>>"),
        ("{==", "{{}=="),
        ("++}", "++{}}"),
        ("--}", "--{}}"),
        ("~~}", "~~{}}"),
        ("<<}", "<<{}}"),
        ("==}", "=={}}"),
        ("~>", "~{}>"),
    ];
    let mut out = String::with_capacity(markdown.len());
    let mut rest = markdown;
    while !rest.is_empty() {
        let Some(start) = super::unescaped_dollar(rest) else {
            out.push_str(&break_delimiters(rest, &SPACED));
            break;
        };
        out.push_str(&break_delimiters(&rest[..start], &SPACED));
        let fence = if rest[start..].starts_with("$$") {
            "$$"
        } else {
            "$"
        };
        let body = start + fence.len();
        let Some(len) = super::math_body(&rest[body..], fence) else {
            out.push_str(&break_delimiters(&rest[start..], &SPACED));
            break;
        };
        let end = body + len + fence.len();
        out.push_str(fence);
        out.push_str(&break_delimiters(&rest[body..body + len], &GROUPED));
        out.push_str(fence);
        rest = &rest[end..];
    }
    out
}

fn break_delimiters(text: &str, marks: &[(&str, &str)]) -> String {
    marks
        .iter()
        .fold(text.to_string(), |text, (from, to)| text.replace(from, to))
}

/// An equation's changes (`$A={--Ï€--}r^2$`), which the converter marks
/// with [`crate::markdown::math::INSERT_START`] and its kin so `defuse` leaves them whole.
fn math_changes(markdown: &str) -> String {
    use crate::markdown::math;
    if !markdown.contains([
        math::INSERT_START,
        math::INSERT_END,
        math::DELETE_START,
        math::DELETE_END,
    ]) {
        return markdown.to_string();
    }
    markdown
        .replace(math::INSERT_START, "{++")
        .replace(math::INSERT_END, "++}")
        .replace(math::DELETE_START, "{--")
        .replace(math::DELETE_END, "--}")
}

/// Whether `out` ends with a line of backticks: a code fence, which must end
/// its line. A CriticMarkup opener written after it (` ```{++ `) reads as the
/// fence's info string, so the fence stops closing its block and the block
/// swallows what follows (#587).
fn ends_with_fence(out: &str) -> bool {
    out.lines().next_back().is_some_and(|line| {
        let line = line.trim();
        line.len() >= 3 && line.bytes().all(|b| b == b'`')
    })
}

/// Appends `prefix` and `body` to `out` after `separator`. When the separator is
/// itself tracked (a paragraph mark that was inserted or deleted), it is marked
/// the way the CriticMarkup spec marks a paragraph break (`{++\n\n++}`), with
/// its attribution after it. An adjoining span of the same kind and attribution
/// is extended rather than opened twice. Before a code fence the opener cannot
/// share the fence's line ([`ends_with_fence`]): the break is written first and
/// the span opens on the next block's line, or is dropped when it would wrap
/// nothing but the break.
///
/// `space` says the text on either side of a tracked break had a space there,
/// which rendering trimmed: once the break is accepted or rejected away the two
/// paragraphs join with that space, so it goes after the marked break.
pub(crate) fn splice(
    out: &mut String,
    separator: &str,
    prefix: &str,
    body: &str,
    change: Option<&Change>,
    space: bool,
) {
    let Some((mark, by)) = change else {
        out.push_str(separator);
        out.push_str(prefix);
        out.push_str(body);
        return;
    };
    let (open, close) = mark.delimiters();
    let shown = by.as_deref().map(display);
    let by = shown.as_deref().map(attribution_note).unwrap_or_default();
    let end = format!("{close}{by}");
    let continues = body.strip_prefix(open).filter(|rest| {
        rest.find(close)
            .is_some_and(|at| rest[at + close.len()..].starts_with(&by))
    });
    let fence = ends_with_fence(out);
    if let Some(kept) = out.strip_suffix(&end) {
        out.truncate(kept.len());
    } else if fence {
        out.push_str(separator);
        out.push_str(prefix);
        if let Some(rest) = continues {
            out.push_str(open);
            out.push_str(rest);
        } else {
            out.push_str(body);
        }
        return;
    } else {
        out.push_str(open);
    }
    out.push_str(separator);
    out.push_str(prefix);
    if let Some(rest) = continues {
        out.push_str(rest);
    } else {
        out.push_str(&end);
        if space {
            out.push(' ');
        }
        out.push_str(body);
    }
}

/// `{--gone--}{r1 @A}` is `(Deletion, Some("r1 @A"))`. Document text is
/// escaped, so the first closer after the opener is the span's own.
pub(crate) fn whole_change(part: &str) -> Option<Change> {
    [Mark::Insertion, Mark::Deletion]
        .into_iter()
        .find_map(|mark| {
            let (open, close) = mark.delimiters();
            let inner = part.strip_prefix(open)?;
            let after = &inner[inner.find(close)? + close.len()..];
            match after {
                "" => Some((mark, None)),
                _ => {
                    let (by, width) = crate::markdown::critic::attribution_tag(after)?;
                    (width == after.len()).then(|| (mark, Some(by.to_string())))
                }
            }
        })
}

/// Joins parts that sit side by side, such as the cells of a flattened
/// table. A part that is wholly one change takes the separator before it into
/// that change (the first part, the separator after it), so accepting or
/// rejecting the change leaves no stray separator.
pub(crate) fn join_changed(parts: Vec<String>, separator: &str) -> String {
    let mut parts = parts.into_iter();
    let mut out = parts.next().unwrap_or_default();
    let first = whole_change(&out);
    for (index, part) in parts.enumerate() {
        let change = whole_change(&part).or_else(|| first.clone().filter(|_| index == 0));
        splice(&mut out, separator, "", &part, change.as_ref(), false);
    }
    out
}

/// A rendered paragraph, the tracked change on the mark that ends it, and
/// whether its text started and ended with a space before rendering trimmed it.
pub(crate) type Part = (String, Option<Change>, (bool, bool));

/// Joins paragraphs, each paired with the tracked change on the paragraph mark
/// that ends it. A blank paragraph drops the pending change: its own break stays.
pub(crate) fn join_marked(parts: Vec<Part>, separator: &str) -> String {
    join_paragraphs(parts, separator, false)
}

/// [`join_marked`], keeping a separator for each empty paragraph when
/// `keep_empty` (the agent view, whose table line counts every paragraph).
pub(crate) fn join_paragraphs(parts: Vec<Part>, separator: &str, keep_empty: bool) -> String {
    let mut out = String::new();
    let mut pending = None;
    let mut trailing = false;
    // The first paragraph, while it is the only one and wholly one change.
    let mut first = None;
    for (i, (part, mark, (leading, ends_in_space))) in parts.into_iter().enumerate() {
        if part.trim().is_empty() {
            // The agent view keeps an empty paragraph, and its mark's change
            // goes with the break after it.
            pending = if keep_empty { mark } else { None };
            if keep_empty && i > 0 {
                first = None;
                out.push_str(separator);
            }
            continue;
        }
        if out.is_empty() {
            first = whole_change(&part);
            if keep_empty && i > 0 {
                out.push_str(separator);
            }
            out.push_str(&part);
        } else {
            // A break with no change of its own goes with a paragraph that is
            // wholly one change, which leaves no stray break once resolved.
            // The agent view (`keep_empty`) prints the break as its caller
            // marks it: in a change only when the paragraph's mark is.
            let first = first.take();
            let change = if keep_empty {
                pending.take()
            } else {
                pending.take().or_else(|| whole_change(&part)).or(first)
            };
            // The agent view already wrote a leading space as `&#32;`.
            // Another space here would double it.
            let space = trailing || (leading && !part.starts_with("&#32;"));
            splice(&mut out, separator, "", &part, change.as_ref(), space);
        }
        pending = mark;
        trailing = ends_in_space;
    }
    out
}

#[cfg(test)]
#[cfg_attr(all(coverage_nightly, test), coverage(off))]
mod tests {
    use super::*;
    use Mark::{Deletion as Del, Insertion as Ins, Scope as Sc};

    fn text(critic: &mut Critic, t: &str) {
        critic.push(t, false, false, None);
    }

    fn md(critic: Critic) -> String {
        critic.into_inline().render(true)
    }

    /// Builds a paragraph from `(op, value)` steps: `open`/`close` take `+`,
    /// `-` or `=`; `note` is a comment, `raw` raw Markdown, anything else text.
    fn script(steps: &[(&str, &str)]) -> String {
        let mut critic = Critic::default();
        for (op, value) in steps {
            match *op {
                "open" => critic.open(kind(value), None),
                // `by+Ana (d)`: an insertion or deletion made by Ana at d.
                op if op.starts_with("by") => critic.open(kind(&op[2..]), Some(value)),
                "close" => critic.close(kind(value)),
                "note" => critic.comment(&Note::same(value)),
                "raw" => critic.raw(value),
                _ => text(&mut critic, value),
            }
        }
        md(critic)
    }

    fn kind(value: &str) -> Mark {
        match value {
            "+" => Ins,
            "-" => Del,
            _ => Sc,
        }
    }

    #[test]
    fn plain_marks_render_with_their_delimiters() {
        assert_eq!(
            script(&[
                ("open", "+"),
                ("t", "a"),
                ("close", "+"),
                ("t", " "),
                ("open", "="),
                ("t", "b"),
                ("close", "=")
            ]),
            "{++a++} {==b==}"
        );
        assert_eq!(
            script(&[("open", "-"), ("t", "gone"), ("close", "-")]),
            "{--gone--}"
        );
    }

    #[test]
    fn a_deletion_then_an_insertion_is_a_substitution_and_the_reverse_two_changes() {
        let del_ins = [
            ("open", "-"),
            ("t", "old"),
            ("close", "-"),
            ("open", "+"),
            ("t", "new"),
            ("close", "+"),
        ];
        let ins_del = [
            ("open", "+"),
            ("t", "new"),
            ("close", "+"),
            ("open", "-"),
            ("t", "old"),
            ("close", "-"),
        ];
        // Word's Compare writes the new text first; the writer puts
        // `{~~old~>new~~}` back so. The other order keeps its two changes.
        assert_eq!(script(&ins_del), "{++new++}{--old--}");
        assert_eq!(script(&del_ins), "{~~old~>new~~}");
        // Only one pair: a span before it stays on its own.
        let triple = [
            ("open", "-"),
            ("t", "a"),
            ("close", "-"),
            ("open", "+"),
            ("t", "b"),
            ("close", "+"),
            ("open", "-"),
            ("t", "c"),
            ("close", "-"),
        ];
        assert_eq!(script(&triple), "{~~a~>b~~}{--c--}");
    }

    #[test]
    fn another_authors_substitution_stays_inside_the_same_kind() {
        let inside_insertion = [
            ("by+", "\u{E000}7+8@B"),
            ("t", "x"),
            ("by-", "\u{E000}3+4@A"),
            ("t", "a"),
            ("close", "-"),
            ("by+", "\u{E000}3+4@A"),
            ("t", "b"),
            ("close", "+"),
            ("t", "y"),
            ("close", "+"),
        ];
        let inside_deletion = [
            ("by-", "\u{E000}7+8@B"),
            ("t", "x"),
            ("by-", "\u{E000}3+4@A"),
            ("t", "a"),
            ("close", "-"),
            ("by+", "\u{E000}3+4@A"),
            ("t", "b"),
            ("close", "+"),
            ("t", "y"),
            ("close", "-"),
        ];
        // Each side already carries both ids, and this script does not
        // collapse a repeated whole, so the joined tag names them twice.
        assert_eq!(
            script(&inside_insertion),
            "{++x++}{r7+8 @B}{~~a~>b~~}{r3+4+3+4 @A re r7+8 @B}{++y++}{r7+8 @B}"
        );
        assert_eq!(
            script(&inside_deletion),
            "{--x--}{r7+8 @B}{~~a~>b~~}{r3+4+3+4 @A re r7+8 @B}{--y--}{r7+8 @B}"
        );
    }

    #[test]
    fn same_kind_nesting_is_merged_into_one_span() {
        let nested = [
            ("open", "-"),
            ("t", "a"),
            ("open", "-"),
            ("t", "b"),
            ("close", "-"),
            ("t", "c"),
            ("close", "-"),
            ("t", "d"),
        ];
        // The outer close still ends the span: `d` is outside it.
        assert_eq!(script(&nested), "{--abc--}d");
    }

    #[test]
    fn merged_spans_are_counted_per_kind() {
        // Two overlapping highlights, and an insertion inside them: closing the
        // insertion must not use up the highlights' count.
        let steps = [
            ("open", "="),
            ("open", "="),
            ("t", "a"),
            ("open", "+"),
            ("t", "b"),
            ("close", "+"),
            ("t", "c"),
            ("close", "="),
            ("close", "="),
        ];
        assert_eq!(script(&steps), "{==a{++b++}c==}");
    }

    #[test]
    fn different_kinds_cannot_nest() {
        // CriticMarkup cannot nest a change in a change: an insertion that
        // is only the deletion splits away entirely, each piece keeping its
        // attribution.
        let nested = [
            ("open", "+"),
            ("open", "-"),
            ("t", "x"),
            ("close", "-"),
            ("close", "+"),
        ];
        assert_eq!(script(&nested), "{--x--}");
    }

    #[test]
    fn a_change_inside_a_change_splits_the_outer_one() {
        let nested = [
            ("by+", "Ana"),
            ("t", "one "),
            ("by-", "Ben"),
            ("t", "two"),
            ("close", "-"),
            ("t", " three"),
            ("close", "+"),
        ];
        assert_eq!(
            script(&nested),
            "{++one ++}{Ana}{--two--}{Ben}{++ three++}{Ana}"
        );
    }

    #[test]
    fn a_change_that_is_another_s_only_content_names_it_in_the_note() {
        // `A` inserted what `B` deleted: nothing of the insertion is left
        // to split around the deletion, so the note names it after ` re `.
        let nested = [
            ("by+", "\u{E000}1@AL"),
            ("by-", "\u{E000}3@BO"),
            ("t", "x"),
            ("close", "-"),
            ("close", "+"),
        ];
        assert_eq!(script(&nested), "{--x--}{r3 @BO re r1 @AL}");
        // Each tag keeps its own timestamp, as notes print them.
        let dated = [
            ("by+", "\u{E000}1@AL 2026-01-01T09:00:00Z"),
            ("by-", "\u{E000}3@BO 2026-01-02T10:00:00Z"),
            ("t", "x"),
            ("close", "-"),
            ("close", "+"),
        ];
        assert_eq!(
            script(&dated),
            "{--x--}{r3 @BO 2026-01-02T10:00:00Z re r1 @AL 2026-01-01T09:00:00Z}"
        );
        // The plain view names the same change by its author and date.
        let plain = [
            ("by+", "\u{E001}1@Ana%20Lima%20(2026-01-01T09:00:00Z)"),
            ("by-", "\u{E001}3@Ben%20Ode%20(2026-01-02T10:00:00Z)"),
            ("t", "x"),
            ("close", "-"),
            ("close", "+"),
        ];
        assert_eq!(
            script(&plain),
            "{--x--}{r3 Ben Ode (2026-01-02T10:00:00Z) \
             re r1 Ana Lima (2026-01-01T09:00:00Z)}"
        );
    }

    #[test]
    fn a_note_that_names_no_change_keeps_the_sole_one() {
        // Untagged changes have no note to name the outer one in, so the
        // outer stays gone, as joined crossings always were.
        let nested = [
            ("by+", "Ana"),
            ("by-", "Ben"),
            ("t", "x"),
            ("close", "-"),
            ("close", "+"),
        ];
        assert_eq!(script(&nested), "{--x--}{Ben}");
    }

    #[test]
    fn an_insertion_inside_a_deletion_splits_it_too() {
        let nested = [
            ("by-", "Ben"),
            ("t", "one "),
            ("by+", "Ana"),
            ("t", "two"),
            ("close", "+"),
            ("t", " three"),
            ("close", "-"),
        ];
        assert_eq!(
            script(&nested),
            "{--one --}{Ben}{++two++}{Ana}{-- three--}{Ben}"
        );
    }

    #[test]
    fn split_pieces_do_not_pair_as_a_substitution() {
        // `one` was inserted before `two`, not instead of it: the pieces
        // the nested change split keep their order.
        let nested = [
            ("by+", "Ana"),
            ("t", "one "),
            ("by-", "Ben"),
            ("t", "two"),
            ("close", "-"),
            ("t", " three"),
            ("close", "+"),
            ("by-", "Ben"),
            ("t", "four"),
            ("close", "-"),
        ];
        assert_eq!(
            script(&nested),
            "{++one ++}{Ana}{--two--}{Ben}{++ three++}{Ana}\
             {--four--}{Ben}"
        );
    }

    #[test]
    fn a_nest_inside_a_nest_splits_at_every_level() {
        // Two changes inside one: the outer change splits around each.
        let nested = [
            ("by+", "Ana"),
            ("t", "a"),
            ("by-", "Ben"),
            ("t", "x"),
            ("close", "-"),
            ("t", "y"),
            ("by-", "Cid"),
            ("t", "z"),
            ("close", "-"),
            ("t", "b"),
            ("close", "+"),
        ];
        assert_eq!(
            script(&nested),
            "{++a++}{Ana}{--x--}{Ben}{++y++}{Ana}{--z--}{Cid}{++b++}{Ana}"
        );
    }

    #[test]
    fn crossing_spans_are_split_so_each_closes_inside_its_parent() {
        let crossing = [
            ("open", "="),
            ("t", "a"),
            ("open", "+"),
            ("t", "b"),
            ("close", "="),
            ("t", "c"),
            ("close", "+"),
        ];
        assert_eq!(script(&crossing), "{==a{++b++}==}{++c++}");
    }

    #[test]
    fn neighbours_of_the_same_kind_join() {
        let split = [
            ("open", "+"),
            ("t", "a"),
            ("close", "+"),
            ("open", "+"),
            ("t", "b"),
            ("close", "+"),
        ];
        assert_eq!(script(&split), "{++ab++}");
        // Joining exposes inner neighbours, which join too. The deletions
        // around them split away: a change cannot hold a change.
        let deep = [
            ("open", "-"),
            ("open", "+"),
            ("t", "a"),
            ("close", "+"),
            ("close", "-"),
            ("open", "-"),
            ("open", "+"),
            ("t", "b"),
            ("close", "+"),
            ("close", "-"),
        ];
        assert_eq!(script(&deep), "{++ab++}");
    }

    #[test]
    fn empty_spans_vanish_but_keep_their_comments() {
        assert_eq!(
            script(&[("t", "a"), ("open", "+"), ("close", "+"), ("t", "b")]),
            "ab"
        );
        assert_eq!(
            script(&[
                ("t", "a"),
                ("open", "+"),
                ("open", "-"),
                ("note", "c"),
                ("close", "-"),
                ("close", "+")
            ]),
            "a{>>c<<}"
        );
        // A whitespace insertion is real content.
        assert_eq!(
            script(&[
                ("t", "a"),
                ("open", "+"),
                ("t", " "),
                ("close", "+"),
                ("t", "b")
            ]),
            "a{++ ++}b"
        );
    }

    #[test]
    fn comments_inside_a_highlight_follow_it() {
        let steps = [
            ("open", "="),
            ("t", "a"),
            ("note", "one"),
            ("open", "+"),
            ("t", "b"),
            ("note", "two"),
            ("close", "+"),
            ("close", "="),
            ("t", "."),
        ];
        assert_eq!(script(&steps), "{==a{++b++}==}{>>one<<}{>>two<<}.");
        // Two highlights separated by a comment stay separate.
        let steps = [
            ("open", "="),
            ("t", "a"),
            ("close", "="),
            ("note", "x"),
            ("open", "="),
            ("t", "b"),
            ("close", "="),
        ];
        assert_eq!(script(&steps), "{==a==}{>>x<<}{==b==}");
    }

    #[test]
    fn overlapping_highlights_become_their_union() {
        let steps = [
            ("open", "="),
            ("t", "a"),
            ("open", "="),
            ("t", "b"),
            ("close", "="),
            ("note", "A"),
            ("t", "c"),
            ("close", "="),
            ("note", "B"),
        ];
        assert_eq!(script(&steps), "{==abc==}{>>A<<}{>>B<<}");
    }

    #[test]
    fn unclosed_spans_close_at_the_end_and_stray_closes_are_ignored() {
        assert_eq!(
            script(&[("close", "+"), ("open", "-"), ("t", "a")]),
            "{--a--}"
        );
    }

    #[test]
    fn raw_markdown_is_content_whose_delimiters_are_spaced_apart() {
        // A backslash would change the LaTeX. Outside an equation a space
        // keeps a delimiter from closing the deletion around it; inside one
        // an empty group does, and the writer drops it.
        assert_eq!(
            script(&[
                ("open", "-"),
                ("raw", "$x_{--}$ {++ ++} ~> {== ==} {>> <<} {~~ ~~}"),
                ("close", "-"),
                ("raw", "")
            ]),
            "{--$x_{{}--{}}$ { ++ ++ } ~ > { == == } { >> << } { ~~ ~~ }--}"
        );
        assert_eq!(defuse("$a+b$"), "$a+b$");
    }

    #[test]
    fn an_equation_breaks_delimiters_on_an_empty_group() {
        assert_eq!(defuse("$x_{--}$"), "$x_{{}--{}}$");
        assert_eq!(defuse("$y^{++}$"), "$y^{{}++{}}$");
        assert_eq!(defuse("$$x_{--}$$"), "$$x_{{}--{}}$$");
        assert_eq!(defuse("$a~>$"), "$a~{}>$");
        // The broken form has no delimiter left, so a second pass keeps it.
        assert_eq!(defuse("$x_{{}--{}}$"), "$x_{{}--{}}$");
        // A backslash before `$` is the character, not a fence.
        assert_eq!(defuse(r"cost \$5 $x_{--}$"), r"cost \$5 $x_{{}--{}}$");
        // An unclosed dollar is text, and stays broken with a space.
        assert_eq!(defuse("cost $5 and {--x--}"), "cost $5 and { --x-- }");
    }

    #[test]
    fn a_tracked_break_finds_the_real_closer_past_an_equation() {
        let mut critic = Critic::default();
        critic.open(Del, Some("Ana"));
        critic.raw("$x_{--}$");
        critic.close(Del);
        let body = critic.into_inline().render(true);
        let mut out = "{--A--}{Ana}".to_string();
        splice(
            &mut out,
            "\n\n",
            "",
            &body,
            Some(&(Del, Some("Ana".into()))),
            false,
        );
        assert_eq!(out, "{--A\n\n$x_{{}--{}}$--}{Ana}");
    }

    #[test]
    fn formatting_and_links_stay_inside_the_markers() {
        let mut critic = Critic::default();
        critic.open(Ins, None);
        critic.push("bold", true, false, None);
        critic.close(Ins);
        critic.push(" ", false, false, None);
        critic.open(Del, None);
        critic.push("site", false, false, Some("https://x.test"));
        critic.close(Del);
        critic.push("", false, false, None);
        assert_eq!(md(critic), "{++**bold**++} {--[site](https://x.test)--}");
    }

    #[test]
    fn blankness_counts_changed_text_but_ignores_markers() {
        // An inserted or deleted space is text Word shows (`{++ ++}`), so it
        // keeps the paragraph's line; a space only highlighted does not.
        let mut critic = Critic::default();
        critic.open(Ins, None);
        text(&mut critic, "  ");
        critic.raw(" ");
        critic.close(Ins);
        assert!(!critic.is_blank());
        let mut scope = Critic::default();
        scope.open(Mark::Scope, None);
        text(&mut scope, "  ");
        scope.close(Mark::Scope);
        assert!(scope.is_blank());
        scope.comment(&Note::same("note"));
        assert!(!scope.is_blank());
        let mut raw = Critic::default();
        raw.raw("![x](y)");
        assert!(!raw.is_blank());
    }

    #[test]
    fn delimiters_in_document_text_are_escaped() {
        assert_eq!(escape("plain"), "plain");
        assert_eq!(escape("a{b}c"), "a{b}c");
        assert_eq!(
            escape("{++ {-- {~~ {>> {== ++} --} ~~} <<} ==} ~>"),
            "{\\++ {\\-- {\\~~ {\\>> {\\== ++\\} --\\} ~~\\} <<\\} ==\\} ~\\>"
        );
        assert_eq!(escape("{+++}"), "{\\+++\\}");
        assert_eq!(
            script(&[("open", "+"), ("t", "a --} b"), ("close", "+")]),
            "{++a --\\} b++}"
        );
    }

    #[test]
    fn splice_marks_a_tracked_break_and_extends_adjacent_spans() {
        let join = |out: &str, body: &str, mark: Option<Mark>| {
            let mut out = out.to_string();
            splice(
                &mut out,
                "\n\n",
                "",
                body,
                mark.map(|m| (m, None)).as_ref(),
                false,
            );
            out
        };
        assert_eq!(join("A", "B", None), "A\n\nB");
        assert_eq!(join("A", "B", Some(Del)), "A{--\n\n--}B");
        assert_eq!(join("A", "B", Some(Ins)), "A{++\n\n++}B");
        assert_eq!(join("{--A--}", "{--B--}", Some(Del)), "{--A\n\nB--}");
        assert_eq!(
            join("Keep {--old--}", "B", Some(Del)),
            "Keep {--old\n\n--}B"
        );
        assert_eq!(join("A", "{++B++} rest", Some(Ins)), "A{++\n\nB++} rest");
        // An escaped delimiter in the text is not a span to extend.
        assert_eq!(join("a --\\}", "b", Some(Del)), "a --\\}{--\n\n--}b");
        let mut heading = "A".to_string();
        splice(&mut heading, "\n\n", "## ", "B", Some(&(Ins, None)), false);
        assert_eq!(heading, "A{++\n\n## ++}B");
    }

    #[test]
    fn a_span_after_a_code_fence_opens_on_the_next_block_s_line() {
        // The closing fence of a code block must end its line: an opener
        // pushed after its backticks (` ```{++ `) reads as the fence's info
        // string, the block never closes, and it swallows the span and the
        // blocks after it (#587).
        let join = |out: &str, body: &str, mark: Option<Mark>| {
            let mut out = out.to_string();
            splice(
                &mut out,
                "\n\n",
                "",
                body,
                mark.map(|m| (m, None)).as_ref(),
                false,
            );
            out
        };
        let fence = "```\nsome code\n```";
        // The paragraph's own inserted runs keep their span, on the line
        // after the fence: the reader marks the new paragraph's break again
        // from the span alone.
        assert_eq!(
            join(fence, "{++B++}", Some(Ins)),
            "```\nsome code\n```\n\n{++B++}"
        );
        // A mark whose span would cover only the break is dropped, as a
        // tracked break before a table is: written after the fence it wraps
        // nothing (`{++++}`), and written into the fence line it reopens it.
        assert_eq!(join(fence, "B", Some(Ins)), "```\nsome code\n```\n\nB");
        assert_eq!(join(fence, "B", Some(Del)), "```\nsome code\n```\n\nB");
        // The fence's length or indentation does not matter.
        assert_eq!(
            join("  ````\ncode\n````", "{++B++}", Some(Ins)),
            "  ````\ncode\n````\n\n{++B++}"
        );
        // A heading keeps its `## ` outside the span, so it stays a heading.
        let mut heading = "```\ncode\n```".to_string();
        splice(
            &mut heading,
            "\n\n",
            "## ",
            "{++B++}",
            Some(&(Ins, None)),
            false,
        );
        assert_eq!(heading, "```\ncode\n```\n\n## {++B++}");
        // After a block that is not a fence, the span still covers the break.
        assert_eq!(join("A", "B", Some(Ins)), "A{++\n\n++}B");
    }

    #[test]
    fn a_plain_span_across_paragraphs_names_every_piece_s_id() {
        let mark = |id: &str| Some(format!("{PLAIN}{id}@Bo%20Chen"));
        let join = |out: &str, body: &str, id: &str| {
            let mut out = out.to_string();
            splice(&mut out, "\n\n", "", body, Some(&(Del, mark(id))), false);
            out
        };
        // The deleted text before the break, the break, the text after it.
        assert_eq!(
            join("a{--A--}{r8 Bo Chen}", "{--B--}{r10 Bo Chen} b", "9"),
            "a{--A--}{r8 Bo Chen}{--\n\n--}{r9 Bo Chen}{--B--}{r10 Bo Chen} b"
        );
        assert_eq!(
            join("{--A--}{r8 Bo Chen}", "B", "9"),
            "{--A--}{r8 Bo Chen}{--\n\n--}{r9 Bo Chen}B"
        );
        assert_eq!(
            join("A", "{--B--}{r10 Bo Chen}", "9"),
            "A{--\n\n--}{r9 Bo Chen}{--B--}{r10 Bo Chen}"
        );
        let notes = |id: &str| Some(format!("{PLAIN}footnotes#{id}@Bo"));
        let mut out = "{--A--}{footnotes.r2 Bo}".to_string();
        splice(&mut out, "\n\n", "", "B", Some(&(Del, notes("3"))), false);
        assert_eq!(out, "{--A--}{footnotes.r2 Bo}{--\n\n--}{footnotes.r3 Bo}B");
    }

    #[test]
    fn join_marked_uses_each_paragraph_mark_and_resets_on_blanks() {
        let bare = (false, false);
        let parts = vec![
            ("a".to_string(), Some((Del, None)), bare),
            ("b".to_string(), None, bare),
            ("c".to_string(), Some((Ins, None)), bare),
            ("  ".to_string(), Some((Del, None)), bare),
            ("d".to_string(), Some((Del, None)), bare),
        ];
        assert_eq!(join_marked(parts, " "), "a{-- --}b c d");
        assert_eq!(join_marked(Vec::new(), " "), "");
    }

    #[test]
    fn a_space_before_a_change_that_ends_the_paragraph_goes_inside_it() {
        let joined = |steps: &[(&str, &str)]| {
            let mut critic = Critic::default();
            critic.set_inline_end();
            for (op, value) in steps {
                match *op {
                    "open" => critic.open(kind(value), None),
                    "close" => critic.close(kind(value)),
                    _ => text(&mut critic, value),
                }
            }
            md(critic)
        };
        assert_eq!(
            joined(&[("t", "in "), ("open", "+"), ("t", "nested"), ("close", "+")]),
            "in{++ nested++}"
        );
        assert_eq!(
            joined(&[("t", "kept "), ("open", "-"), ("t", "gone"), ("close", "-")]),
            "kept{-- gone--}"
        );
        // Not at the end, or not a change: left as it is.
        assert_eq!(
            joined(&[
                ("t", "a "),
                ("open", "+"),
                ("t", "b"),
                ("close", "+"),
                ("t", " c")
            ]),
            "a {++b++} c"
        );
        assert_eq!(
            joined(&[("t", "a "), ("open", "="), ("t", "b"), ("close", "=")]),
            "a {==b==}"
        );
        // A paragraph of its own keeps the space where it was.
        assert_eq!(
            script(&[("t", "in "), ("open", "+"), ("t", "nested"), ("close", "+")]),
            "in {++nested++}"
        );
    }

    #[test]
    fn a_wholly_changed_paragraph_takes_the_break_before_it() {
        let bare = (false, false);
        let parts = vec![
            ("Para".to_string(), None, bare),
            ("{++Added para++}{noid Alice}".to_string(), None, bare),
        ];
        assert_eq!(
            join_marked(parts, "<br>"),
            "Para{++<br>Added para++}{noid Alice}"
        );
        let parts = vec![
            ("{--Gone--}".to_string(), None, bare),
            ("Kept".to_string(), None, bare),
        ];
        assert_eq!(join_marked(parts, "<br>"), "{--Gone<br>--}Kept");
    }

    #[test]
    fn a_space_trimmed_at_a_tracked_break_comes_back_after_it() {
        // `notice.` + ` The` with the break deleted reads `notice. The` once
        // accepted; the space stays outside the change.
        let parts = vec![
            ("notice.".to_string(), Some((Del, None)), (false, false)),
            ("The".to_string(), None, (true, false)),
            ("before".to_string(), Some((Ins, None)), (false, true)),
            ("accepting".to_string(), None, (false, false)),
        ];
        assert_eq!(
            join_marked(parts, "<br>"),
            "notice.{--<br>--} The<br>before{++<br>++} accepting"
        );
        // An untracked break needs no space: it stays a break.
        let parts = vec![
            ("a".to_string(), None, (false, true)),
            ("b".to_string(), None, (true, false)),
        ];
        assert_eq!(join_marked(parts, "<br>"), "a<br>b");
        // A space the text already keeps (`&#32;`) is not written again.
        let parts = vec![
            ("notice.".to_string(), Some((Del, None)), (false, false)),
            ("&#32;The".to_string(), None, (true, false)),
        ];
        assert_eq!(join_marked(parts, "<br>"), "notice.{--<br>--}&#32;The");
    }

    #[test]
    fn each_change_is_followed_by_its_author_and_date() {
        let steps = [
            ("by+", "Ana (2026-01-02T03:04:00Z)"),
            ("t", "new"),
            ("close", "+"),
            ("t", " and "),
            ("by-", "Bo"),
            ("t", "old"),
            ("close", "-"),
            ("t", "."),
        ];
        assert_eq!(
            script(&steps),
            "{++new++}{Ana (2026-01-02T03:04:00Z)} and {--old--}{Bo}."
        );
    }

    #[test]
    fn a_story_substitution_keeps_each_inline_date_when_written_back() {
        let mut critic = Critic::default();
        for (mark, tag, text) in [
            (Ins, "header1#1@BO 2026-09-30T09:00:00Z", "new"),
            (Del, "header1#2@AL 2026-09-29T14:05:00Z", "old"),
        ] {
            critic.open(mark, Some(&format!("{TAG}{tag}")));
            critic.raw(text);
            critic.close(mark);
        }
        let story = critic.into_story();
        let view = format!(
            "---\nauthors:\n  AL: Alice\n  BO: Bob\nheaders:\n  default: {{id: header1, text: \"{story}\"}}\n---\nBody.\n"
        );
        let written =
            crate::markdown::markdown_to_docx(&view, &crate::markdown::DocxOptions::default())
                .unwrap();
        let package = crate::opc::PartFs::open(&written.docx).unwrap();
        let xml = package.part_string("word/header1.xml").unwrap();
        let header = super::super::ooxml::parse_xml(xml.as_bytes()).unwrap();
        let paragraph = header
            .elements()
            .find(|element| element.local() == "p")
            .unwrap();
        let revisions: Vec<_> = paragraph
            .elements()
            .filter(|element| matches!(element.local(), "ins" | "del"))
            .map(|element| {
                (
                    element.local(),
                    element.attr("author"),
                    element.attr("date"),
                )
            })
            .collect();
        assert_eq!(
            revisions,
            [
                ("ins", Some("Bob"), Some("2026-09-30T09:00:00Z")),
                ("del", Some("Alice"), Some("2026-09-29T14:05:00Z")),
            ],
            "{view}\n{xml}"
        );
    }

    #[test]
    fn adjacent_changes_keep_each_author_in_document_order() {
        let same = [
            ("by+", "Ana"),
            ("t", "new"),
            ("close", "+"),
            ("by-", "Ana"),
            ("t", "old"),
            ("close", "-"),
        ];
        assert_eq!(script(&same), "{++new++}{Ana}{--old--}{Ana}");
        // Each author follows in the order of old then new.
        let different = [
            ("by+", "Bo"),
            ("t", "new"),
            ("close", "+"),
            ("by-", "Ana"),
            ("t", "old"),
            ("close", "-"),
        ];
        assert_eq!(script(&different), "{++new++}{Bo}{--old--}{Ana}");
        let one_side = [
            ("by+", "Bo"),
            ("t", "new"),
            ("close", "+"),
            ("open", "-"),
            ("t", "old"),
            ("close", "-"),
        ];
        assert_eq!(script(&one_side), "{++new++}{Bo}{--old--}");
        let other_side = [
            ("open", "+"),
            ("t", "new"),
            ("close", "+"),
            ("by-", "Ana"),
            ("t", "old"),
            ("close", "-"),
        ];
        assert_eq!(script(&other_side), "{++new++}{--old--}{Ana}");
    }

    #[test]
    fn neighbours_join_only_when_made_by_the_same_person_at_the_same_time() {
        let same = [
            ("by+", "Ana"),
            ("t", "a"),
            ("close", "+"),
            ("by+", "Ana"),
            ("t", "b"),
            ("close", "+"),
        ];
        assert_eq!(script(&same), "{++ab++}{Ana}");
        let different = [
            ("by+", "Ana"),
            ("t", "a"),
            ("close", "+"),
            ("by+", "Bo"),
            ("t", "b"),
            ("close", "+"),
        ];
        assert_eq!(script(&different), "{++a++}{Ana}{++b++}{Bo}");
    }

    #[test]
    fn attributions_stay_with_their_change_inside_a_highlight() {
        // A Word comment moves after the highlight; the attribution does not.
        let steps = [
            ("open", "="),
            ("t", "a"),
            ("by+", "Bo"),
            ("t", "b"),
            ("close", "+"),
            ("note", "Ana: why?"),
            ("close", "="),
        ];
        assert_eq!(script(&steps), "{==a{++b++}{Bo}==}{>>Ana: why?<<}");
    }

    #[test]
    fn merged_nesting_keeps_the_outer_author_and_split_spans_keep_theirs() {
        let nested = [
            ("by-", "Row"),
            ("t", "a"),
            ("by-", "Run"),
            ("t", "b"),
            ("close", "-"),
            ("close", "-"),
        ];
        assert_eq!(script(&nested), "{--ab--}{Row}");
        let crossing = [
            ("open", "="),
            ("t", "a"),
            ("by+", "Bo"),
            ("t", "b"),
            ("close", "="),
            ("t", "c"),
            ("close", "+"),
        ];
        assert_eq!(script(&crossing), "{==a{++b++}{Bo}==}{++c++}{Bo}");
        // An empty change leaves no attribution behind.
        assert_eq!(script(&[("t", "x"), ("by+", "Bo"), ("close", "+")]), "x");
    }

    #[test]
    fn a_tracked_break_extends_spans_only_by_the_same_person() {
        let join = |out: &str, body: &str, change: Option<Change>| {
            let mut out = out.to_string();
            splice(&mut out, "\n\n", "", body, change.as_ref(), false);
            out
        };
        let ana = || Some((Del, Some("Ana".to_string())));
        assert_eq!(join("A", "B", ana()), "A{--\n\n--}{Ana}B");
        assert_eq!(
            join("{--A--}{Ana}", "{--B--}{Ana} rest", ana()),
            "{--A\n\nB--}{Ana} rest"
        );
        // Someone else's deletion on either side stays its own span.
        assert_eq!(
            join("{--A--}{Bo}", "{--B--}{Bo}", ana()),
            "{--A--}{Bo}{--\n\n--}{Ana}{--B--}{Bo}"
        );
        // A body span of the same kind whose closer is followed by nothing
        // attributed is not the same change.
        assert_eq!(
            join("A", "{--B--} rest", ana()),
            "A{--\n\n--}{Ana}{--B--} rest"
        );
        assert_eq!(join("A", "{--B", ana()), "A{--\n\n--}{Ana}{--B");
    }

    #[test]
    fn tree_ignores_a_close_with_nothing_open() {
        assert_eq!(tree(vec![Token::Close(Ins)]), Vec::<Node>::new());
    }

    #[test]
    fn review_5_literal_braces_are_not_whole_change_attributions() {
        for literal in ["{X Y}", "{>>c0 @A: note<<}", "{r1 @A}{literal}"] {
            assert_eq!(
                whole_change(&format!("{{--a--}}{literal}")),
                None,
                "{literal}"
            );
        }
        assert_eq!(
            whole_change("{--a--}{r1 @A}"),
            Some((Del, Some("r1 @A".into())))
        );
    }

    #[test]
    fn a_separator_next_to_a_wholly_changed_part_joins_its_change() {
        let by = "{noid Alice}";
        let joined = |parts: &[&str]| {
            join_changed(parts.iter().map(|part| part.to_string()).collect(), "; ")
        };
        assert_eq!(
            joined(&["in {++nested++}", &format!("{{--gone--}}{by}")]),
            format!("in {{++nested++}}{{--; gone--}}{by}")
        );
        assert_eq!(
            joined(&[&format!("{{--gone--}}{by}"), "kept", "{++new++}"]),
            format!("{{--gone; --}}{by}kept{{++; new++}}")
        );
        // Text around the change keeps the separator outside it.
        assert_eq!(
            joined(&["a {--b--}", "c {++d++} e"]),
            "a {--b--}; c {++d++} e"
        );
    }

    #[test]
    fn a_change_inside_bold_text_keeps_one_bold_run() {
        let mut critic = Critic::default();
        critic.push("around ", true, false, None);
        critic.open(Ins, Some("Ana"));
        critic.push("an insertion", true, false, None);
        critic.close(Ins);
        critic.push(" inside", true, false, None);
        critic.push(" end", false, false, None);
        assert_eq!(md(critic), "**around {++an insertion++}{Ana} inside** end");
        // Bold text next to a plain change stays apart from it.
        let mut critic = Critic::default();
        critic.push("bold", true, false, None);
        critic.open(Del, None);
        critic.push(" plain", false, false, None);
        critic.close(Del);
        assert_eq!(md(critic), "**bold**{-- plain--}");
    }

    #[test]
    fn markers_side_by_side_join_the_emphasis_together_or_not_at_all() {
        // file_170: a highlight begun before the bold ends with the bold's
        // last run; the notes after its end stay outside the bold too.
        let mut critic = Critic::default();
        critic.open(Sc, None);
        critic.push("varied ", false, false, None);
        critic.push("Emphasis lives here. ", true, true, None);
        critic.close(Sc);
        critic.comment(&Note::same("c2 @AS: Testing."));
        critic.push("See note.", true, true, None);
        assert_eq!(
            md(critic),
            "{==varied **_Emphasis lives here._** ==}{>>c2 @AS: Testing.<<}**_See note._**"
        );
        // A note alone between two bold runs still joins them.
        let mut critic = Critic::default();
        critic.push("bold ", true, false, None);
        critic.comment(&Note::same("c0 @AS: Here."));
        critic.push("text", true, false, None);
        assert_eq!(md(critic), "**bold {>>c0 @AS: Here.<<}text**");
    }

    #[test]
    fn a_change_inside_link_text_keeps_one_link() {
        let mut critic = Critic::default();
        let url = Some("https://example.com");
        critic.push("the ", false, false, url);
        critic.open(Ins, Some("Ana"));
        critic.push("new ", false, false, url);
        critic.close(Ins);
        critic.push("page", false, false, url);
        assert_eq!(md(critic), "[the {++new ++}{Ana}page](https://example.com)");
        // A change around a whole link, or between two different links,
        // stays outside them.
        let mut critic = Critic::default();
        critic.push("see ", false, false, None);
        critic.open(Del, None);
        critic.push("old", false, false, Some("https://a.example"));
        critic.close(Del);
        critic.push(" and ", false, false, Some("https://b.example"));
        assert_eq!(
            md(critic),
            "see {--[old](https://a.example)--} [and](https://b.example)"
        );
    }

    #[test]
    fn a_delimiter_split_across_runs_is_escaped() {
        // Word splits runs anywhere; `a -` + `-} b` must not close the deletion.
        assert_eq!(
            script(&[("open", "-"), ("t", "a -"), ("t", "-} b"), ("close", "-")]),
            "{--a --\\} b--}"
        );
        // An empty span between the two pieces is dropped, so they still meet.
        assert_eq!(
            script(&[
                ("open", "-"),
                ("t", "x {"),
                ("open", "+"),
                ("close", "+"),
                ("t", "++ y"),
                ("close", "-")
            ]),
            "{--x {\\++ y--}"
        );
    }

    #[test]
    fn a_note_prints_the_form_its_context_escapes() {
        // A note anchored in a code block is written inside its fence, where
        // the writer unwinds only the fence's escape (#518): the prose form
        // would double its backslashes on every round trip.
        let note = Note {
            prose: r"prose \ and \* form".to_string(),
            code: r"code \ and * form".to_string(),
        };
        let mut prose = Critic::default();
        prose.comment(&note);
        assert_eq!(md(prose), r"{>>prose \ and \* form<<}");
        let mut code = Critic::default();
        code.set_code_block();
        code.comment(&note);
        assert_eq!(md(code), r"{>>code \ and * form<<}");
    }

    #[test]
    fn a_code_block_breaks_only_the_delimiter_shapes_it_holds() {
        // Inside a fence Markdown reads no backslash escape, so the pair
        // has to say itself what is text: a delimiter shape with a run of
        // backslashes at its break point gains enough of them that the run
        // is odd, and every other backslash the document holds is left
        // alone. The writer unwinds exactly those runs there, so code a
        // human wrote (`C:\path`, a regex `\\d+`) is not touched either.
        let code = |t: &str| {
            let mut critic = Critic::default();
            critic.set_code_block();
            text(&mut critic, t);
            md(critic)
        };
        assert_eq!(code("fenced {++x++} code"), "fenced {\\++x++\\} code");
        for (document, markdown) in [
            (r"{\++x++}", r"{\\\++x++\}"),
            (r"{\\++x++}", r"{\\\\\++x++\}"),
            (r"\{++", r"\{\++"),
            ("a~>b", r"a~\>b"),
            (r"a\~>b", r"a\~\>b"),
            (r"back\slash", r"back\slash"),
            (r"C:\path\x", r"C:\path\x"),
            (r"\\d+", r"\\d+"),
            (r"a\\b", r"a\\b"),
        ] {
            assert_eq!(code(document), markdown, "{document}");
        }
        // A tracked change keeps its delimiters: the fence still reads
        // CriticMarkup, so only the text inside is escaped, even where
        // that text ends in a backslash right at a delimiter.
        let mut critic = Critic::default();
        critic.set_code_block();
        critic.open(Ins, None);
        text(&mut critic, "new {++x++}");
        critic.close(Ins);
        assert_eq!(md(critic), "{++new {\\++x++\\}++}");
        let mut critic = Critic::default();
        critic.set_code_block();
        text(&mut critic, "old\\");
        critic.open(Ins, None);
        text(&mut critic, "new");
        critic.close(Ins);
        assert_eq!(md(critic), "old\\{++new++}");
        let mut critic = Critic::default();
        critic.set_code_block();
        critic.open(Ins, None);
        text(&mut critic, "new\\");
        critic.close(Ins);
        assert_eq!(md(critic), "{++new\\++}");
        let mut critic = Critic::default();
        critic.set_code_block();
        critic.open(Ins, None);
        text(&mut critic, "n");
        critic.close(Ins);
        critic.open(Del, None);
        text(&mut critic, "old\\");
        critic.close(Del);
        assert_eq!(md(critic), "{++n++}{--old\\--}");
        // Converter-built Markdown (an equation, an image) is fence text
        // too, and comes back the same way.
        let mut critic = Critic::default();
        critic.set_code_block();
        critic.raw("$x_{--}$ and ![a {++b++}](u\\v)");
        assert_eq!(md(critic), "$x_{\\--}$ and ![a {\\++b++\\}](u\\v)");
    }

    #[test]
    fn the_writer_unwinds_the_code_escape_inside_a_fence() {
        // `encode` reads a fence's escaped text back as the document held
        // it, and still opens the changes the reader wrote there.
        let fence = |code: &str| format!("```\n{code}\n```\n");
        for document in [
            "fenced {++x++} code",
            "{\\++x++}",
            "{\\\\++x++}",
            "\\{++",
            "a~>b",
            "a\\~>b",
            "back\\slash",
            "C:\\path\\x",
            "\\\\d+",
            "a\\\\b",
        ] {
            let source = fence(&escape_code(document));
            assert_eq!(
                crate::markdown::critic::encode(&source),
                fence(document),
                "{document}"
            );
        }
        let mut critic = Critic::default();
        critic.set_code_block();
        critic.open(Ins, None);
        text(&mut critic, "new");
        critic.close(Ins);
        assert_eq!(md(critic), "{++new++}");
    }

    #[test]
    fn the_fence_escape_pair_is_exact() {
        // Over every mix of backslash runs and delimiter shapes:
        // unwind(escape_code(t)) == t, and escape_code(unwind(s)) == s for
        // every s escape_code can produce, so no code text can reach Word
        // changed or name another document's fence.
        let fence = |code: &str| format!("```\n{code}\n```\n");
        let unwind = |escaped: &str| {
            let whole = crate::markdown::critic::encode(&fence(escaped));
            whole["```\n".len()..whole.len() - "\n```\n".len()].to_string()
        };
        let shapes = [
            ("{", "++"),
            ("{", "--"),
            ("{", "~~"),
            ("{", ">>"),
            ("{", "=="),
            ("++", "}"),
            ("--", "}"),
            ("~~", "}"),
            ("<<", "}"),
            ("==", "}"),
            ("~", ">"),
        ];
        let mut texts: Vec<String> = Vec::new();
        for run in 0..5 {
            let backslashes = "\\".repeat(run);
            for (head, tail) in shapes {
                texts.push(format!("a{head}{backslashes}{tail}b"));
                texts.push(format!("{head}{backslashes}{tail}"));
                texts.push(format!("x{backslashes} {head}{backslashes}{tail}"));
            }
            texts.push(format!("u{backslashes}v"));
        }
        texts.extend([
            "{++x++}".to_string(),
            "{\\\\}".to_string(),
            "~~}".to_string(),
            "{~~a~>b~~}".to_string(),
            "C:\\path\\x".to_string(),
            "\\\\d+".to_string(),
        ]);
        for text in &texts {
            let escaped = escape_code(text);
            assert_eq!(unwind(&escaped), *text, "unwind(escape_code({text:?}))");
            assert_eq!(
                escape_code(&unwind(&escaped)),
                escaped,
                "escape_code(unwind({escaped:?}))"
            );
        }
    }
}