mdbook-lint 0.14.1

A fast markdown linter and preprocessor for mdBook
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
use mdbook::book::{Book, BookItem, Chapter};
use mdbook::preprocess::{Preprocessor, PreprocessorContext};

use crate::Config;
#[cfg(test)]
use mdbook_lint_core::RuleCategory;
use mdbook_lint_core::{
    Document, LintEngine, MdBookLintError, PluginRegistry, Severity, Violation,
};
#[cfg(feature = "content")]
use mdbook_lint_rulesets::ContentRuleProvider;
use mdbook_lint_rulesets::{MdBookRuleProvider, StandardRuleProvider};
use serde_json::Value;
use std::io::{self, Read};
use std::path::PathBuf;

/// mdbook-lint preprocessor
pub struct MdBookLint {
    /// Linting engine with combined rules
    pub engine: LintEngine,
    /// Configuration options
    pub config: Config,
    /// Book source directory (set from PreprocessorContext in preprocessor mode)
    book_src_dir: Option<PathBuf>,
}

impl MdBookLint {
    /// Create a new preprocessor with default rules and config
    pub fn new() -> Self {
        let mut registry = PluginRegistry::new();
        registry
            .register_provider(Box::new(StandardRuleProvider))
            .expect("Failed to register standard rules");
        registry
            .register_provider(Box::new(MdBookRuleProvider))
            .expect("Failed to register mdbook rules");
        #[cfg(feature = "content")]
        registry
            .register_provider(Box::new(ContentRuleProvider))
            .expect("Failed to register content rules");
        let engine = registry.create_engine().expect("Failed to create engine");

        Self {
            config: Config::default(),
            engine,
            book_src_dir: None,
        }
    }

    /// Create a new preprocessor with custom config
    #[allow(dead_code)]
    pub fn with_config(config: Config) -> Self {
        let mut registry = PluginRegistry::new();
        registry
            .register_provider(Box::new(StandardRuleProvider))
            .expect("Failed to register standard rules");
        registry
            .register_provider(Box::new(MdBookRuleProvider))
            .expect("Failed to register mdbook rules");
        #[cfg(feature = "content")]
        registry
            .register_provider(Box::new(ContentRuleProvider))
            .expect("Failed to register content rules");
        let engine = registry.create_engine().expect("Failed to create engine");

        Self {
            config,
            engine,
            book_src_dir: None,
        }
    }

    /// Load configuration from preprocessor context
    ///
    /// Configuration is loaded from multiple sources with the following precedence
    /// (later sources override earlier ones):
    /// 1. Default configuration
    /// 2. `.mdbook-lint.toml` file (if found in book root or parent directories)
    /// 3. `[preprocessor.lint]` section in `book.toml`
    pub fn load_config_from_context(
        &mut self,
        ctx: &PreprocessorContext,
    ) -> mdbook_lint_core::Result<()> {
        let book_root = &ctx.root;

        // Compute the book source directory from context
        // This is root + book.src (which defaults to "src")
        let src_dir_name = ctx.config.book.src.to_str().unwrap_or("src").to_string();
        self.book_src_dir = Some(book_root.join(&src_dir_name));

        // First, try to discover and load .mdbook-lint.toml config file
        if let Some(discovered_path) = Config::discover_config(Some(book_root)) {
            self.config = Config::from_file(&discovered_path)?;
        }

        // Then, merge with book.toml preprocessor config (takes precedence)
        let preprocessor_config = ctx
            .config
            .get_preprocessor("mdbook-lint")
            .or_else(|| ctx.config.get_preprocessor("lint"));

        if let Some(config) = preprocessor_config {
            let book_toml_config = parse_mdbook_config(config)?;
            self.config.merge(book_toml_config);
        }

        // Recreate the engine with the loaded configuration
        let mut registry = PluginRegistry::new();
        registry
            .register_provider(Box::new(StandardRuleProvider))
            .expect("Failed to register standard rules");
        registry
            .register_provider(Box::new(MdBookRuleProvider))
            .expect("Failed to register mdbook rules");
        self.engine = registry
            .create_engine_with_config(Some(&self.config.core))
            .expect("Failed to create configured engine");

        Ok(())
    }

    /// Process a chapter and return any violations found
    fn process_chapter(&self, chapter: &Chapter) -> mdbook_lint_core::Result<Vec<Violation>> {
        // Create document from chapter content
        // When running in preprocessor mode, source_path is relative to the book source directory
        // We need to resolve it to an absolute path for rules that check file existence
        let source_path = chapter
            .source_path
            .as_ref()
            .unwrap_or(&PathBuf::from("unknown.md"))
            .clone();

        // If we have a book source directory, resolve the path to absolute
        let resolved_path = if let Some(ref book_src) = self.book_src_dir {
            book_src.join(&source_path)
        } else {
            source_path
        };

        let document = Document::with_book_src_dir(
            chapter.content.clone(),
            resolved_path,
            self.book_src_dir.clone(),
        )?;

        // Use optimized checking (single AST parse) with configuration
        let violations = self
            .engine
            .lint_document_with_config(&document, &self.config.core)?;

        Ok(violations)
    }

