#sass #formatter #css #less #formatting #configurable #smart

malva

Configurable, smart and fast CSS, SCSS, Sass and Less formatter

22 releases (11 breaking)

new 0.11.1 Dec 9, 2024
0.11.0 Oct 21, 2024
0.10.1 Aug 25, 2024
0.5.1 Jul 1, 2024
0.1.2 Nov 1, 2023

#484 in Web programming

Download history 970/week @ 2024-08-22 832/week @ 2024-08-29 1239/week @ 2024-09-05 940/week @ 2024-09-12 893/week @ 2024-09-19 792/week @ 2024-09-26 1115/week @ 2024-10-03 1626/week @ 2024-10-10 1580/week @ 2024-10-17 1596/week @ 2024-10-24 1162/week @ 2024-10-31 1017/week @ 2024-11-07 766/week @ 2024-11-14 940/week @ 2024-11-21 1052/week @ 2024-11-28 988/week @ 2024-12-05

3,893 downloads per month
Used in 8 crates (5 directly)

MIT license

270KB
7K SLoC

Malva is a configurable, smart and fast CSS, SCSS, Sass and Less formatter.

Basic Usage

You can format source code string by using format_text function.

use malva::{config::FormatOptions, format_text, Syntax};

let options = FormatOptions::default();
assert_eq!("a {
  color: red;
}
", &format_text("a{color:red}", Syntax::Css, &options).unwrap());

For detailed documentation of configuration, please refer to Configuration on GitHub.

If there're syntax errors in source code, it will return Err:

use malva::{config::FormatOptions, format_text, Syntax};

let options = FormatOptions::default();
assert!(format_text("a{", Syntax::Css, &options).is_err());

Print AST

If you have already parsed the AST with Raffia, you can use print_stylesheet to print it.

Please note that though you have AST, you still need to provide comments and specify syntax, also create LineBounds manually.

use malva::{config::FormatOptions, print_stylesheet, LineBounds, Syntax};
use raffia::{ast::Stylesheet, ParserBuilder};

let input = "a{color:red}";
let mut comments = vec![];
let mut parser = ParserBuilder::new(input)
    .syntax(Syntax::Css)
    .comments(&mut comments)
    .build();
let stylesheet = parser.parse::<Stylesheet>().unwrap();

let options = FormatOptions::default();
let line_bounds = LineBounds::new(input);
assert_eq!("a {
  color: red;
}
", &print_stylesheet(&stylesheet, &comments, Some(input), line_bounds, Syntax::Css, &options));

Dependencies

~1.9–2.7MB
~55K SLoC