crate-cli 11.0.0

A command-line tool for managing Cargo package lifecycles: version bump, workspace dependency sync, members-ordered publish and code formatting.
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
use super::*;

/// Formats Markdown (`.md` / `.markdown` / `.mdx`) matching VS Code `esbenp.prettier-vscode` (Prettier 3.7.4: `tabWidth: 2`, `singleQuote: true`, `semi: true`, `printWidth: 80`, `proseWrap: "preserve"`, `embeddedLanguageFormatting: "auto"`, LF).
///
/// # Arguments
///
/// - `&str` - Raw Markdown source text.
///
/// # Returns
///
/// - `String` - Formatted Markdown document matching Prettier 3.7.4.
pub fn format_markdown(input: &str) -> String {
    format_markdown_inner(input, PRETTIER_PRINT_WIDTH, 0)
}

/// Formats Markdown at a specific line width and embedded recursion depth.
///
/// # Arguments
///
/// - `&str` - Raw Markdown text.
/// - `usize` - Target maximum line width.
/// - `usize` - Current embedded Markdown nesting depth.
///
/// # Returns
///
/// - `String` - Formatted Markdown text with Prettier 3.7.4 parity post-processing.
fn format_markdown_inner(input: &str, width: usize, depth: usize) -> String {
    let base_opts: &'static Options = vscode_tidy_options_ref();
    let prepared: Cow<'_, str> = normalize_markdown_triple_emphasis_prepass(input);
    let formatted: String = if width == PRETTIER_PRINT_WIDTH {
        tidy::markdown::format_with(&prepared, &base_opts.markdown, |code: Code<'_>| {
            format_markdown_embedded(code, base_opts, depth)
        })
    } else {
        let mut custom_opts: Options = base_opts.clone();
        custom_opts.markdown.width = width;
        tidy::markdown::format_with(&prepared, &custom_opts.markdown, |code: Code<'_>| {
            format_markdown_embedded(code, &custom_opts, depth)
        })
    };
    normalize_markdown_prettier_quirks(formatted)
}

/// Lowercases an ASCII fence language tag into a stack buffer without heap allocation.
///
/// # Arguments
///
/// - `&str` - Raw Markdown fence language identifier.
/// - `&'a mut [u8; 16]` - Stack scratch buffer.
///
/// # Returns
///
/// - `Option<&'a str>` - Lowercased language slice if short enough to match a Prettier language.
#[inline(always)]
fn lowercase_embed_lang<'a>(lang: &str, buf: &'a mut [u8; 16]) -> Option<&'a str> {
    let bytes: &[u8] = lang.as_bytes();
    let len: usize = bytes.len();
    if len == 0 || len > buf.len() {
        return None;
    }
    for (dst, &src) in buf[..len].iter_mut().zip(bytes.iter()) {
        *dst = src.to_ascii_lowercase();
    }
    from_utf8(&buf[..len]).ok()
}

/// Returns `true` when `lang` is handled by Prettier's embedded Markdown fence formatter.
///
/// # Arguments
///
/// - `&str` - Lowercased fence language tag.
/// - `usize` - Current nested Markdown recursion depth.
///
/// # Returns
///
/// - `bool` - Whether `format_markdown_embedded` should format this fenced block.
#[inline(always)]
fn is_supported_embed_lang(lang: &str, depth: usize) -> bool {
    MD_EMBED_YAML.contains(&lang)
        || lang == MD_EMBED_JSON
        || lang == MD_EMBED_JSONC
        || lang == MD_EMBED_JSON5
        || MD_EMBED_JS.contains(&lang)
        || MD_EMBED_TS.contains(&lang)
        || lang == MD_EMBED_CSS
        || lang == MD_EMBED_SCSS
        || lang == MD_EMBED_LESS
        || lang == MD_EMBED_HTML
        || lang == MD_EMBED_VUE
        || (MD_EMBED_MARKDOWN.contains(&lang) && depth < MD_MAX_EMBED_DEPTH)
}

