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

malva

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

21 releases (11 breaking)

0.11.0 Oct 21, 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

#302 in Web programming

Download history 273/week @ 2024-07-29 511/week @ 2024-08-05 694/week @ 2024-08-12 851/week @ 2024-08-19 683/week @ 2024-08-26 971/week @ 2024-09-02 1171/week @ 2024-09-09 879/week @ 2024-09-16 846/week @ 2024-09-23 839/week @ 2024-09-30 1617/week @ 2024-10-07 1494/week @ 2024-10-14 1547/week @ 2024-10-21 1413/week @ 2024-10-28 1190/week @ 2024-11-04 842/week @ 2024-11-11

5,105 downloads per month
Used in 6 crates (3 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.7MB
~55K SLoC