    /// Format violations for output
    fn format_violations(&self, violations: &[Violation], chapter_path: &str) -> String {
        if violations.is_empty() {
            return String::new();
        }

        let mut output = String::new();
        for violation in violations {
            output.push_str(&format!(
                "{}:{}:{}: {}\n",
                chapter_path, violation.line, violation.column, violation
            ));
        }
        output
    }

    /// Determine if we should fail the build based on violations
    fn should_fail_build(&self, violations: &[Violation]) -> bool {
        for violation in violations {
            match violation.severity {
                Severity::Error if self.config.fail_on_errors => return true,
                Severity::Warning if self.config.fail_on_warnings => return true,
                _ => {}
            }
        }
        false
    }
}

impl Default for MdBookLint {
    fn default() -> Self {
        Self::new()
    }
}

impl Preprocessor for MdBookLint {
    fn name(&self) -> &str {
        "lint"
    }

    fn run(&self, _ctx: &PreprocessorContext, book: Book) -> mdbook::errors::Result<Book> {
        let mut total_violations = Vec::new();
        let mut should_fail = false;

        // Process each chapter
        for item in book.iter() {
            if let BookItem::Chapter(chapter) = item {
                let violations = self.process_chapter(chapter).map_err(|e| {
                    mdbook::errors::Error::msg(format!("Failed to process chapter: {e}"))
                })?;

                if !violations.is_empty() {
                    let chapter_path = chapter
                        .source_path
                        .as_ref()
                        .map(|p| p.to_string_lossy())
                        .unwrap_or("unknown".into());

                    // Print violations to stderr
                    eprint!("{}", self.format_violations(&violations, &chapter_path));

                    if self.should_fail_build(&violations) {
                        should_fail = true;
                    }

                    total_violations.extend(violations);
                }
            }
        }

        // Print summary
        if !total_violations.is_empty() {
            let error_count = total_violations
                .iter()
                .filter(|v| v.severity == Severity::Error)
                .count();
            let warning_count = total_violations
                .iter()
                .filter(|v| v.severity == Severity::Warning)
                .count();
            let info_count = total_violations
                .iter()
                .filter(|v| v.severity == Severity::Info)
                .count();

            eprintln!(
                "mdbook-lint: {error_count} error(s), {warning_count} warning(s), {info_count} info"
            );

            if should_fail {
                return Err(mdbook::errors::Error::msg(format!(
                    "mdbook-lint: Build failed due to {error_count} error(s) and {warning_count} warning(s)"
                )));
            }
        } else {
            eprintln!("mdbook-lint: No issues found");
        }

        // Return the book unchanged (we're just linting, not modifying)
        Ok(book)
    }

    fn supports_renderer(&self, renderer: &str) -> bool {
        // We support all renderers since we don't modify content
        match renderer {
            "html" | "markdown" | "epub" | "pdf" => true,
            _ => true, // Default to supporting unknown renderers
        }
    }
}

/// Parse preprocessor configuration from mdbook config
fn parse_mdbook_config(config: &toml::value::Table) -> mdbook_lint_core::Result<Config> {
    let mut preprocessor_config = Config::default();

    if let Some(fail_on_warnings) = config.get("fail-on-warnings") {
        preprocessor_config.fail_on_warnings = fail_on_warnings
            .as_bool()
            .ok_or_else(|| MdBookLintError::config_error("fail-on-warnings must be a boolean"))?;
    }

    if let Some(fail_on_errors) = config.get("fail-on-errors") {
        preprocessor_config.fail_on_errors = fail_on_errors
            .as_bool()
            .ok_or_else(|| MdBookLintError::config_error("fail-on-errors must be a boolean"))?;
    }

    if let Some(enabled_categories) = config.get("enabled-categories")
        && let Some(categories_array) = enabled_categories.as_array()
    {
        for category in categories_array {
            if let Some(category_str) = category.as_str() {
                preprocessor_config
                    .core
                    .enabled_categories
                    .push(category_str.to_string());
            }
        }
    }

    if let Some(disabled_categories) = config.get("disabled-categories")
        && let Some(categories_array) = disabled_categories.as_array()
    {
        for category in categories_array {
            if let Some(category_str) = category.as_str() {
                preprocessor_config
                    .core
                    .disabled_categories
                    .push(category_str.to_string());
            }
        }
    }

    if let Some(enabled_rules) = config.get("enabled-rules")
        && let Some(rules_array) = enabled_rules.as_array()
    {
        for rule in rules_array {
            if let Some(rule_str) = rule.as_str() {
                preprocessor_config
                    .core
                    .enabled_rules
                    .push(rule_str.to_string());
            }
        }
    }

    if let Some(disabled_rules) = config.get("disabled-rules")
        && let Some(rules_array) = disabled_rules.as_array()
    {
        for rule in rules_array {
            if let Some(rule_str) = rule.as_str() {
                preprocessor_config
                    .core
                    .disabled_rules
                    .push(rule_str.to_string());
            }
        }
    }

    Ok(preprocessor_config)
}