/// Formats Prettier-supported YAML frontmatter and embedded fenced code blocks inside Markdown, leaving non-Prettier languages verbatim.
///
/// # Arguments
///
/// - `Code<'_>` - Embedded code slice, language tag, and available column width.
/// - `&Options` - Base VS Code formatter options.
/// - `usize` - Current embedded Markdown nesting depth.
///
/// # Returns
///
/// - `Option<String>` - Formatted embedded code if supported and valid, or `None` to preserve verbatim.
fn format_markdown_embedded(code: Code<'_>, base_opts: &Options, depth: usize) -> Option<String> {
    let mut lang_buf: [u8; 16] = [0u8; 16];
    let lang: &str = lowercase_embed_lang(code.lang, &mut lang_buf)?;
    if !is_supported_embed_lang(lang, depth) {
        return None;
    }
    if MD_EMBED_MARKDOWN.contains(&lang) {
        return Some(format_markdown_inner(code.text, code.width, depth + 1));
    }
    let mut sub_opts: Options = base_opts.clone();
    sub_opts.share(&Shared {
        width: Some(code.width),
        ..Shared::default()
    });
    sub_opts.js.semi = true;
    sub_opts.js.single_quote = true;
    sub_opts.js.jsdoc = false;
    sub_opts.yaml.single_quote = true;
    if MD_EMBED_YAML.contains(&lang) {
        return tidy::yaml::format(code.text, &sub_opts.yaml).ok();
    }
    if lang == MD_EMBED_JSON {
        sub_opts.json.dialect = tidy::json::Dialect::Json;
        return tidy::json::format(code.text, &sub_opts.json).ok();
    }
    if lang == MD_EMBED_JSONC {
        sub_opts.json.dialect = tidy::json::Dialect::Jsonc;
        return tidy::json::format(code.text, &sub_opts.json).ok();
    }
    if lang == MD_EMBED_JSON5 {
        sub_opts.json.dialect = tidy::json::Dialect::Json5;
        return tidy::json::format(code.text, &sub_opts.json).ok();
    }
    if MD_EMBED_JS.contains(&lang) {
        let hint: &str = if lang == "cjs" {
            MD_HINT_CJS
        } else if lang == "jsx" {
            MD_HINT_JSX
        } else {
            MD_HINT_JS
        };
        let mut embed: Verbatim = Verbatim;
        return tidy::format(
            code.text,
            Language::Js(tidy::js::Dialect::Js),
            &sub_opts,
            Some(hint),
            &mut embed,
        )
        .ok();
    }
    if MD_EMBED_TS.contains(&lang) {
        let hint: &str = if lang == "cts" {
            MD_HINT_CTS
        } else if lang == "tsx" {
            MD_HINT_TSX
        } else {
            MD_HINT_TS
        };
        let mut embed: Verbatim = Verbatim;
        return tidy::format(
            code.text,
            Language::Js(tidy::js::Dialect::Ts),
            &sub_opts,
            Some(hint),
            &mut embed,
        )
        .ok();
    }
    if lang == MD_EMBED_CSS {
        return tidy::format(
            code.text,
            Language::Css(Dialect::Css),
            &sub_opts,
            None,
            &mut |req: Request<'_>| format_markup_embedded(req, &sub_opts),
        )
        .ok();
    }
    if lang == MD_EMBED_SCSS {
        return tidy::format(
            code.text,
            Language::Css(Dialect::Scss),
            &sub_opts,
            None,
            &mut |req: Request<'_>| format_markup_embedded(req, &sub_opts),
        )
        .ok();
    }
    if lang == MD_EMBED_LESS {
        return tidy::format(
            code.text,
            Language::Css(Dialect::Less),
            &sub_opts,
            None,
            &mut |req: Request<'_>| format_markup_embedded(req, &sub_opts),
        )
        .ok();
    }
    if lang == MD_EMBED_HTML {
        return tidy::format(
            code.text,
            Language::Html,
            &sub_opts,
            None,
            &mut |req: Request<'_>| format_markup_embedded(req, &sub_opts),
        )
        .ok();
    }
    if lang == MD_EMBED_VUE {
        return tidy::format(
            code.text,
            Language::Vue,
            &sub_opts,
            Some(MD_HINT_VUE),
            &mut |req: Request<'_>| format_markup_embedded(req, &sub_opts),
        )
        .ok();
    }
    None
}

/// Pre-pass rewriting triple emphasis (`***...***` and `___...___`) to `**_..._**` outside frontmatter, code blocks, thematic breaks, and inline code spans.
///
/// Preserves explicit `_**...**_` spans verbatim so `tidy::markdown` matches Prettier 3.7.4 across all four bold-italic delimiter combinations.
///
/// # Arguments
///
/// - `&str` - Raw Markdown input before `tidy::markdown`.
///
/// # Returns
///
/// - `Cow<'_, str>` - Borrowed input when unchanged, or pre-normalized Markdown ready for `tidy::markdown`.
fn normalize_markdown_triple_emphasis_prepass(input: &str) -> Cow<'_, str> {
    if !input.contains(MD_TRIPLE_STAR) && !input.contains(MD_TRIPLE_UNDER) {
        return Cow::Borrowed(input);
    }
    let has_trailing_lf: bool = input.ends_with(TEXT_LF);
    let body: &str = if has_trailing_lf {
        &input[..input.len() - 1]
    } else {
        input
    };
    let mut out: Option<String> = None;
    let mut line_start: usize = 0;
    let mut active_fence: Option<(char, usize, usize)> = None;
    let mut prev_bq_depth: usize = 0;
    let mut list_indent_stack: Vec<usize> = Vec::new();
    let mut prev_can_start_indented_code: bool = true;
    let mut indented_code_threshold: Option<usize> = None;
    let mut frontmatter_fence: Option<&str> = None;
    let mut is_first_line: bool = true;
    for line in body.split(TEXT_LF) {
        let line_len: usize = line.len();
        if is_first_line {
            is_first_line = false;
            let first_clean: &str = line.trim_end_matches(TEXT_CR);
            if first_clean == MD_FRONTMATTER_YAML || first_clean == MD_FRONTMATTER_TOML {
                let remaining: &str = body.get(line_len + 1..).unwrap_or("");
                let has_close: bool = remaining
                    .split(TEXT_LF)
                    .any(|l: &str| l.trim_end_matches(TEXT_CR) == first_clean);
                if has_close {
                    frontmatter_fence = Some(first_clean);
                    line_start += line_len + 1;
                    continue;
                }
            }
        } else if let Some(fence) = frontmatter_fence {
            if line.trim_end_matches(TEXT_CR) == fence {
                frontmatter_fence = None;
            }
            line_start += line_len + 1;
            continue;
        }
        let clean: &str = line.trim_end_matches(TEXT_CR);
        let (bq_depth, _bq_end, indent_cols, stripped): (usize, usize, usize, &str) =
            strip_blockquote_and_measure_indent(clean);
        let mut rewritten: Cow<'_, str> = Cow::Borrowed(line);
        if let Some((fence_ch, fence_len, fence_base)) = active_fence {
            if indent_cols < fence_base + 4 && is_closing_fence(stripped, fence_ch, fence_len) {
                active_fence = None;
                prev_can_start_indented_code = true;
            }
        } else {
            if bq_depth != prev_bq_depth {
                list_indent_stack.clear();
                indented_code_threshold = None;
                prev_can_start_indented_code = true;
                prev_bq_depth = bq_depth;
            }
            let in_indented_code: bool =
                indented_code_threshold.is_some_and(|threshold: usize| indent_cols >= threshold);
            if stripped.is_empty() || in_indented_code {
                prev_can_start_indented_code = true;
            } else {
                indented_code_threshold = None;
                while list_indent_stack
                    .last()
                    .is_some_and(|&col: &usize| indent_cols < col)
                {
                    list_indent_stack.pop();
                }
                let base_indent: usize = list_indent_stack.last().copied().unwrap_or(0);
                let rel_indent: usize = indent_cols.saturating_sub(base_indent);
                if rel_indent >= 4 && prev_can_start_indented_code {
                    indented_code_threshold = Some(base_indent + 4);
                    prev_can_start_indented_code = true;
                } else {
                    let (fence_base, fence_candidate): (usize, &str) = if rel_indent < 4
                        && let Some((marker_cols, after)) = parse_list_marker(stripped)
                    {
                        let next_col: usize = indent_cols + marker_cols;
                        list_indent_stack.push(next_col);
                        (next_col, after)
                    } else {
                        (base_indent, stripped)
                    };
                    if rel_indent < 4
                        && let Some((fence_ch, fence_len)) = parse_opening_fence(fence_candidate)
                    {
                        active_fence = Some((fence_ch, fence_len, fence_base));
                        prev_can_start_indented_code = false;
                    } else if rel_indent < 4 && is_thematic_break_line(stripped) {
                        prev_can_start_indented_code = true;
                    } else {
                        prev_can_start_indented_code =
                            rel_indent < 4 && is_atx_heading_or_dash_break(stripped);
                        if let Cow::Owned(mut changed) = rewrite_triple_emphasis_line(clean) {
                            if line.ends_with(TEXT_CR) {
                                changed.push(TEXT_CR);
                            }
                            rewritten = Cow::Owned(changed);
                        }
                    }
                }
            }
        }
        match (&mut out, rewritten) {
            (Some(buf), line_cow) => {
                buf.push(TEXT_LF);
                buf.push_str(&line_cow);
            }
            (None, Cow::Owned(changed)) => {
                let mut buf: String = String::with_capacity(input.len() + 16);
                buf.push_str(&input[..line_start]);
                buf.push_str(&changed);
                out = Some(buf);
            }
            (None, Cow::Borrowed(_)) => {}
        }
        line_start += line_len + 1;
    }
    match out {
        Some(mut buf) => {
            if has_trailing_lf {
                buf.push(TEXT_LF);
            }
            Cow::Owned(buf)
        }
        None => Cow::Borrowed(input),
    }
}

