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 {
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 RUMDL_CONFIG_FILES: &[&str] = &[".rumdl.toml", "rumdl.toml", ".config/rumdl.toml", "pyproject.toml"];
510
511pub const MARKDOWNLINT_CONFIG_FILES: &[&str] = &[
512 ".markdownlint-cli2.jsonc",
513 ".markdownlint-cli2.yaml",
514 ".markdownlint-cli2.yml",
515 ".markdownlint.json",
516 ".markdownlint.jsonc",
517 ".markdownlint.yaml",
518 ".markdownlint.yml",
519 "markdownlint.json",
520 "markdownlint.jsonc",
521 "markdownlint.yaml",
522 "markdownlint.yml",
523];
524
525pub fn create_default_config(path: &str) -> Result<(), ConfigError> {
527 create_preset_config("default", path)
528}
529
530pub fn create_preset_config(preset: &str, path: &str) -> Result<(), ConfigError> {
532 if Path::new(path).exists() {
533 return Err(ConfigError::FileExists { path: path.to_string() });
534 }
535
536 let config_content = match preset {
537 "default" => generate_default_preset(),
538 "google" => generate_google_preset(),
539 "relaxed" => generate_relaxed_preset(),
540 _ => {
541 return Err(ConfigError::UnknownPreset {
542 name: preset.to_string(),
543 });
544 }
545 };
546
547 match fs::write(path, config_content) {
548 Ok(_) => Ok(()),
549 Err(err) => Err(ConfigError::IoError {
550 source: err,
551 path: path.to_string(),
552 }),
553 }
554}
555
556fn generate_default_preset() -> String {
559 r#"# rumdl configuration file
560
561# Inherit settings from another config file (relative to this file's directory)
562# extends = "../base.rumdl.toml"
563
564# Global configuration options
565[global]
566# List of rules to disable (uncomment and modify as needed)
567# disable = ["MD013", "MD033"]
568
569# List of rules to enable exclusively (replaces defaults; only these rules will run)
570# enable = ["MD001", "MD003", "MD004"]
571
572# Additional rules to enable on top of defaults (additive, does not replace)
573# Use this to activate opt-in rules like MD060, MD063, MD072, MD073, MD074
574# extend-enable = ["MD060", "MD063"]
575
576# Additional rules to disable on top of the disable list (additive)
577# extend-disable = ["MD041"]
578
579# List of file/directory patterns to include for linting (if provided, only these will be linted)
580# include = [
581# "docs/*.md",
582# "src/**/*.md",
583# "README.md"
584# ]
585
586# List of file/directory patterns to exclude from linting
587exclude = [
588 # Common directories to exclude
589 ".git",
590 ".github",
591 "node_modules",
592 "vendor",
593 "dist",
594 "build",
595
596 # Specific files or patterns
597 "CHANGELOG.md",
598 "LICENSE.md",
599]
600
601# Respect .gitignore files when scanning directories (default: true)
602respect-gitignore = true
603
604# Markdown flavor/dialect (uncomment to enable)
605# Options: standard (default), gfm, commonmark, mkdocs, mdx, quarto
606# flavor = "mkdocs"
607
608# Rule-specific configurations (uncomment and modify as needed)
609
610# [MD003]
611# style = "atx" # Heading style (atx, atx_closed, setext)
612
613# [MD004]
614# style = "asterisk" # Unordered list style (asterisk, plus, dash, consistent)
615
616# [MD007]
617# indent = 4 # Unordered list indentation
618
619# [MD013]
620# line-length = 100 # Line length
621# code-blocks = false # Exclude code blocks from line length check
622# tables = false # Exclude tables from line length check
623# headings = true # Include headings in line length check
624
625# [MD044]
626# names = ["rumdl", "Markdown", "GitHub"] # Proper names that should be capitalized correctly
627# code-blocks = false # Check code blocks for proper names (default: false, skips code blocks)
628"#
629 .to_string()
630}
631
632fn generate_google_preset() -> String {
635 r#"# rumdl configuration - Google developer documentation style
636# Based on https://google.github.io/styleguide/docguide/style.html
637
638[global]
639exclude = [
640 ".git",
641 ".github",
642 "node_modules",
643 "vendor",
644 "dist",
645 "build",
646 "CHANGELOG.md",
647 "LICENSE.md",
648]
649respect-gitignore = true
650
651# ATX-style headings required
652[MD003]
653style = "atx"
654
655# Unordered list style: dash
656[MD004]
657style = "dash"
658
659# 4-space indent for nested lists
660[MD007]
661indent = 4
662
663# Strict mode: no trailing spaces allowed (Google uses backslash for line breaks)
664[MD009]
665strict = true
666
667# 80-character line length
668[MD013]
669line-length = 80
670code-blocks = false
671tables = false
672
673# No trailing punctuation in headings
674[MD026]
675punctuation = ".,;:!。,;:!"
676
677# Fenced code blocks only (no indented code blocks)
678[MD046]
679style = "fenced"
680
681# Emphasis with underscores
682[MD049]
683style = "underscore"
684
685# Strong with asterisks
686[MD050]
687style = "asterisk"
688"#
689 .to_string()
690}
691
692fn generate_relaxed_preset() -> String {
695 r#"# rumdl configuration - Relaxed preset
696# Lenient settings for existing projects adopting rumdl incrementally.
697# Minimizes initial warnings while still catching important issues.
698
699[global]
700exclude = [
701 ".git",
702 ".github",
703 "node_modules",
704 "vendor",
705 "dist",
706 "build",
707 "CHANGELOG.md",
708 "LICENSE.md",
709]
710respect-gitignore = true
711
712# Disable rules that produce the most noise on existing projects
713disable = [
714 "MD013", # Line length - most existing files exceed 80 chars
715 "MD033", # Inline HTML - commonly used in real-world markdown
716 "MD041", # First line heading - not all files need it
717]
718
719# Consistent heading style (any style, just be consistent)
720[MD003]
721style = "consistent"
722
723# Consistent list style
724[MD004]
725style = "consistent"
726
727# Consistent emphasis style
728[MD049]
729style = "consistent"
730
731# Consistent strong style
732[MD050]
733style = "consistent"
734"#
735 .to_string()
736}
737
738#[derive(Debug, thiserror::Error)]
740pub enum ConfigError {
741 #[error("Failed to read config file at {path}: {source}")]
743 IoError { source: io::Error, path: String },
744
745 #[error("Failed to parse config: {0}")]
747 ParseError(String),
748
749 #[error("Configuration file already exists at {path}")]
751 FileExists { path: String },
752
753 #[error("Circular extends reference: {path} already in chain {chain:?}")]
755 CircularExtends { path: String, chain: Vec<String> },
756
757 #[error("Extends chain exceeds maximum depth of {max_depth} at {path}")]
759 ExtendsDepthExceeded { path: String, max_depth: usize },
760
761 #[error("extends target not found: {path} (referenced from {from})")]
763 ExtendsNotFound { path: String, from: String },
764
765 #[error("Unknown preset: {name}. Valid presets: default, google, relaxed")]
767 UnknownPreset { name: String },
768}
769
770pub fn get_rule_config_value<T: serde::de::DeserializeOwned>(config: &Config, rule_name: &str, key: &str) -> Option<T> {
774 let norm_rule_name = rule_name.to_ascii_uppercase(); let rule_config = config.rules.get(&norm_rule_name)?;
777
778 let key_variants = [
780 key.to_string(), normalize_key(key), key.replace('-', "_"), key.replace('_', "-"), ];
785
786 for variant in &key_variants {
788 if let Some(value) = rule_config.values.get(variant)
789 && let Ok(result) = T::deserialize(value.clone())
790 {
791 return Some(result);
792 }
793 }
794
795 None
796}
797
798pub fn generate_pyproject_preset_config(preset: &str) -> Result<String, ConfigError> {
801 match preset {
802 "default" => Ok(generate_pyproject_config()),
803 other => {
804 let rumdl_config = match other {
805 "google" => generate_google_preset(),
806 "relaxed" => generate_relaxed_preset(),
807 _ => {
808 return Err(ConfigError::UnknownPreset {
809 name: other.to_string(),
810 });
811 }
812 };
813 Ok(convert_rumdl_to_pyproject(&rumdl_config))
814 }
815 }
816}
817
818fn convert_rumdl_to_pyproject(rumdl_config: &str) -> String {
821 let mut output = String::with_capacity(rumdl_config.len() + 128);
822 for line in rumdl_config.lines() {
823 let trimmed = line.trim();
824 if trimmed.starts_with('[') && trimmed.ends_with(']') && !trimmed.starts_with("# [") {
825 let section = &trimmed[1..trimmed.len() - 1];
826 if section == "global" {
827 output.push_str("[tool.rumdl]");
828 } else {
829 output.push_str(&format!("[tool.rumdl.{section}]"));
830 }
831 } else {
832 output.push_str(line);
833 }
834 output.push('\n');
835 }
836 output
837}
838
839pub fn generate_pyproject_config() -> String {
841 let config_content = r#"
842[tool.rumdl]
843# Global configuration options
844line-length = 100
845disable = []
846# extend-enable = ["MD060"] # Add opt-in rules (additive, keeps defaults)
847# extend-disable = [] # Additional rules to disable (additive)
848exclude = [
849 # Common directories to exclude
850 ".git",
851 ".github",
852 "node_modules",
853 "vendor",
854 "dist",
855 "build",
856]
857respect-gitignore = true
858
859# Rule-specific configurations (uncomment and modify as needed)
860
861# [tool.rumdl.MD003]
862# style = "atx" # Heading style (atx, atx_closed, setext)
863
864# [tool.rumdl.MD004]
865# style = "asterisk" # Unordered list style (asterisk, plus, dash, consistent)
866
867# [tool.rumdl.MD007]
868# indent = 4 # Unordered list indentation
869
870# [tool.rumdl.MD013]
871# line-length = 100 # Line length
872# code-blocks = false # Exclude code blocks from line length check
873# tables = false # Exclude tables from line length check
874# headings = true # Include headings in line length check
875
876# [tool.rumdl.MD044]
877# names = ["rumdl", "Markdown", "GitHub"] # Proper names that should be capitalized correctly
878# code-blocks = false # Check code blocks for proper names (default: false, skips code blocks)
879"#;
880
881 config_content.to_string()
882}