/// Parse preprocessor configuration from serde_json Value (for tests)
#[allow(dead_code)]
fn parse_config(config: &Value) -> mdbook_lint_core::Result<Config> {
    let mut preprocessor_config = Config::default();

    if let Some(fail_on_warnings) = config.get("fail-on-warnings") {
        preprocessor_config.fail_on_warnings = fail_on_warnings
            .as_bool()
            .ok_or_else(|| MdBookLintError::config_error("fail-on-warnings must be a boolean"))?;
    }

    if let Some(fail_on_errors) = config.get("fail-on-errors") {
        preprocessor_config.fail_on_errors = fail_on_errors
            .as_bool()
            .ok_or_else(|| MdBookLintError::config_error("fail-on-errors must be a boolean"))?;
    }

    if let Some(enabled_categories) = config.get("enabled-categories")
        && let Some(categories_array) = enabled_categories.as_array()
    {
        for category in categories_array {
            if let Some(category_str) = category.as_str() {
                preprocessor_config
                    .core
                    .enabled_categories
                    .push(category_str.to_string());
            }
        }
    }

    if let Some(disabled_categories) = config.get("disabled-categories")
        && let Some(categories_array) = disabled_categories.as_array()
    {
        for category in categories_array {
            if let Some(category_str) = category.as_str() {
                preprocessor_config
                    .core
                    .disabled_categories
                    .push(category_str.to_string());
            }
        }
    }

    if let Some(enabled_rules) = config.get("enabled-rules")
        && let Some(rules_array) = enabled_rules.as_array()
    {
        for rule in rules_array {
            if let Some(rule_str) = rule.as_str() {
                preprocessor_config
                    .core
                    .enabled_rules
                    .push(rule_str.to_string());
            }
        }
    }

    if let Some(disabled_rules) = config.get("disabled-rules")
        && let Some(rules_array) = disabled_rules.as_array()
    {
        for rule in rules_array {
            if let Some(rule_str) = rule.as_str() {
                preprocessor_config
                    .core
                    .disabled_rules
                    .push(rule_str.to_string());
            }
        }
    }

    Ok(preprocessor_config)
}

/// Recursively remove null values from a JSON value, except for specific fields.
///
/// This is necessary because mdbook sends JSON with null values (e.g., `"description": null`),
/// but mdbook's Config struct deserializes through `toml::Value` which doesn't support null.
/// By stripping nulls before deserialization, we avoid the "invalid type: null, expected any
/// valid TOML value" error.
///
/// However, we preserve `__non_exhaustive` fields because mdbook's Book struct requires them
/// for deserialization (they deserialize to `()` which accepts null).
fn strip_null_values(value: Value) -> Value {
    match value {
        Value::Array(arr) => Value::Array(arr.into_iter().map(strip_null_values).collect()),
        Value::Object(obj) => Value::Object(
            obj.into_iter()
                .filter(|(k, v)| !v.is_null() || k == "__non_exhaustive")
                .map(|(k, v)| (k, strip_null_values(v)))
                .collect(),
        ),
        other => other,
    }
}

/// Normalize mdbook JSON format for compatibility between 0.4.x and 0.5.x.
///
/// mdbook 0.5.x made several changes to the JSON format:
/// 1. Book struct's field name changed from `sections` to `items`
/// 2. The `__non_exhaustive` field was removed from the JSON output
///
/// This function converts the 0.5.x format back to 0.4.x format so we can deserialize
/// using the mdbook 0.4.x types we depend on.
///
/// The input is expected to be a JSON array: `[PreprocessorContext, Book]`
/// Returns (normalized_value, is_mdbook_05) tuple.
fn normalize_mdbook_json(value: Value) -> (Value, bool) {
    let is_05 = detect_mdbook_05_format(&value);
    let normalized = normalize_mdbook_json_inner(value);
    (normalized, is_05)
}

/// Detect if the JSON is in mdbook 0.5.x format (uses "items" instead of "sections")
fn detect_mdbook_05_format(value: &Value) -> bool {
    if let Value::Array(arr) = value {
        // The Book object is the second element in the array
        if let Some(book) = arr.get(1) {
            return book.get("items").is_some() && book.get("sections").is_none();
        }
    }
    false
}

/// Inner normalization function that converts items -> sections
fn normalize_mdbook_json_inner(value: Value) -> Value {
    match value {
        Value::Array(arr) => {
            Value::Array(arr.into_iter().map(normalize_mdbook_json_inner).collect())
        }
        Value::Object(mut obj) => {
            // If this object has "items" but not "sections", it's a mdbook 0.5.x Book
            // We need to:
            // 1. Rename "items" to "sections"
            // 2. Add "__non_exhaustive": null (required by mdbook 0.4.x deserialization)
            if obj.contains_key("items")
                && !obj.contains_key("sections")
                && let Some(items) = obj.remove("items")
            {
                obj.insert("sections".to_string(), normalize_mdbook_json_inner(items));
                // Add __non_exhaustive if not present (mdbook 0.5.x removed it)
                if !obj.contains_key("__non_exhaustive") {
                    obj.insert("__non_exhaustive".to_string(), Value::Null);
                }
            }

            // Recursively normalize nested objects
            Value::Object(
                obj.into_iter()
                    .map(|(k, v)| (k, normalize_mdbook_json_inner(v)))
                    .collect(),
            )
        }
        other => other,
    }
}

