1use crate::types::LineLength;
2use globset::{Glob, GlobBuilder, GlobMatcher, GlobSet, GlobSetBuilder};
3use indexmap::IndexMap;
4use serde::{Deserialize, Serialize};
5use std::collections::BTreeMap;
6use std::collections::{HashMap, HashSet};
7use std::fs;
8use std::io;
9use std::path::Path;
10use std::sync::{Arc, OnceLock};
11
12use super::flavor::{MarkdownFlavor, normalize_key};
13
14#[derive(Debug, Clone, Serialize, Deserialize, Default, PartialEq, schemars::JsonSchema)]
16pub struct RuleConfig {
17 #[serde(default, skip_serializing_if = "Option::is_none")]
19 pub severity: Option<crate::rule::Severity>,
20
21 #[serde(flatten)]
23 #[schemars(schema_with = "arbitrary_value_schema")]
24 pub values: BTreeMap<String, toml::Value>,
25}
26
27fn arbitrary_value_schema(_gen: &mut schemars::SchemaGenerator) -> schemars::Schema {
29 schemars::json_schema!({
30 "type": "object",
31 "additionalProperties": true
32 })
33}
34
35#[derive(Debug, Clone, Serialize, Deserialize, Default, schemars::JsonSchema)]
37#[schemars(
38 description = "rumdl configuration for linting Markdown files. Rules can be configured individually using [MD###] sections with rule-specific options."
39)]
40pub struct Config {
41 #[serde(default, skip_serializing_if = "Option::is_none")]
45 pub extends: Option<String>,
46
47 #[serde(default)]
49 pub global: GlobalConfig,
50
51 #[serde(default, rename = "per-file-ignores")]
54 pub per_file_ignores: HashMap<String, Vec<String>>,
55
56 #[serde(default, rename = "per-file-flavor")]
60 #[schemars(with = "HashMap<String, MarkdownFlavor>")]
61 pub per_file_flavor: IndexMap<String, MarkdownFlavor>,
62
63 #[serde(default, rename = "code-block-tools")]
66 pub code_block_tools: crate::code_block_tools::CodeBlockToolsConfig,
67
68 #[serde(flatten)]
79 pub rules: BTreeMap<String, RuleConfig>,
80
81 #[serde(skip)]
83 pub project_root: Option<std::path::PathBuf>,
84
85 #[serde(skip)]
86 #[schemars(skip)]
87 pub(super) per_file_ignores_cache: Arc<OnceLock<PerFileIgnoreCache>>,
88
89 #[serde(skip)]
90 #[schemars(skip)]
91 pub(super) per_file_flavor_cache: Arc<OnceLock<PerFileFlavorCache>>,
92}
93
94impl PartialEq for Config {
95 fn eq(&self, other: &Self) -> bool {
96 self.global == other.global
97 && self.per_file_ignores == other.per_file_ignores
98 && self.per_file_flavor == other.per_file_flavor
99 && self.code_block_tools == other.code_block_tools
100 && self.rules == other.rules
101 && self.project_root == other.project_root
102 }
103}
104
105#[derive(Debug)]
106pub(super) struct PerFileIgnoreCache {
107 globset: GlobSet,
108 rules: Vec<Vec<String>>,
109}
110
111#[derive(Debug)]
112pub(super) struct PerFileFlavorCache {
113 matchers: Vec<(GlobMatcher, MarkdownFlavor)>,
114}
115
116impl Config {
117 pub fn is_mkdocs_flavor(&self) -> bool {
119 self.global.flavor == MarkdownFlavor::MkDocs
120 }
121
122 pub fn markdown_flavor(&self) -> MarkdownFlavor {
128 self.global.flavor
129 }
130
131 pub fn is_mkdocs_project(&self) -> bool {
133 self.is_mkdocs_flavor()
134 }
135
136 pub fn apply_per_rule_enabled(&mut self) {
147 let mut to_enable: Vec<String> = Vec::new();
148 let mut to_disable: Vec<String> = Vec::new();
149
150 for (name, cfg) in &self.rules {
151 match cfg.values.get("enabled") {
152 Some(toml::Value::Boolean(true)) => {
153 to_enable.push(name.clone());
154 }
155 Some(toml::Value::Boolean(false)) => {
156 to_disable.push(name.clone());
157 }
158 _ => {}
159 }
160 }
161
162 for name in to_enable {
163 if !self.global.extend_enable.contains(&name) {
164 self.global.extend_enable.push(name.clone());
165 }
166 self.global.disable.retain(|n| n != &name);
167 self.global.extend_disable.retain(|n| n != &name);
168 }
169
170 for name in to_disable {
171 if !self.global.disable.contains(&name) {
172 self.global.disable.push(name.clone());
173 }
174 self.global.extend_enable.retain(|n| n != &name);
175 }
176 }
177
178 pub fn get_rule_severity(&self, rule_name: &str) -> Option<crate::rule::Severity> {
180 self.rules.get(rule_name).and_then(|r| r.severity)
181 }
182
183 pub fn get_ignored_rules_for_file(&self, file_path: &Path) -> HashSet<String> {
186 let mut ignored_rules = HashSet::new();
187
188 if self.per_file_ignores.is_empty() {
189 return ignored_rules;
190 }
191
192 let path_for_matching: std::borrow::Cow<'_, Path> = if let Some(ref root) = self.project_root {
195 if let Ok(canonical_path) = file_path.canonicalize() {
196 if let Ok(canonical_root) = root.canonicalize() {
197 if let Ok(relative) = canonical_path.strip_prefix(&canonical_root) {
198 std::borrow::Cow::Owned(relative.to_path_buf())
199 } else {
200 std::borrow::Cow::Borrowed(file_path)
201 }
202 } else {
203 std::borrow::Cow::Borrowed(file_path)
204 }
205 } else {
206 std::borrow::Cow::Borrowed(file_path)
207 }
208 } else {
209 std::borrow::Cow::Borrowed(file_path)
210 };
211
212 let cache = self
213 .per_file_ignores_cache
214 .get_or_init(|| PerFileIgnoreCache::new(&self.per_file_ignores));
215
216 for match_idx in cache.globset.matches(path_for_matching.as_ref()) {
218 if let Some(rules) = cache.rules.get(match_idx) {
219 for rule in rules.iter() {
220 ignored_rules.insert(rule.clone());
222 }
223 }
224 }
225
226 ignored_rules
227 }
228
229 pub fn get_flavor_for_file(&self, file_path: &Path) -> MarkdownFlavor {
233 if self.per_file_flavor.is_empty() {
235 return self.resolve_flavor_fallback(file_path);
236 }
237
238 let path_for_matching: std::borrow::Cow<'_, Path> = if let Some(ref root) = self.project_root {
240 if let Ok(canonical_path) = file_path.canonicalize() {
241 if let Ok(canonical_root) = root.canonicalize() {
242 if let Ok(relative) = canonical_path.strip_prefix(&canonical_root) {
243 std::borrow::Cow::Owned(relative.to_path_buf())
244 } else {
245 std::borrow::Cow::Borrowed(file_path)
246 }
247 } else {
248 std::borrow::Cow::Borrowed(file_path)
249 }
250 } else {
251 std::borrow::Cow::Borrowed(file_path)
252 }
253 } else {
254 std::borrow::Cow::Borrowed(file_path)
255 };
256
257 let cache = self
258 .per_file_flavor_cache
259 .get_or_init(|| PerFileFlavorCache::new(&self.per_file_flavor));
260
261 for (matcher, flavor) in &cache.matchers {
263 if matcher.is_match(path_for_matching.as_ref()) {
264 return *flavor;
265 }
266 }
267
268 self.resolve_flavor_fallback(file_path)
270 }
271
272 fn resolve_flavor_fallback(&self, file_path: &Path) -> MarkdownFlavor {
274 if self.global.flavor != MarkdownFlavor::Standard {
276 return self.global.flavor;
277 }
278 MarkdownFlavor::from_path(file_path)
280 }
281
282 pub fn merge_with_inline_config(&self, inline_config: &crate::inline_config::InlineConfig) -> Self {
290 let overrides = inline_config.get_all_rule_configs();
291 if overrides.is_empty() {
292 return self.clone();
293 }
294
295 let mut merged = self.clone();
296
297 for (rule_name, json_override) in overrides {
298 let rule_config = merged.rules.entry(rule_name.clone()).or_default();
300
301 if let Some(obj) = json_override.as_object() {
303 for (key, value) in obj {
304 let normalized_key = key.replace('_', "-");
306
307 if let Some(toml_value) = json_to_toml(value) {
309 rule_config.values.insert(normalized_key, toml_value);
310 }
311 }
312 }
313 }
314
315 merged
316 }
317}
318
319pub(super) fn json_to_toml(json: &serde_json::Value) -> Option<toml::Value> {
321 match json {
322 serde_json::Value::Null => None,
323 serde_json::Value::Bool(b) => Some(toml::Value::Boolean(*b)),
324 serde_json::Value::Number(n) => n
325 .as_i64()
326 .map(toml::Value::Integer)
327 .or_else(|| n.as_f64().map(toml::Value::Float)),
328 serde_json::Value::String(s) => Some(toml::Value::String(s.clone())),
329 serde_json::Value::Array(arr) => {
330 let toml_arr: Vec<toml::Value> = arr.iter().filter_map(json_to_toml).collect();
331 Some(toml::Value::Array(toml_arr))
332 }
333 serde_json::Value::Object(obj) => {
334 let mut table = toml::map::Map::new();
335 for (k, v) in obj {
336 if let Some(tv) = json_to_toml(v) {
337 table.insert(k.clone(), tv);
338 }
339 }
340 Some(toml::Value::Table(table))
341 }
342 }
343}
344
345impl PerFileIgnoreCache {
346 fn new(per_file_ignores: &HashMap<String, Vec<String>>) -> Self {
347 let mut builder = GlobSetBuilder::new();
348 let mut rules = Vec::new();
349
350 for (pattern, rules_list) in per_file_ignores {
351 if let Ok(glob) = Glob::new(pattern) {
352 builder.add(glob);
353 rules.push(rules_list.iter().map(|rule| normalize_key(rule)).collect());
354 } else {
355 log::warn!("Invalid glob pattern in per-file-ignores: {pattern}");
356 }
357 }
358
359 let globset = builder.build().unwrap_or_else(|e| {
360 log::error!("Failed to build globset for per-file-ignores: {e}");
361 GlobSetBuilder::new().build().unwrap()
362 });
363
364 Self { globset, rules }
365 }
366}
367
368impl PerFileFlavorCache {
369 fn new(per_file_flavor: &IndexMap<String, MarkdownFlavor>) -> Self {
370 let mut matchers = Vec::new();
371
372 for (pattern, flavor) in per_file_flavor {
373 if let Ok(glob) = GlobBuilder::new(pattern).literal_separator(true).build() {
374 matchers.push((glob.compile_matcher(), *flavor));
375 } else {
376 log::warn!("Invalid glob pattern in per-file-flavor: {pattern}");
377 }
378 }
379
380 Self { matchers }
381 }
382}
383
384#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, schemars::JsonSchema)]
386#[serde(default, rename_all = "kebab-case")]
387pub struct GlobalConfig {
388 #[serde(default)]
390 pub enable: Vec<String>,
391
392 #[serde(default)]
394 pub disable: Vec<String>,
395
396 #[serde(default)]
398 pub exclude: Vec<String>,
399
400 #[serde(default)]
402 pub include: Vec<String>,
403
404 #[serde(default = "default_respect_gitignore", alias = "respect_gitignore")]
406 pub respect_gitignore: bool,
407
408 #[serde(default, alias = "line_length")]
410 pub line_length: LineLength,
411
412 #[serde(skip_serializing_if = "Option::is_none", alias = "output_format")]
414 pub output_format: Option<String>,
415
416 #[serde(default)]
419 pub fixable: Vec<String>,
420
421 #[serde(default)]
424 pub unfixable: Vec<String>,
425
426 #[serde(default)]
429 pub flavor: MarkdownFlavor,
430
431 #[serde(default, alias = "force_exclude")]
436 #[deprecated(since = "0.0.156", note = "Exclude patterns are now always respected")]
437 pub force_exclude: bool,
438
439 #[serde(default, alias = "cache_dir", skip_serializing_if = "Option::is_none")]
442 pub cache_dir: Option<String>,
443
444 #[serde(default = "default_true")]
447 pub cache: bool,
448
449 #[serde(default, alias = "extend_enable")]
451 pub extend_enable: Vec<String>,
452
453 #[serde(default, alias = "extend_disable")]
455 pub extend_disable: Vec<String>,
456
457 #[serde(skip)]
461 pub enable_is_explicit: bool,
462}
463
464fn default_respect_gitignore() -> bool {
465 true
466}
467
468fn default_true() -> bool {
469 true
470}
471
472impl Default for GlobalConfig {
474 #[allow(deprecated)]
475 fn default() -> Self {
476 Self {
477 enable: Vec::new(),
478 disable: Vec::new(),
479 exclude: Vec::new(),
480 include: Vec::new(),
481 respect_gitignore: true,
482 line_length: LineLength::default(),
483 output_format: None,
484 fixable: Vec::new(),
485 unfixable: Vec::new(),
486 flavor: MarkdownFlavor::default(),
487 force_exclude: false,
488 cache_dir: None,
489 cache: true,
490 extend_enable: Vec::new(),
491 extend_disable: Vec::new(),
492 enable_is_explicit: false,
493 }
494 }
495}
496
497pub const MARKDOWNLINT_CONFIG_FILES: &[&str] = &[
498 ".markdownlint-cli2.jsonc",
499 ".markdownlint-cli2.yaml",
500 ".markdownlint-cli2.yml",
501 ".markdownlint.json",
502 ".markdownlint.jsonc",
503 ".markdownlint.yaml",
504 ".markdownlint.yml",
505 "markdownlint.json",
506 "markdownlint.jsonc",
507 "markdownlint.yaml",
508 "markdownlint.yml",
509];
510
511pub fn create_default_config(path: &str) -> Result<(), ConfigError> {
513 create_preset_config("default", path)
514}
515
516pub fn create_preset_config(preset: &str, path: &str) -> Result<(), ConfigError> {
518 if Path::new(path).exists() {
519 return Err(ConfigError::FileExists { path: path.to_string() });
520 }
521
522 let config_content = match preset {
523 "default" => generate_default_preset(),
524 "google" => generate_google_preset(),
525 "relaxed" => generate_relaxed_preset(),
526 _ => {
527 return Err(ConfigError::UnknownPreset {
528 name: preset.to_string(),
529 });
530 }
531 };
532
533 match fs::write(path, config_content) {
534 Ok(_) => Ok(()),
535 Err(err) => Err(ConfigError::IoError {
536 source: err,
537 path: path.to_string(),
538 }),
539 }
540}
541
542fn generate_default_preset() -> String {
545 r#"# rumdl configuration file
546
547# Inherit settings from another config file (relative to this file's directory)
548# extends = "../base.rumdl.toml"
549
550# Global configuration options
551[global]
552# List of rules to disable (uncomment and modify as needed)
553# disable = ["MD013", "MD033"]
554
555# List of rules to enable exclusively (replaces defaults; only these rules will run)
556# enable = ["MD001", "MD003", "MD004"]
557
558# Additional rules to enable on top of defaults (additive, does not replace)
559# Use this to activate opt-in rules like MD060, MD063, MD072, MD073, MD074
560# extend-enable = ["MD060", "MD063"]
561
562# Additional rules to disable on top of the disable list (additive)
563# extend-disable = ["MD041"]
564
565# List of file/directory patterns to include for linting (if provided, only these will be linted)
566# include = [
567# "docs/*.md",
568# "src/**/*.md",
569# "README.md"
570# ]
571
572# List of file/directory patterns to exclude from linting
573exclude = [
574 # Common directories to exclude
575 ".git",
576 ".github",
577 "node_modules",
578 "vendor",
579 "dist",
580 "build",
581
582 # Specific files or patterns
583 "CHANGELOG.md",
584 "LICENSE.md",
585]
586
587# Respect .gitignore files when scanning directories (default: true)
588respect-gitignore = true
589
590# Markdown flavor/dialect (uncomment to enable)
591# Options: standard (default), gfm, commonmark, mkdocs, mdx, quarto
592# flavor = "mkdocs"
593
594# Rule-specific configurations (uncomment and modify as needed)
595
596# [MD003]
597# style = "atx" # Heading style (atx, atx_closed, setext)
598
599# [MD004]
600# style = "asterisk" # Unordered list style (asterisk, plus, dash, consistent)
601
602# [MD007]
603# indent = 4 # Unordered list indentation
604
605# [MD013]
606# line-length = 100 # Line length
607# code-blocks = false # Exclude code blocks from line length check
608# tables = false # Exclude tables from line length check
609# headings = true # Include headings in line length check
610
611# [MD044]
612# names = ["rumdl", "Markdown", "GitHub"] # Proper names that should be capitalized correctly
613# code-blocks = false # Check code blocks for proper names (default: false, skips code blocks)
614"#
615 .to_string()
616}
617
618fn generate_google_preset() -> String {
621 r#"# rumdl configuration - Google developer documentation style
622# Based on https://google.github.io/styleguide/docguide/style.html
623
624[global]
625exclude = [
626 ".git",
627 ".github",
628 "node_modules",
629 "vendor",
630 "dist",
631 "build",
632 "CHANGELOG.md",
633 "LICENSE.md",
634]
635respect-gitignore = true
636
637# ATX-style headings required
638[MD003]
639style = "atx"
640
641# Unordered list style: dash
642[MD004]
643style = "dash"
644
645# 4-space indent for nested lists
646[MD007]
647indent = 4
648
649# Strict mode: no trailing spaces allowed (Google uses backslash for line breaks)
650[MD009]
651strict = true
652
653# 80-character line length
654[MD013]
655line-length = 80
656code-blocks = false
657tables = false
658
659# No trailing punctuation in headings
660[MD026]
661punctuation = ".,;:!。,;:!"
662
663# Fenced code blocks only (no indented code blocks)
664[MD046]
665style = "fenced"
666
667# Emphasis with underscores
668[MD049]
669style = "underscore"
670
671# Strong with asterisks
672[MD050]
673style = "asterisk"
674"#
675 .to_string()
676}
677
678fn generate_relaxed_preset() -> String {
681 r#"# rumdl configuration - Relaxed preset
682# Lenient settings for existing projects adopting rumdl incrementally.
683# Minimizes initial warnings while still catching important issues.
684
685[global]
686exclude = [
687 ".git",
688 ".github",
689 "node_modules",
690 "vendor",
691 "dist",
692 "build",
693 "CHANGELOG.md",
694 "LICENSE.md",
695]
696respect-gitignore = true
697
698# Disable rules that produce the most noise on existing projects
699disable = [
700 "MD013", # Line length - most existing files exceed 80 chars
701 "MD033", # Inline HTML - commonly used in real-world markdown
702 "MD041", # First line heading - not all files need it
703]
704
705# Consistent heading style (any style, just be consistent)
706[MD003]
707style = "consistent"
708
709# Consistent list style
710[MD004]
711style = "consistent"
712
713# Consistent emphasis style
714[MD049]
715style = "consistent"
716
717# Consistent strong style
718[MD050]
719style = "consistent"
720"#
721 .to_string()
722}
723
724#[derive(Debug, thiserror::Error)]
726pub enum ConfigError {
727 #[error("Failed to read config file at {path}: {source}")]
729 IoError { source: io::Error, path: String },
730
731 #[error("Failed to parse config: {0}")]
733 ParseError(String),
734
735 #[error("Configuration file already exists at {path}")]
737 FileExists { path: String },
738
739 #[error("Circular extends reference: {path} already in chain {chain:?}")]
741 CircularExtends { path: String, chain: Vec<String> },
742
743 #[error("Extends chain exceeds maximum depth of {max_depth} at {path}")]
745 ExtendsDepthExceeded { path: String, max_depth: usize },
746
747 #[error("extends target not found: {path} (referenced from {from})")]
749 ExtendsNotFound { path: String, from: String },
750
751 #[error("Unknown preset: {name}. Valid presets: default, google, relaxed")]
753 UnknownPreset { name: String },
754}
755
756pub fn get_rule_config_value<T: serde::de::DeserializeOwned>(config: &Config, rule_name: &str, key: &str) -> Option<T> {
760 let norm_rule_name = rule_name.to_ascii_uppercase(); let rule_config = config.rules.get(&norm_rule_name)?;
763
764 let key_variants = [
766 key.to_string(), normalize_key(key), key.replace('-', "_"), key.replace('_', "-"), ];
771
772 for variant in &key_variants {
774 if let Some(value) = rule_config.values.get(variant)
775 && let Ok(result) = T::deserialize(value.clone())
776 {
777 return Some(result);
778 }
779 }
780
781 None
782}
783
784pub fn generate_pyproject_preset_config(preset: &str) -> Result<String, ConfigError> {
787 match preset {
788 "default" => Ok(generate_pyproject_config()),
789 other => {
790 let rumdl_config = match other {
791 "google" => generate_google_preset(),
792 "relaxed" => generate_relaxed_preset(),
793 _ => {
794 return Err(ConfigError::UnknownPreset {
795 name: other.to_string(),
796 });
797 }
798 };
799 Ok(convert_rumdl_to_pyproject(&rumdl_config))
800 }
801 }
802}
803
804fn convert_rumdl_to_pyproject(rumdl_config: &str) -> String {
807 let mut output = String::with_capacity(rumdl_config.len() + 128);
808 for line in rumdl_config.lines() {
809 let trimmed = line.trim();
810 if trimmed.starts_with('[') && trimmed.ends_with(']') && !trimmed.starts_with("# [") {
811 let section = &trimmed[1..trimmed.len() - 1];
812 if section == "global" {
813 output.push_str("[tool.rumdl]");
814 } else {
815 output.push_str(&format!("[tool.rumdl.{section}]"));
816 }
817 } else {
818 output.push_str(line);
819 }
820 output.push('\n');
821 }
822 output
823}
824
825pub fn generate_pyproject_config() -> String {
827 let config_content = r#"
828[tool.rumdl]
829# Global configuration options
830line-length = 100
831disable = []
832# extend-enable = ["MD060"] # Add opt-in rules (additive, keeps defaults)
833# extend-disable = [] # Additional rules to disable (additive)
834exclude = [
835 # Common directories to exclude
836 ".git",
837 ".github",
838 "node_modules",
839 "vendor",
840 "dist",
841 "build",
842]
843respect-gitignore = true
844
845# Rule-specific configurations (uncomment and modify as needed)
846
847# [tool.rumdl.MD003]
848# style = "atx" # Heading style (atx, atx_closed, setext)
849
850# [tool.rumdl.MD004]
851# style = "asterisk" # Unordered list style (asterisk, plus, dash, consistent)
852
853# [tool.rumdl.MD007]
854# indent = 4 # Unordered list indentation
855
856# [tool.rumdl.MD013]
857# line-length = 100 # Line length
858# code-blocks = false # Exclude code blocks from line length check
859# tables = false # Exclude tables from line length check
860# headings = true # Include headings in line length check
861
862# [tool.rumdl.MD044]
863# names = ["rumdl", "Markdown", "GitHub"] # Proper names that should be capitalized correctly
864# code-blocks = false # Check code blocks for proper names (default: false, skips code blocks)
865"#;
866
867 config_content.to_string()
868}