/// Returns `true` when `stripped` is a CommonMark thematic break made of `*` or `_`.
///
/// # Arguments
///
/// - `&str` - Line slice after container indentation and blockquote markers.
///
/// # Returns
///
/// - `bool` - Whether the line is a `*` or `_` thematic break.
#[inline(always)]
fn is_thematic_break_line(stripped: &str) -> bool {
    let mut marker: u8 = 0;
    let mut count: usize = 0;
    for &b in stripped.as_bytes() {
        if b == b' ' || b == b'\t' {
            continue;
        }
        if b != MD_BYTE_STAR && b != MD_BYTE_UNDER {
            return false;
        }
        if marker == 0 {
            marker = b;
        } else if marker != b {
            return false;
        }
        count += 1;
    }
    count >= 3
}

/// Rewrites `***...***` and `___...___` to `**_..._**` within a single Markdown line outside escapes, code spans, autolinks, and link destinations.
///
/// # Arguments
///
/// - `&str` - Single non-code Markdown line without trailing CR.
///
/// # Returns
///
/// - `Cow<'_, str>` - Borrowed line when unchanged, or rewritten line.
fn rewrite_triple_emphasis_line(line: &str) -> Cow<'_, str> {
    if !line.contains(MD_TRIPLE_STAR) && !line.contains(MD_TRIPLE_UNDER) {
        return Cow::Borrowed(line);
    }
    let bytes: &[u8] = line.as_bytes();
    let len: usize = bytes.len();
    let mut out: Option<String> = None;
    let mut i: usize = 0;
    while i < len {
        if bytes[i] == b'\\' && i + 1 < len {
            let next_len: usize = if bytes[i + 1] < 0x80 {
                1
            } else {
                line[(i + 1)..]
                    .chars()
                    .next()
                    .map(char::len_utf8)
                    .unwrap_or(1)
            };
            if let Some(ref mut buf) = out {
                buf.push_str(&line[i..(i + 1 + next_len)]);
            }
            i += 1 + next_len;
            continue;
        }
        if bytes[i] == MD_BYTE_BACKTICK {
            let end_code: usize = scan_inline_code_span(bytes, i);
            if let Some(ref mut buf) = out {
                buf.push_str(&line[i..end_code]);
            }
            i = end_code;
            continue;
        }
        if bytes[i] == MD_BYTE_RBRACKET
            && i + 1 < len
            && bytes[i + 1] == MD_BYTE_LPAREN
            && let Some(end_link) = scan_inline_link_tail(bytes, i + 2)
        {
            if let Some(ref mut buf) = out {
                buf.push_str(&line[i..end_link]);
            }
            i = end_link;
            continue;
        }
        if bytes[i] == MD_BYTE_LT
            && let Some(end_angle) = scan_autolink_or_tag(bytes, i)
        {
            if let Some(ref mut buf) = out {
                buf.push_str(&line[i..end_angle]);
            }
            i = end_angle;
            continue;
        }
        if (bytes[i] == MD_BYTE_STAR || bytes[i] == MD_BYTE_UNDER)
            && i + 2 < len
            && bytes[i + 1] == bytes[i]
            && bytes[i + 2] == bytes[i]
        {
            let delim: u8 = bytes[i];
            let prev_not_delim: bool = i == 0 || bytes[i - 1] != delim;
            let next_not_delim: bool = i + 3 < len && bytes[i + 3] != delim;
            let next_not_ws: bool = i + 3 < len && !bytes[i + 3].is_ascii_whitespace();
            let under_boundary_ok: bool = delim != MD_BYTE_UNDER
                || i == 0
                || !line[..i]
                    .chars()
                    .next_back()
                    .is_some_and(char::is_alphanumeric);
            if prev_not_delim
                && next_not_delim
                && next_not_ws
                && under_boundary_ok
                && let Some(close_idx) = find_closing_triple_emphasis(line, i + 3, delim)
            {
                let buf: &mut String = out.get_or_insert_with(|| {
                    let mut s: String = String::with_capacity(len);
                    s.push_str(&line[..i]);
                    s
                });
                buf.push_str(MD_PRETTIER_BOLD_ITALIC_OPEN);
                buf.push_str(&line[(i + 3)..close_idx]);
                buf.push_str(MD_PRETTIER_BOLD_ITALIC_CLOSE);
                i = close_idx + 3;
                continue;
            }
        }
        let ch_len: usize = if bytes[i] < 0x80 {
            1
        } else {
            line[i..].chars().next().map(char::len_utf8).unwrap_or(1)
        };
        if let Some(ref mut buf) = out {
            buf.push_str(&line[i..(i + ch_len)]);
        }
        i += ch_len;
    }
    match out {
        Some(buf) => Cow::Owned(buf),
        None => Cow::Borrowed(line),
    }
}