/// Denormalize output for mdbook 0.5.x compatibility.
///
/// Converts "sections" back to "items" and removes "__non_exhaustive" fields
/// for mdbook 0.5.x compatibility.
fn denormalize_for_mdbook_05(value: Value) -> Value {
    match value {
        Value::Array(arr) => Value::Array(arr.into_iter().map(denormalize_for_mdbook_05).collect()),
        Value::Object(mut obj) => {
            // Remove __non_exhaustive (mdbook 0.5.x doesn't use it)
            obj.remove("__non_exhaustive");

            // If this object has "sections" but not "items", rename to "items"
            if obj.contains_key("sections")
                && !obj.contains_key("items")
                && let Some(sections) = obj.remove("sections")
            {
                obj.insert("items".to_string(), denormalize_for_mdbook_05(sections));
            }

            // Recursively denormalize nested objects
            Value::Object(
                obj.into_iter()
                    .map(|(k, v)| (k, denormalize_for_mdbook_05(v)))
                    .collect(),
            )
        }
        other => other,
    }
}

/// Handle the preprocessor protocol (stdin/stdout communication with mdbook)
pub fn handle_preprocessing() -> mdbook_lint_core::Result<()> {
    let mut stdin = io::stdin();
    let mut input = String::new();
    stdin
        .read_to_string(&mut input)
        .map_err(MdBookLintError::Io)?;

    // Parse as generic JSON first, then normalize and clean before deserializing.
    // 1. Normalize: Convert mdbook 0.5.x format (items) to 0.4.x format (sections)
    // 2. Strip nulls: Remove null values that toml::Value can't handle
    let json_value: Value = serde_json::from_str(&input).map_err(MdBookLintError::Json)?;
    let (normalized, is_mdbook_05) = normalize_mdbook_json(json_value);
    let cleaned = strip_null_values(normalized);

    let (ctx, book): (PreprocessorContext, Book) =
        serde_json::from_value(cleaned).map_err(MdBookLintError::Json)?;

    let mut preprocessor = MdBookLint::new();
    preprocessor.load_config_from_context(&ctx)?;

    let processed_book = preprocessor
        .run(&ctx, book)
        .map_err(|e| MdBookLintError::document_error(format!("Preprocessor failed: {e}")))?;

    // Serialize the book back to JSON
    let output_value = serde_json::to_value(&processed_book).map_err(MdBookLintError::Json)?;

    // If input was mdbook 0.5.x format, convert output back to 0.5.x format
    let final_output = if is_mdbook_05 {
        denormalize_for_mdbook_05(output_value)
    } else {
        output_value
    };

    let output = serde_json::to_string(&final_output).map_err(MdBookLintError::Json)?;

    print!("{output}");
    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;
    use mdbook::book::Chapter;

    use serde_json::json;
    use std::path::PathBuf;

    #[test]
    fn test_preprocessor_name() {
        let preprocessor = MdBookLint::new();
        assert_eq!(preprocessor.name(), "lint");
    }

    #[test]
    fn test_supports_renderer() {
        let preprocessor = MdBookLint::new();
        assert!(preprocessor.supports_renderer("html"));
        assert!(preprocessor.supports_renderer("markdown"));
        assert!(preprocessor.supports_renderer("epub"));
        assert!(preprocessor.supports_renderer("pdf"));
        assert!(preprocessor.supports_renderer("custom"));
    }

    #[test]
    fn test_process_chapter_clean() {
        let preprocessor = MdBookLint::new();
        // Content needs 50+ words to pass CONTENT003 (short chapter detection)
        // and lines under 80 chars to pass MD013
        let content = "# Test Chapter

This is a clean chapter with enough content to pass all linting rules.
We need to write several sentences here to make sure we have at least
fifty words in total for the content quality checks.

Let me add some more text to ensure we definitely pass the minimum word
count threshold that is required by the linter for content validation.
";
        let chapter = Chapter::new(
            "Test Chapter",
            content.to_string(),
            PathBuf::from("test.md"),
            Vec::new(),
        );

        let violations = preprocessor.process_chapter(&chapter).unwrap();
        assert_eq!(violations.len(), 0);
    }

    #[test]
    fn test_process_chapter_with_violations() {
        let preprocessor = MdBookLint::new();
        let content = "# Level 1\n### Level 3 - skipped level 2\n".to_string();
        let chapter = Chapter::new(
            "Test Chapter",
            content,
            PathBuf::from("test.md"),
            Vec::new(),
        );

        let violations = preprocessor.process_chapter(&chapter).unwrap();
        assert!(!violations.is_empty());

        // Print violations for debugging
        println!("Found {} violations:", violations.len());
        for (i, violation) in violations.iter().enumerate() {
            println!(
                "  {}: {} (line {}) - {}",
                i, violation.rule_id, violation.line, violation.message
            );
        }

        // Test should not depend on specific ordering - just verify MD001 is present
        let md001_violations: Vec<_> = violations.iter().filter(|v| v.rule_id == "MD001").collect();
        assert!(
            !md001_violations.is_empty(),
            "Should have at least one MD001 violation"
        );
    }

    #[test]
    fn test_rule_filtering_default() {
        let mut registry = PluginRegistry::new();
        registry
            .register_provider(Box::new(StandardRuleProvider))
            .unwrap();
        registry
            .register_provider(Box::new(MdBookRuleProvider))
            .unwrap();
        let engine = registry.create_engine().unwrap();
        let config = Config::default();
        let enabled_rules = engine.enabled_rules(&config.core);

        // Default config should enable all non-deprecated rules
        let enabled_rule_ids: Vec<&str> = enabled_rules.iter().map(|r| r.id()).collect();
        assert!(enabled_rule_ids.contains(&"MD001"));
        assert!(enabled_rule_ids.contains(&"MD013"));
        assert!(!enabled_rule_ids.contains(&"MD002")); // MD002 is deprecated
    }

    #[test]
    fn test_rule_filtering_with_enabled_rules() {
        let mut registry = PluginRegistry::new();
        registry
            .register_provider(Box::new(StandardRuleProvider))
            .unwrap();
        registry
            .register_provider(Box::new(MdBookRuleProvider))
            .unwrap();
        let engine = registry.create_engine().unwrap();
        let config = Config {
            core: mdbook_lint_core::Config {
                enabled_rules: vec!["MD001".to_string(), "MD002".to_string()],
                ..Default::default()
            },
            ..Default::default()
        };
        let enabled_rules = engine.enabled_rules(&config.core);

        let enabled_rule_ids: Vec<&str> = enabled_rules.iter().map(|r| r.id()).collect();
        assert!(enabled_rule_ids.contains(&"MD001"));
        assert!(enabled_rule_ids.contains(&"MD002")); // Explicitly enabled deprecated rule
        assert_eq!(enabled_rule_ids.len(), 2);
    }

    #[test]
    fn test_rule_filtering_with_disabled_rules() {
        let mut registry = PluginRegistry::new();
        registry
            .register_provider(Box::new(StandardRuleProvider))
            .unwrap();
        registry
            .register_provider(Box::new(MdBookRuleProvider))
            .unwrap();
        let engine = registry.create_engine().unwrap();
        let config = Config {
            core: mdbook_lint_core::Config {
                disabled_rules: vec!["MD001".to_string()],
                ..Default::default()
            },
            ..Default::default()
        };
        let enabled_rules = engine.enabled_rules(&config.core);

        let enabled_rule_ids: Vec<&str> = enabled_rules.iter().map(|r| r.id()).collect();
        assert!(!enabled_rule_ids.contains(&"MD001"));
        assert!(enabled_rule_ids.contains(&"MD013"));
    }

    #[test]
    fn test_with_config_constructor() {
        let config = Config {
            fail_on_warnings: true,
            core: mdbook_lint_core::Config {
                enabled_rules: vec!["MD001".to_string()],
                ..Default::default()
            },
            ..Default::default()
        };

        let preprocessor = MdBookLint::with_config(config);
        assert!(preprocessor.config.fail_on_warnings);
        assert_eq!(
            preprocessor.config.core.enabled_rules,
            vec!["MD001".to_string()]
        );
    }

    #[test]
    fn test_process_chapter_with_empty_content() {
        let preprocessor = MdBookLint::new();
        let chapter = Chapter::new(
            "Empty Chapter",
            String::new(),
            PathBuf::from("empty.md"),
            Vec::new(),
        );

        let result = preprocessor.process_chapter(&chapter);
        // Processing empty content should not crash
        assert!(result.is_ok(), "Processing empty content should succeed");
    }

    #[test]
    fn test_process_chapter_with_whitespace_only() {
        let preprocessor = MdBookLint::new();
        let chapter = Chapter::new(
            "Whitespace Chapter",
            "   \n  \n  ".to_string(),
            PathBuf::from("whitespace.md"),
            Vec::new(),
        );

        let result = preprocessor.process_chapter(&chapter);
        // Processing whitespace-only content should not crash
        assert!(
            result.is_ok(),
            "Processing whitespace-only content should succeed"
        );
    }

    #[test]
    fn test_process_chapter_with_custom_config() {
        let config = Config {
            core: mdbook_lint_core::Config {
                disabled_rules: vec!["MD001".to_string()],
                ..Default::default()
            },
            ..Default::default()
        };
        let preprocessor = MdBookLint::with_config(config);

        let content = "# Level 1\n### Level 3 - skipped level 2\n".to_string();
        let chapter = Chapter::new(
            "Test Chapter",
            content,
            PathBuf::from("test.md"),
            Vec::new(),
        );

        let violations = preprocessor.process_chapter(&chapter).unwrap();
        // MD001 should be disabled, so no violations for header level skipping
        let md001_violations: Vec<_> = violations.iter().filter(|v| v.rule_id == "MD001").collect();
        assert_eq!(md001_violations.len(), 0);
    }

    #[test]
    fn test_process_chapter_error_handling() {
        let preprocessor = MdBookLint::new();
        let chapter = Chapter::new(
            "Test Chapter",
            "# Valid content".to_string(),
            PathBuf::from("test.md"),
            Vec::new(),
        );

        // This should not panic or error
        let result = preprocessor.process_chapter(&chapter);
        assert!(result.is_ok());
    }

    #[test]
    fn test_rule_filtering_with_categories() {
        let mut registry = PluginRegistry::new();
        registry
            .register_provider(Box::new(StandardRuleProvider))
            .unwrap();
        registry
            .register_provider(Box::new(MdBookRuleProvider))
            .unwrap();
        let engine = registry.create_engine().unwrap();
        let config = Config {
            core: mdbook_lint_core::Config {
                enabled_categories: vec!["structure".to_string()],
                ..Default::default()
            },
            ..Default::default()
        };
        let enabled_rules = engine.enabled_rules(&config.core);

        let enabled_rule_ids: Vec<&str> = enabled_rules.iter().map(|r| r.id()).collect();
        // Should include structure rules like MD001
        assert!(enabled_rule_ids.contains(&"MD001"));
        // Should not include non-structure rules
        let structure_rules = enabled_rules
            .iter()
            .filter(|r| matches!(r.metadata().category, RuleCategory::Structure))
            .count();
        assert!(structure_rules > 0);
    }

    #[test]
    fn test_rule_filtering_with_disabled_categories() {
        let mut registry = PluginRegistry::new();
        registry
            .register_provider(Box::new(StandardRuleProvider))
            .unwrap();
        registry
            .register_provider(Box::new(MdBookRuleProvider))
            .unwrap();
        let engine = registry.create_engine().unwrap();
        let config = Config {
            core: mdbook_lint_core::Config {
                disabled_categories: vec!["style".to_string()],
                ..Default::default()
            },
            ..Default::default()
        };
        let enabled_rules = engine.enabled_rules(&config.core);

        let enabled_rule_ids: Vec<&str> = enabled_rules.iter().map(|r| r.id()).collect();
        // Should include non-style rules like MD001
        assert!(enabled_rule_ids.contains(&"MD001"));
        // Should exclude style rules - check that some style rules are disabled
        let style_rules = enabled_rules
            .iter()
            .filter(|r| matches!(r.metadata().category, RuleCategory::Formatting))
            .count();
        // There should be fewer formatting rules enabled when the category is disabled
        assert!(style_rules < 50); // Should have most rules still enabled
    }

    #[test]
    fn test_rule_filtering_with_disabled_rules_comprehensive() {
        let mut registry = PluginRegistry::new();
        registry
            .register_provider(Box::new(StandardRuleProvider))
            .unwrap();
        registry
            .register_provider(Box::new(MdBookRuleProvider))
            .unwrap();
        let engine = registry.create_engine().unwrap();
        let config = Config {
            core: mdbook_lint_core::Config {
                disabled_rules: vec!["MD013".to_string()],
                ..Default::default()
            },
            ..Default::default()
        };
        let enabled_rules = engine.enabled_rules(&config.core);

        let enabled_rule_ids: Vec<&str> = enabled_rules.iter().map(|r| r.id()).collect();
        assert!(enabled_rule_ids.contains(&"MD001"));
        assert!(!enabled_rule_ids.contains(&"MD013")); // Explicitly disabled
        assert!(enabled_rule_ids.len() > 50); // Should still have most rules
    }

    #[test]
    fn test_parse_config() {
        let config_json = json!({
            "fail-on-warnings": true,
            "fail-on-errors": false,
            "enabled-rules": ["MD001", "MD013"],
            "disabled-rules": ["MD002"]
        });

        let config = parse_config(&config_json).unwrap();
        assert!(config.fail_on_warnings);
        assert!(!config.fail_on_errors);
        assert_eq!(config.core.enabled_rules, vec!["MD001", "MD013"]);
        assert_eq!(config.core.disabled_rules, vec!["MD002"]);
    }

    #[test]
    fn test_should_fail_build() {
        let config = Config {
            fail_on_warnings: false,
            fail_on_errors: true,
            ..Default::default()
        };
        let preprocessor = MdBookLint::with_config(config);

        // Test with warning - should NOT fail build
        let warning_violations = vec![Violation {
            rule_id: "MD001".to_string(),
            rule_name: "test".to_string(),
            message: "test".to_string(),
            line: 1,
            column: 1,
            severity: Severity::Warning,
            fix: None,
        }];
        assert!(!preprocessor.should_fail_build(&warning_violations));

        // Test with error - should fail build
        let error_violations = vec![Violation {
            rule_id: "MD001".to_string(),
            rule_name: "test".to_string(),
            message: "test".to_string(),
            line: 1,
            column: 1,
            severity: Severity::Error,
            fix: None,
        }];
        assert!(preprocessor.should_fail_build(&error_violations));
    }

    #[test]
    fn test_format_violations() {
        let preprocessor = MdBookLint::new();
        let violations = vec![Violation {
            rule_id: "MD001".to_string(),
            rule_name: "heading-increment".to_string(),
            message: "Test violation".to_string(),
            line: 2,
            column: 1,
            severity: Severity::Error,
            fix: None,
        }];

        let output = preprocessor.format_violations(&violations, "test.md");
        assert!(output.contains("test.md:2:1"));
        assert!(output.contains("MD001"));
        assert!(output.contains("Test violation"));
    }

    #[test]
    fn test_strip_null_values() {
        // Test that null values are stripped from JSON objects
        let input = json!({
            "title": "My Book",
            "description": null,
            "authors": ["Author"],
            "nested": {
                "field": "value",
                "null_field": null
            },
            "array_with_nulls": [1, null, 3]
        });

        let result = strip_null_values(input);

        // Top-level null should be removed
        assert!(result.get("title").is_some());
        assert!(result.get("description").is_none());
        assert!(result.get("authors").is_some());

        // Nested null should be removed
        let nested = result.get("nested").unwrap();
        assert!(nested.get("field").is_some());
        assert!(nested.get("null_field").is_none());

        // Nulls in arrays are kept (arrays preserve structure)
        let arr = result.get("array_with_nulls").unwrap().as_array().unwrap();
        assert_eq!(arr.len(), 3);
    }

    #[test]
    fn test_strip_null_values_preserves_non_exhaustive() {
        // __non_exhaustive fields should be preserved even when null
        let input = json!({
            "sections": [],
            "__non_exhaustive": null
        });

        let result = strip_null_values(input);

        assert!(result.get("sections").is_some());
        assert!(result.get("__non_exhaustive").is_some());
    }

    #[test]
    fn test_strip_null_values_preserves_valid_data() {
        let input = json!({
            "string": "hello",
            "number": 42,
            "bool": true,
            "array": [1, 2, 3],
            "object": {"key": "value"}
        });

        let result = strip_null_values(input.clone());

        // All non-null values should be preserved
        assert_eq!(result, input);
    }

    #[test]
    fn test_normalize_mdbook_json_v04_format() {
        // mdbook 0.4.x format uses "sections"
        let input = json!([
            {"root": "/tmp", "config": {}, "renderer": "html", "mdbook_version": "0.4.52"},
            {"sections": [{"Chapter": {"name": "Test", "content": "# Test"}}], "__non_exhaustive": null}
        ]);

        let (result, is_05) = normalize_mdbook_json(input.clone());

        // Should not detect as mdbook 0.5.x
        assert!(!is_05);

        // Should remain unchanged - already in 0.4.x format
        let book = result.as_array().unwrap().get(1).unwrap();
        assert!(book.get("sections").is_some());
        assert!(book.get("items").is_none());
    }

    #[test]
    fn test_normalize_mdbook_json_v05_format() {
        // mdbook 0.5.x format uses "items" and has no __non_exhaustive field
        let input = json!([
            {"root": "/tmp", "config": {}, "renderer": "html", "mdbook_version": "0.5.1"},
            {"items": [{"Chapter": {"name": "Test", "content": "# Test"}}]}
        ]);

        let (result, is_05) = normalize_mdbook_json(input);

        // Should detect as mdbook 0.5.x
        assert!(is_05);

        // "items" should be renamed to "sections"
        let book = result.as_array().unwrap().get(1).unwrap();
        assert!(book.get("sections").is_some());
        assert!(book.get("items").is_none());

        // __non_exhaustive should be added for mdbook 0.4.x compatibility
        assert!(book.get("__non_exhaustive").is_some());

        // Content should be preserved
        let sections = book.get("sections").unwrap().as_array().unwrap();
        assert_eq!(sections.len(), 1);
    }

    #[test]
    fn test_normalize_mdbook_json_nested_items() {
        // Chapters can have nested sub_items in 0.5.x
        let input = json!([
            {"root": "/tmp", "config": {}, "renderer": "html", "mdbook_version": "0.5.1"},
            {
                "items": [
                    {
                        "Chapter": {
                            "name": "Parent",
                            "content": "# Parent",
                            "sub_items": [
                                {"Chapter": {"name": "Child", "content": "# Child"}}
                            ]
                        }
                    }
                ],
                "__non_exhaustive": null
            }
        ]);

        let (result, _is_05) = normalize_mdbook_json(input);

        // Top-level "items" should be renamed to "sections"
        let book = result.as_array().unwrap().get(1).unwrap();
        assert!(book.get("sections").is_some());

        // sub_items should remain as "sub_items" (not renamed)
        let sections = book.get("sections").unwrap().as_array().unwrap();
        let chapter = sections[0].get("Chapter").unwrap();
        assert!(chapter.get("sub_items").is_some());
    }

    #[test]
    fn test_normalize_preserves_other_fields() {
        // Ensure normalization doesn't affect unrelated fields
        // Note: This is a non-standard structure (not mdbook format), so we use the inner function
        let input = json!({
            "items": [1, 2, 3],
            "other_field": "value",
            "nested": {
                "items": ["a", "b"],
                "data": 42
            }
        });

        let result = normalize_mdbook_json_inner(input);

        // "items" at each level should become "sections"
        assert!(result.get("sections").is_some());
        assert!(result.get("other_field").is_some());

        let nested = result.get("nested").unwrap();
        assert!(nested.get("sections").is_some());
        assert!(nested.get("data").is_some());
    }

    #[test]
    fn test_full_mdbook_04_json_parsing() {
        // Full mdbook 0.4.x JSON payload
        let input = json!([
            {
                "root": "/tmp/test",
                "config": {
                    "book": {
                        "title": "Test Book",
                        "authors": ["Author"],
                        "description": null,
                        "language": "en",
                        "text-direction": null
                    },
                    "output": {"html": {}},
                    "preprocessor": {}
                },
                "renderer": "html",
                "mdbook_version": "0.4.52"
            },
            {
                "sections": [
                    {
                        "Chapter": {
                            "name": "Introduction",
                            "content": "# Introduction\n\nWelcome!",
                            "number": [1],
                            "sub_items": [],
                            "path": "intro.md",
                            "source_path": "intro.md",
                            "parent_names": []
                        }
                    }
                ],
                "__non_exhaustive": null
            }
        ]);

        // Apply both normalization and null stripping
        let (normalized, is_05) = normalize_mdbook_json(input);
        let cleaned = strip_null_values(normalized);

        // Should not detect as mdbook 0.5.x
        assert!(!is_05);

        // Verify structure is correct for deserialization
        let arr = cleaned.as_array().unwrap();
        assert_eq!(arr.len(), 2);

        let ctx = &arr[0];
        assert!(ctx.get("root").is_some());
        assert!(ctx.get("config").is_some());

        let book = &arr[1];
        assert!(book.get("sections").is_some());
        assert!(book.get("__non_exhaustive").is_some());

        // Null values should be stripped from config
        let config = ctx.get("config").unwrap();
        let book_config = config.get("book").unwrap();
        assert!(book_config.get("description").is_none()); // null stripped
        assert!(book_config.get("title").is_some()); // non-null preserved
    }

    #[test]
    fn test_full_mdbook_05_json_parsing() {
        // Full mdbook 0.5.x JSON payload (no __non_exhaustive field)
        let input = json!([
            {
                "root": "/tmp/test",
                "config": {
                    "book": {
                        "title": "Test Book",
                        "authors": ["Author"],
                        "description": null,
                        "language": "en",
                        "text-direction": null
                    },
                    "output": {"html": {}},
                    "preprocessor": {}
                },
                "renderer": "html",
                "mdbook_version": "0.5.1"
            },
            {
                "items": [
                    {
                        "Chapter": {
                            "name": "Introduction",
                            "content": "# Introduction\n\nWelcome!",
                            "number": [1],
                            "sub_items": [],
                            "path": "intro.md",
                            "source_path": "intro.md",
                            "parent_names": []
                        }
                    }
                ]
            }
        ]);

        // Apply both normalization and null stripping
        let (normalized, is_05) = normalize_mdbook_json(input);
        let cleaned = strip_null_values(normalized);

        // Should detect as mdbook 0.5.x
        assert!(is_05);

        // Verify structure is correct for deserialization
        let arr = cleaned.as_array().unwrap();
        assert_eq!(arr.len(), 2);

        let book = &arr[1];
        // "items" should have been converted to "sections"
        assert!(book.get("sections").is_some());
        assert!(book.get("items").is_none());
        // __non_exhaustive should have been added
        assert!(book.get("__non_exhaustive").is_some());
    }

    #[test]
    fn test_denormalize_for_mdbook_05() {
        // Test that output is properly converted back to mdbook 0.5.x format
        let input = json!({
            "sections": [
                {"Chapter": {"name": "Test", "content": "# Test"}}
            ],
            "__non_exhaustive": null
        });

        let result = denormalize_for_mdbook_05(input);

        // "sections" should be renamed to "items"
        assert!(result.get("items").is_some());
        assert!(result.get("sections").is_none());

        // __non_exhaustive should be removed
        assert!(result.get("__non_exhaustive").is_none());
    }

    #[test]
    fn test_roundtrip_mdbook_05() {
        // Test full roundtrip: mdbook 0.5.x input -> normalize -> denormalize -> mdbook 0.5.x output
        let input = json!([
            {"root": "/tmp", "config": {}, "renderer": "html", "mdbook_version": "0.5.1"},
            {"items": [{"Chapter": {"name": "Test", "content": "# Test"}}]}
        ]);

        let (normalized, is_05) = normalize_mdbook_json(input.clone());
        assert!(is_05);

        // Verify normalization worked
        let book = normalized.as_array().unwrap().get(1).unwrap();
        assert!(book.get("sections").is_some());

        // Denormalize the book part (simulating what handle_preprocessing does)
        let denormalized_book = denormalize_for_mdbook_05(book.clone());

        // Should be back to mdbook 0.5.x format
        assert!(denormalized_book.get("items").is_some());
        assert!(denormalized_book.get("sections").is_none());
        assert!(denormalized_book.get("__non_exhaustive").is_none());
    }
}