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

malva

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

20 releases (10 breaking)

0.10.1 Aug 25, 2024
0.9.1 Aug 15, 2024
0.5.1 Jul 1, 2024
0.2.0 Mar 24, 2024
0.1.2 Nov 1, 2023

#342 in Web programming

Download history 9/week @ 2024-06-14 154/week @ 2024-06-21 178/week @ 2024-06-28 67/week @ 2024-07-05 5/week @ 2024-07-12 1/week @ 2024-07-19 134/week @ 2024-07-26 384/week @ 2024-08-02 799/week @ 2024-08-09 559/week @ 2024-08-16 917/week @ 2024-08-23 888/week @ 2024-08-30 1167/week @ 2024-09-06 833/week @ 2024-09-13 961/week @ 2024-09-20 654/week @ 2024-09-27

3,836 downloads per month
Used in 3 crates (2 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

~2–2.8MB
~56K SLoC