/// Scans past an inline link `(dest ...)` tail starting right after `](` so destinations are not rewritten.
///
/// # Arguments
///
/// - `&[u8]` - Line bytes.
/// - `usize` - Byte index immediately after `](`.
///
/// # Returns
///
/// - `Option<usize>` - Byte index right after the closing `)` if well-formed.
fn scan_inline_link_tail(bytes: &[u8], start: usize) -> Option<usize> {
    let len: usize = bytes.len();
    let mut i: usize = start;
    while i < len && (bytes[i] == b' ' || bytes[i] == b'\t') {
        i += 1;
    }
    let after_dest: usize = scan_link_destination(bytes, i)?;
    let mut j: usize = after_dest;
    while j < len && (bytes[j] == b' ' || bytes[j] == b'\t') {
        j += 1;
    }
    if j < len && bytes[j] == MD_BYTE_RPAREN {
        return Some(j + 1);
    }
    if j > after_dest
        && j < len
        && (bytes[j] == MD_BYTE_DQUOTE || bytes[j] == MD_BYTE_SQUOTE || bytes[j] == MD_BYTE_LPAREN)
    {
        let close_delim: u8 = if bytes[j] == MD_BYTE_LPAREN {
            MD_BYTE_RPAREN
        } else {
            bytes[j]
        };
        j += 1;
        while j < len {
            if bytes[j] == b'\\' && j + 1 < len {
                j += 2;
                continue;
            }
            if bytes[j] == close_delim {
                j += 1;
                break;
            }
            j += 1;
        }
        while j < len && (bytes[j] == b' ' || bytes[j] == b'\t') {
            j += 1;
        }
        if j < len && bytes[j] == MD_BYTE_RPAREN {
            return Some(j + 1);
        }
    }
    None
}

/// Scans an angle-bracket autolink or raw HTML tag starting at `<` so inner characters are not rewritten.
///
/// # Arguments
///
/// - `&[u8]` - Line bytes.
/// - `usize` - Byte index of `<`.
///
/// # Returns
///
/// - `Option<usize>` - Byte index right after `>` when `<...>` is an autolink or tag.
fn scan_autolink_or_tag(bytes: &[u8], start: usize) -> Option<usize> {
    let len: usize = bytes.len();
    if start + 2 >= len {
        return None;
    }
    let next: u8 = bytes[start + 1];
    if !next.is_ascii_alphabetic() && next != MD_BYTE_SLASH && next != MD_BYTE_BANG {
        return None;
    }
    let mut i: usize = start + 2;
    while i < len {
        if bytes[i] == MD_BYTE_GT {
            return Some(i + 1);
        }
        if bytes[i] == MD_BYTE_LT {
            return None;
        }
        i += 1;
    }
    None
}

/// Finds the matching closing `***` or `___` delimiter for a triple-emphasis span starting at `start`.
///
/// # Arguments
///
/// - `&str` - Full Markdown line.
/// - `usize` - Byte index right after the opening 3-byte delimiter.
/// - `u8` - Delimiter byte (`MD_BYTE_STAR` or `MD_BYTE_UNDER`).
///
/// # Returns
///
/// - `Option<usize>` - Byte index of the matching closing 3-byte delimiter.
fn find_closing_triple_emphasis(line: &str, start: usize, delim: u8) -> Option<usize> {
    let bytes: &[u8] = line.as_bytes();
    let len: usize = bytes.len();
    let mut i: usize = start;
    while i + 2 < len {
        if bytes[i] == b'\\' && i + 1 < len {
            i += 2;
            continue;
        }
        if bytes[i] == MD_BYTE_BACKTICK {
            i = scan_inline_code_span(bytes, i);
            continue;
        }
        if bytes[i] == delim && bytes[i + 1] == delim && bytes[i + 2] == delim {
            let prev_ok: bool =
                i > start && bytes[i - 1] != delim && !bytes[i - 1].is_ascii_whitespace();
            let next_not_delim: bool = i + 3 >= len || bytes[i + 3] != delim;
            let under_boundary_ok: bool = delim != MD_BYTE_UNDER
                || i + 3 >= len
                || !line[(i + 3)..]
                    .chars()
                    .next()
                    .is_some_and(char::is_alphanumeric);
            if prev_ok && next_not_delim && under_boundary_ok {
                return Some(i);
            }
        }
        i += 1;
    }
    None
}

