#formatter #configurable #smart #css #syntax #sass #formatting

malva

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

9 unstable releases (3 breaking)

new 0.3.1 May 10, 2024
0.3.0 Apr 11, 2024
0.2.0 Mar 24, 2024
0.1.5 Feb 18, 2024
0.0.0 Sep 6, 2023

#1599 in Web programming

Download history 48/week @ 2024-01-29 123/week @ 2024-02-12 58/week @ 2024-02-19 94/week @ 2024-02-26 50/week @ 2024-03-04 78/week @ 2024-03-11 81/week @ 2024-03-18 23/week @ 2024-03-25 31/week @ 2024-04-01 165/week @ 2024-04-08 14/week @ 2024-04-29 172/week @ 2024-05-06

191 downloads per month

MIT license

225KB
6K SLoC

Malva is a configurable, smart and fast CSS/SCSS/Sass/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, line_bounds, Syntax::Css, &options));

Dependencies

~3.5MB
~78K SLoC