beautiful_md/lib.rs
1//! beautiful-md: A CLI tool and library to format and beautify Markdown files.
2//!
3//! This crate provides functionality to parse, format, and beautify Markdown files
4//! according to configurable style rules. It can be used as a library or as a
5//! command-line tool.
6//!
7//! # Examples
8//!
9//! ```no_run
10//! use beautiful_md::{Config, format_markdown};
11//!
12//! let markdown = "# Heading\n\n|Name|Age|\n|---|---|\n|Alice|30|";
13//! let config = Config::default();
14//! let (formatted, _diagnostics) = format_markdown(markdown, &config).unwrap();
15//! println!("{}", formatted);
16//! ```
17//!
18//! # Features
19//!
20//! - Table alignment and padding
21//! - Heading spacing normalization
22//! - List indentation consistency
23//! - Code block formatting
24//! - Configurable via TOML files
25//!
26//! # Configuration
27//!
28//! Create a `.beautiful-md.toml` file in your project root or home directory:
29//!
30//! ```toml
31//! [tables]
32//! align = true
33//! min_column_width = 3
34//! padding = 1
35//!
36//! [headings]
37//! blank_lines_before = 1
38//! blank_lines_after = 1
39//! space_after_hash = true
40//!
41//! [lists]
42//! indent_size = 2
43//! marker = "-"
44//! normalize_numbers = true
45//!
46//! [code]
47//! ensure_language_tag = false
48//! fence_style = "```"
49//! ```
50
51#![warn(missing_docs)]
52#![warn(clippy::all, clippy::pedantic)]
53#![allow(clippy::module_name_repetitions)]
54#![allow(clippy::multiple_crate_versions)]
55
56pub mod config;
57pub mod diagnostics;
58pub mod error;
59mod formatter;
60mod formatters;
61mod preprocessor;
62
63// Re-export main types for convenience
64pub use config::Config;
65pub use diagnostics::Diagnostics;
66pub use error::{Error, Result};
67
68/// Format markdown content according to the provided configuration.
69///
70/// Returns the formatted markdown and any diagnostics collected during processing.
71///
72/// # Errors
73///
74/// Returns an error if the markdown cannot be parsed or formatted.
75///
76/// # Examples
77///
78/// ```
79/// use beautiful_md::{Config, format_markdown};
80///
81/// let markdown = "# Heading\n\nSome text.";
82/// let config = Config::default();
83/// let (formatted, diagnostics) = format_markdown(markdown, &config).unwrap();
84/// assert!(!formatted.is_empty());
85/// ```
86pub fn format_markdown(content: &str, config: &Config) -> Result<(String, Diagnostics)> {
87 formatter::format(content, config)
88}
89
90/// Format a markdown file in-place.
91///
92/// Returns diagnostics collected during processing.
93///
94/// # Errors
95///
96/// Returns an error if the file cannot be read, parsed, formatted, or written.
97pub fn format_file<P: AsRef<std::path::Path>>(path: P, config: &Config) -> Result<Diagnostics> {
98 let content = std::fs::read_to_string(path.as_ref())?;
99 let (formatted, diagnostics) = format_markdown(&content, config)?;
100 std::fs::write(path.as_ref(), formatted)?;
101 Ok(diagnostics)
102}