/// Applies minimal fence-safe and inline-code-safe Prettier 3.7.4 Markdown parity normalizations on top of `tidy::markdown`.
///
/// Returns the owned `input` buffer untouched with zero allocation whenever no line needs modification.
///
/// # Arguments
///
/// - `String` - Markdown output produced by `tidy::markdown`.
///
/// # Returns
///
/// - `String` - Prettier 3.7.4 aligned Markdown output.
fn normalize_markdown_prettier_quirks(input: String) -> String {
    if !input.contains('"') && !input.contains(MD_FENCE_BACKTICK) && !input.contains(MD_FENCE_TILDE)
    {
        return input;
    }
    let has_trailing_lf: bool = input.ends_with(TEXT_LF);
    let body: &str = if has_trailing_lf {
        &input[..input.len() - 1]
    } else {
        input.as_str()
    };
    let mut out: Option<String> = None;
    let mut line_start: usize = 0;
    let mut active_fence: Option<(char, usize, usize)> = None;
    let mut fence_just_opened: bool = false;
    let mut prev_bq_depth: usize = 0;
    let mut list_indent_stack: Vec<usize> = Vec::new();
    let mut prev_can_start_indented_code: bool = true;
    let mut indented_code_threshold: Option<usize> = None;
    let mut frontmatter_fence: Option<&str> = None;
    let mut is_first_line: bool = true;
    for line in body.split(TEXT_LF) {
        let line_len: usize = line.len();
        if is_first_line {
            is_first_line = false;
            if line == MD_FRONTMATTER_YAML || line == MD_FRONTMATTER_TOML {
                let remaining: &str = body.get(line_len + 1..).unwrap_or("");
                if remaining.split(TEXT_LF).any(|l: &str| l == line) {
                    frontmatter_fence = Some(line);
                    line_start += line_len + 1;
                    continue;
                }
            }
        } else if let Some(fence) = frontmatter_fence {
            if line == fence {
                frontmatter_fence = None;
            }
            line_start += line_len + 1;
            continue;
        }
        let (bq_depth, bq_end, indent_cols, stripped): (usize, usize, usize, &str) =
            strip_blockquote_and_measure_indent(line);
        if let Some((fence_ch, fence_len, fence_base)) = active_fence {
            let mut insert_blank: Option<&str> = None;
            if indent_cols < fence_base + 4 && is_closing_fence(stripped, fence_ch, fence_len) {
                if fence_just_opened {
                    insert_blank = Some(line[..bq_end].trim_end_matches([TEXT_SPACE, TEXT_TAB]));
                }
                active_fence = None;
                fence_just_opened = false;
                prev_can_start_indented_code = true;
            } else {
                fence_just_opened = false;
            }
            if let Some(blank_prefix) = insert_blank {
                if let Some(ref mut buf) = out {
                    buf.push(TEXT_LF);
                    buf.push_str(blank_prefix);
                    buf.push(TEXT_LF);
                    buf.push_str(line);
                } else {
                    let mut buf: String = String::with_capacity(input.len() + 16);
                    buf.push_str(&input[..line_start]);
                    buf.push_str(blank_prefix);
                    buf.push(TEXT_LF);
                    buf.push_str(line);
                    out = Some(buf);
                }
            } else if let Some(ref mut buf) = out {
                buf.push(TEXT_LF);
                buf.push_str(line);
            }
            line_start += line_len + 1;
            continue;
        }
        if bq_depth != prev_bq_depth {
            list_indent_stack.clear();
            indented_code_threshold = None;
            prev_can_start_indented_code = true;
            prev_bq_depth = bq_depth;
        }
        let mut rewritten: Cow<'_, str> = Cow::Borrowed(line);
        let in_indented_code: bool =
            indented_code_threshold.is_some_and(|threshold: usize| indent_cols >= threshold);
        if stripped.is_empty() || in_indented_code {
            prev_can_start_indented_code = true;
        } else {
            indented_code_threshold = None;
            while list_indent_stack
                .last()
                .is_some_and(|&col: &usize| indent_cols < col)
            {
                list_indent_stack.pop();
            }
            let base_indent: usize = list_indent_stack.last().copied().unwrap_or(0);
            let rel_indent: usize = indent_cols.saturating_sub(base_indent);
            if rel_indent >= 4 && prev_can_start_indented_code {
                indented_code_threshold = Some(base_indent + 4);
                prev_can_start_indented_code = true;
            } else {
                let (fence_base, fence_candidate): (usize, &str) = if rel_indent < 4
                    && let Some((marker_cols, after)) = parse_list_marker(stripped)
                {
                    let next_col: usize = indent_cols + marker_cols;
                    list_indent_stack.push(next_col);
                    (next_col, after)
                } else {
                    (base_indent, stripped)
                };
                if rel_indent < 4
                    && let Some((fence_ch, fence_len)) = parse_opening_fence(fence_candidate)
                {
                    active_fence = Some((fence_ch, fence_len, fence_base));
                    fence_just_opened = true;
                    prev_can_start_indented_code = false;
                } else {
                    prev_can_start_indented_code = rel_indent < 4
                        && (is_thematic_break_line(stripped)
                            || is_atx_heading_or_dash_break(stripped));
                    if line.contains('"') {
                        let step_def: Cow<'_, str> = normalize_link_def_title(line, stripped);
                        match step_def {
                            Cow::Borrowed(s) => {
                                rewritten = normalize_inline_prettier_line(s);
                            }
                            Cow::Owned(s) => {
                                let final_line: String = match normalize_inline_prettier_line(&s) {
                                    Cow::Borrowed(_) => s,
                                    Cow::Owned(inline_changed) => inline_changed,
                                };
                                rewritten = Cow::Owned(final_line);
                            }
                        }
                    }
                }
            }
        }
        match (&mut out, rewritten) {
            (Some(buf), line_cow) => {
                buf.push(TEXT_LF);
                buf.push_str(&line_cow);
            }
            (None, Cow::Owned(changed)) => {
                let mut buf: String = String::with_capacity(input.len() + 16);
                buf.push_str(&input[..line_start]);
                buf.push_str(&changed);
                out = Some(buf);
            }
            (None, Cow::Borrowed(_)) => {}
        }
        line_start += line_len + 1;
    }
    match out {
        Some(mut buf) => {
            if has_trailing_lf {
                buf.push(TEXT_LF);
            }
            buf
        }
        None => input,
    }
}

/// Strips CommonMark blockquote markers (`>` with 0..=3 leading spaces and optional 1-column space) and measures remaining column indentation.
///
/// # Arguments
///
/// - `&str` - Raw Markdown line without trailing CR.
///
/// # Returns
///
/// - `(usize, usize, usize, &str)` - Blockquote depth, byte offset right after the last `>`, remaining column indentation (tab stop = 4), and content slice.
#[inline(always)]
fn strip_blockquote_and_measure_indent(line: &str) -> (usize, usize, usize, &str) {
    let bytes: &[u8] = line.as_bytes();
    let len: usize = bytes.len();
    let mut idx: usize = 0;
    let mut bq_depth: usize = 0;
    let mut bq_end: usize = 0;
    loop {
        let mut scan: usize = idx;
        let mut ws_cols: usize = 0;
        while scan < len {
            if bytes[scan] == b' ' {
                ws_cols += 1;
                scan += 1;
            } else if bytes[scan] == b'\t' {
                ws_cols += 4 - (ws_cols % 4);
                scan += 1;
            } else {
                break;
            }
        }
        if ws_cols < 4 && scan < len && bytes[scan] == MD_BYTE_GT {
            bq_depth += 1;
            scan += 1;
            bq_end = scan;
            if scan < len && bytes[scan] == b' ' {
                scan += 1;
            }
            idx = scan;
            continue;
        }
        return (bq_depth, bq_end, ws_cols, &line[scan..]);
    }
}

/// Parses a CommonMark unordered (`-`, `*`, `+`) or ordered (`1.`, `1)`) list item marker at the start of `stripped`.
///
/// # Arguments
///
/// - `&str` - Line content after container indentation and blockquote markers.
///
/// # Returns
///
/// - `Option<(usize, &str)>` - Marker content column width and remaining text on the line when `stripped` starts a list item.
#[inline(always)]
fn parse_list_marker(stripped: &str) -> Option<(usize, &str)> {
    let bytes: &[u8] = stripped.as_bytes();
    let len: usize = bytes.len();
    if len == 0 {
        return None;
    }
    let marker_len: usize =
        if bytes[0] == MD_BYTE_DASH || bytes[0] == MD_BYTE_STAR || bytes[0] == MD_BYTE_PLUS {
            1
        } else if bytes[0].is_ascii_digit() {
            let digits: usize = bytes
                .iter()
                .take(9)
                .take_while(|&&b: &&u8| b.is_ascii_digit())
                .count();
            if digits == 0
                || digits >= len
                || (bytes[digits] != MD_BYTE_DOT && bytes[digits] != MD_BYTE_RPAREN)
            {
                return None;
            }
            digits + 1
        } else {
            return None;
        };
    if marker_len == len {
        return Some((marker_len + 1, ""));
    }
    if bytes[marker_len] != b' ' && bytes[marker_len] != b'\t' {
        return None;
    }
    let mut scan: usize = marker_len;
    let mut ws_cols: usize = 0;
    while scan < len {
        if bytes[scan] == b' ' {
            ws_cols += 1;
            scan += 1;
        } else if bytes[scan] == b'\t' {
            ws_cols += 4 - ((marker_len + ws_cols) % 4);
            scan += 1;
        } else {
            break;
        }
    }
    if scan == len || ws_cols >= 5 {
        return Some((marker_len + 1, ""));
    }
    Some((marker_len + ws_cols, &stripped[scan..]))
}

/// Returns `true` when `stripped` is an ATX heading (`#`..`######`) or a `-` thematic break that allows an indented code block on the next line.
///
/// # Arguments
///
/// - `&str` - Line slice after container indentation and blockquote markers.
///
/// # Returns
///
/// - `bool` - Whether the line terminates any paragraph and permits a following indented code block.
#[inline(always)]
fn is_atx_heading_or_dash_break(stripped: &str) -> bool {
    let bytes: &[u8] = stripped.as_bytes();
    if bytes.is_empty() {
        return false;
    }
    if bytes[0] == MD_BYTE_HASH {
        let hashes: usize = bytes
            .iter()
            .take_while(|&&b: &&u8| b == MD_BYTE_HASH)
            .count();
        if (1..=6).contains(&hashes) {
            return hashes == bytes.len() || bytes[hashes] == b' ' || bytes[hashes] == b'\t';
        }
        return false;
    }
    if bytes[0] != MD_BYTE_DASH {
        return false;
    }
    let mut dashes: usize = 0;
    for &b in bytes {
        if b == b' ' || b == b'\t' {
            continue;
        }
        if b != MD_BYTE_DASH {
            return false;
        }
        dashes += 1;
    }
    dashes >= 3
}

/// Parses an opening fenced code block delimiter (`` ``` `` or `~~~`) on an unquoted line slice.
///
/// # Arguments
///
/// - `&str` - Line slice after container indentation and blockquote markers.
///
/// # Returns
///
/// - `Option<(char, usize)>` - Fence character and delimiter length if an opening fence starts here.
#[inline(always)]
fn parse_opening_fence(stripped: &str) -> Option<(char, usize)> {
    let bytes: &[u8] = stripped.as_bytes();
    let first: u8 = *bytes.first()?;
    if first != MD_BYTE_BACKTICK && first != MD_BYTE_TILDE {
        return None;
    }
    let len: usize = bytes.iter().take_while(|&&b: &&u8| b == first).count();
    if len < 3 {
        return None;
    }
    if first == MD_BYTE_BACKTICK && bytes[len..].contains(&MD_BYTE_BACKTICK) {
        return None;
    }
    Some((first as char, len))
}

/// Returns `true` when `stripped` is a valid closing fence matching `(fence_ch, fence_len)`.
///
/// # Arguments
///
/// - `&str` - Line slice after container indentation and blockquote markers.
/// - `char` - Expected fence character (`` ` `` or `~`).
/// - `usize` - Minimum closing fence length.
///
/// # Returns
///
/// - `bool` - Whether this line closes the active fenced code block.
#[inline(always)]
fn is_closing_fence(stripped: &str, fence_ch: char, fence_len: usize) -> bool {
    let trimmed: &[u8] = stripped.trim_end_matches([TEXT_SPACE, TEXT_TAB]).as_bytes();
    let target: u8 = fence_ch as u8;
    let count: usize = trimmed.iter().take_while(|&&b: &&u8| b == target).count();
    count >= fence_len && count == trimmed.len()
}

/// Converts a double-quoted title on a CommonMark link reference definition (`[label]: dest "title"`) to single quotes under `singleQuote: true`.
///
/// # Arguments
///
/// - `&'a str` - Non-code Markdown line.
/// - `&str` - Line slice already stripped of blockquote markers and indentation.
///
/// # Returns
///
/// - `Cow<'a, str>` - Borrowed line when unchanged, or normalized line with single-quoted definition title.
fn normalize_link_def_title<'a>(line: &'a str, trimmed: &str) -> Cow<'a, str> {
    if !trimmed.starts_with('[') || trimmed.starts_with("[^") {
        return Cow::Borrowed(line);
    }
    let prefix_len: usize = line.len() - trimmed.len();
    let bytes: &[u8] = trimmed.as_bytes();
    let len: usize = bytes.len();
    let mut i: usize = 1;
    while i < len {
        if bytes[i] == b'\\' && i + 1 < len {
            i += 2;
            continue;
        }
        if bytes[i] == MD_BYTE_RBRACKET {
            break;
        }
        i += 1;
    }
    if i + 1 >= len || bytes[i] != MD_BYTE_RBRACKET || bytes[i + 1] != MD_BYTE_COLON {
        return Cow::Borrowed(line);
    }
    i += 2;
    while i < len && (bytes[i] == b' ' || bytes[i] == b'\t') {
        i += 1;
    }
    let Some(after_dest) = scan_link_destination(bytes, i) else {
        return Cow::Borrowed(line);
    };
    let mut ws: usize = after_dest;
    while ws < len && (bytes[ws] == b' ' || bytes[ws] == b'\t') {
        ws += 1;
    }
    if ws == after_dest || ws >= len || bytes[ws] != MD_BYTE_DQUOTE {
        return Cow::Borrowed(line);
    }
    let Some(close_quote) = scan_double_quoted_title(bytes, ws) else {
        return Cow::Borrowed(line);
    };
    if !trimmed[(close_quote + 1)..]
        .trim_matches([TEXT_SPACE, TEXT_TAB])
        .is_empty()
    {
        return Cow::Borrowed(line);
    }
    let inner: &str = &trimmed[(ws + 1)..close_quote];
    let Some((open_ch, converted, close_ch)) = convert_double_title_to_prettier(inner) else {
        return Cow::Borrowed(line);
    };
    let mut out: String = String::with_capacity(line.len());
    out.push_str(&line[..(prefix_len + ws)]);
    out.push(open_ch);
    out.push_str(&converted);
    out.push(close_ch);
    out.push_str(&trimmed[(close_quote + 1)..]);
    Cow::Owned(out)
}

/// Normalizes inline link/image `"title"` quotes outside inline code spans to match Prettier `singleQuote: true`.
///
/// # Arguments
///
/// - `&str` - Single non-code Markdown line.
///
/// # Returns
///
/// - `Cow<'_, str>` - Borrowed line when unchanged, or normalized Markdown line.
fn normalize_inline_prettier_line(line: &str) -> Cow<'_, str> {
    if !line.contains("](") || !line.contains('"') {
        return Cow::Borrowed(line);
    }
    let bytes: &[u8] = line.as_bytes();
    let len: usize = bytes.len();
    let mut out: Option<String> = None;
    let mut i: usize = 0;
    while i < len {
        if bytes[i] == b'\\' && i + 1 < len {
            let next_len: usize = if bytes[i + 1] < 0x80 {
                1
            } else {
                line[(i + 1)..]
                    .chars()
                    .next()
                    .map(char::len_utf8)
                    .unwrap_or(1)
            };
            if let Some(ref mut buf) = out {
                buf.push_str(&line[i..(i + 1 + next_len)]);
            }
            i += 1 + next_len;
            continue;
        }
        if bytes[i] == MD_BYTE_BACKTICK {
            let end_code: usize = scan_inline_code_span(bytes, i);
            if let Some(ref mut buf) = out {
                buf.push_str(&line[i..end_code]);
            }
            i = end_code;
            continue;
        }
        if bytes[i] == MD_BYTE_RBRACKET
            && i + 1 < len
            && bytes[i + 1] == MD_BYTE_LPAREN
            && let Some((replacement, next_i)) = try_rewrite_inline_link_title(line, i)
        {
            let buf: &mut String = out.get_or_insert_with(|| {
                let mut s: String = String::with_capacity(len);
                s.push_str(&line[..i]);
                s
            });
            buf.push_str(&replacement);
            i = next_i;
            continue;
        }
        let ch_len: usize = if bytes[i] < 0x80 {
            1
        } else {
            line[i..].chars().next().map(char::len_utf8).unwrap_or(1)
        };
        if let Some(ref mut buf) = out {
            buf.push_str(&line[i..(i + ch_len)]);
        }
        i += ch_len;
    }
    match out {
        Some(buf) => Cow::Owned(buf),
        None => Cow::Borrowed(line),
    }
}

/// Scans past a CommonMark multi-backtick inline code span starting at `start`.
///
/// # Arguments
///
/// - `&[u8]` - Line bytes.
/// - `usize` - Byte offset of the opening backtick.
///
/// # Returns
///
/// - `usize` - Byte offset immediately after the closing backtick run (or opening run if unclosed).
fn scan_inline_code_span(bytes: &[u8], start: usize) -> usize {
    let len: usize = bytes.len();
    let ticks: usize = bytes[start..]
        .iter()
        .take_while(|&&b: &&u8| b == MD_BYTE_BACKTICK)
        .count();
    let mut i: usize = start + ticks;
    while i < len {
        if bytes[i] == MD_BYTE_BACKTICK {
            let run: usize = bytes[i..]
                .iter()
                .take_while(|&&b: &&u8| b == MD_BYTE_BACKTICK)
                .count();
            if run == ticks {
                return i + run;
            }
            i += run;
            continue;
        }
        i += 1;
    }
    start + ticks
}

/// Attempts to rewrite an inline link or image `](dest "title")` title to Prettier `singleQuote: true` quoting.
///
/// # Arguments
///
/// - `&str` - Full line string.
/// - `usize` - Byte offset of `]` immediately preceding `(`.
///
/// # Returns
///
/// - `Option<(String, usize)>` - Rewritten `](...)` slice and byte index after closing `)`, if rewritten.
fn try_rewrite_inline_link_title(line: &str, bracket_idx: usize) -> Option<(String, usize)> {
    let bytes: &[u8] = line.as_bytes();
    let len: usize = bytes.len();
    let mut i: usize = bracket_idx + 2;
    while i < len && (bytes[i] == b' ' || bytes[i] == b'\t') {
        i += 1;
    }
    let after_dest: usize = scan_link_destination(bytes, i)?;
    let mut ws: usize = after_dest;
    while ws < len && (bytes[ws] == b' ' || bytes[ws] == b'\t') {
        ws += 1;
    }
    if ws == after_dest || ws >= len || bytes[ws] != MD_BYTE_DQUOTE {
        return None;
    }
    let close_quote: usize = scan_double_quoted_title(bytes, ws)?;
    let mut tail: usize = close_quote + 1;
    while tail < len && (bytes[tail] == b' ' || bytes[tail] == b'\t') {
        tail += 1;
    }
    if tail >= len || bytes[tail] != MD_BYTE_RPAREN {
        return None;
    }
    let inner: &str = &line[(ws + 1)..close_quote];
    let (open_ch, converted, close_ch): (char, String, char) =
        convert_double_title_to_prettier(inner)?;
    let mut out: String = String::with_capacity(tail + 1 - bracket_idx);
    out.push_str(&line[bracket_idx..ws]);
    out.push(open_ch);
    out.push_str(&converted);
    out.push(close_ch);
    out.push_str(&line[(close_quote + 1)..=tail]);
    Some((out, tail + 1))
}

/// Scans a CommonMark link destination (`<...>` or balanced-paren non-whitespace token) starting at `start`.
///
/// # Arguments
///
/// - `&[u8]` - Line bytes.
/// - `usize` - Starting byte offset of the destination.
///
/// # Returns
///
/// - `Option<usize>` - Byte offset immediately after the link destination.
fn scan_link_destination(bytes: &[u8], start: usize) -> Option<usize> {
    let len: usize = bytes.len();
    if start >= len {
        return None;
    }
    if bytes[start] == MD_BYTE_LT {
        let mut i: usize = start + 1;
        while i < len {
            if bytes[i] == b'\\' && i + 1 < len {
                i += 2;
                continue;
            }
            if bytes[i] == MD_BYTE_GT {
                return Some(i + 1);
            }
            if bytes[i] == MD_BYTE_LT {
                return None;
            }
            i += 1;
        }
        return None;
    }
    let mut i: usize = start;
    let mut paren_depth: usize = 0;
    while i < len {
        let b: u8 = bytes[i];
        if b == b'\\' && i + 1 < len {
            i += 2;
            continue;
        }
        if b == b' ' || b == b'\t' {
            break;
        }
        if b == MD_BYTE_LPAREN {
            paren_depth += 1;
        } else if b == MD_BYTE_RPAREN {
            if paren_depth == 0 {
                break;
            }
            paren_depth -= 1;
        }
        i += 1;
    }
    if i == start || paren_depth != 0 {
        return None;
    }
    Some(i)
}

/// Scans a double-quoted Markdown link title starting at `open_quote`.
///
/// # Arguments
///
/// - `&[u8]` - Line bytes.
/// - `usize` - Byte offset of opening `"`.
///
/// # Returns
///
/// - `Option<usize>` - Byte offset of matching closing `"`.
fn scan_double_quoted_title(bytes: &[u8], open_quote: usize) -> Option<usize> {
    let len: usize = bytes.len();
    let mut i: usize = open_quote + 1;
    while i < len {
        if bytes[i] == b'\\' && i + 1 < len {
            i += 2;
            continue;
        }
        if bytes[i] == MD_BYTE_DQUOTE {
            return Some(i);
        }
        i += 1;
    }
    None
}

/// Converts the interior of a double-quoted Markdown title to Prettier `singleQuote: true` delimiters.
///
/// Uses single quotes `'...'` whenever no `'` appears (unescaping `\"` to `"`), or parentheses `(...)` when both `'` and `\"` appear without parentheses; otherwise preserves `"..."`.
///
/// # Arguments
///
/// - `&str` - Raw inner slice between `"` delimiters.
///
/// # Returns
///
/// - `Option<(char, String, char)>` - Opening delimiter, normalized title content, and closing delimiter, or `None` to keep double quotes.
fn convert_double_title_to_prettier(inner: &str) -> Option<(char, String, char)> {
    let has_single: bool = inner.contains('\'');
    let has_escaped_double: bool = inner.contains('"');
    let (open_ch, close_ch): (char, char) = if !has_single {
        ('\'', '\'')
    } else if has_escaped_double && !inner.contains('(') && !inner.contains(')') {
        ('(', ')')
    } else {
        return None;
    };
    let bytes: &[u8] = inner.as_bytes();
    let len: usize = bytes.len();
    let mut out: String = String::with_capacity(len);
    let mut i: usize = 0;
    while i < len {
        if bytes[i] == b'\\' && i + 1 < len {
            if bytes[i + 1] == MD_BYTE_DQUOTE {
                out.push('"');
            } else {
                out.push('\\');
                let next_len: usize = if bytes[i + 1] < 0x80 {
                    1
                } else {
                    inner[(i + 1)..]
                        .chars()
                        .next()
                        .map(char::len_utf8)
                        .unwrap_or(1)
                };
                out.push_str(&inner[(i + 1)..(i + 1 + next_len)]);
                i += 1 + next_len;
                continue;
            }
            i += 2;
            continue;
        }
        let ch_len: usize = if bytes[i] < 0x80 {
            1
        } else {
            inner[i..].chars().next().map(char::len_utf8).unwrap_or(1)
        };
        out.push_str(&inner[i..(i + ch_len)]);
        i += ch_len;
    }
    Some((open_ch, out, close_ch))
}