#markup-language #formatter #formatting #vue #html #svelte #astro

markup_fmt

Configurable HTML, Vue, Svelte, Astro, Angular, Jinja, Twig, Nunjucks and Vento formatter

27 releases (14 breaking)

new 0.15.1 Nov 1, 2024
0.14.0 Oct 21, 2024
0.11.0 Jul 19, 2024
0.6.0 Mar 2, 2024
0.2.1 Nov 11, 2023

#27 in Template engine

Download history 191/week @ 2024-07-18 165/week @ 2024-07-25 56/week @ 2024-08-01 211/week @ 2024-08-08 393/week @ 2024-08-15 738/week @ 2024-08-22 833/week @ 2024-08-29 1186/week @ 2024-09-05 919/week @ 2024-09-12 1173/week @ 2024-09-19 1189/week @ 2024-09-26 1162/week @ 2024-10-03 1701/week @ 2024-10-10 1632/week @ 2024-10-17 1762/week @ 2024-10-24 1489/week @ 2024-10-31

6,915 downloads per month
Used in 7 crates (4 directly)

MIT license

230KB
6K SLoC

markup_fmt is a configurable HTML, Vue, Svelte, Astro, Angular, Jinja, Twig, Nunjucks and Vento formatter.

Basic Usage

You can format source code string by using format_text function.

use markup_fmt::{config::FormatOptions, format_text, Language};

let options = FormatOptions::default();
assert_eq!("<div class=\"container\"></div>\n", &format_text(
    "<div class=container></div>",
    Language::Html,
    &options,
    |code, _| Ok::<_, std::convert::Infallible>(code.into()),
).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 markup_fmt::{config::FormatOptions, format_text, FormatError, Language, SyntaxError};

let options = FormatOptions::default();
assert!(matches!(
    format_text(
        "<div>",
        Language::Html,
        &options,
        |code, _| Ok::<_, std::convert::Infallible>(code.into()),
    ).unwrap_err(),
    FormatError::Syntax(SyntaxError { .. })
));

External formatter can return [Err] as well. This error will be aggregated and returned in FormatError::External:

use markup_fmt::{config::FormatOptions, format_text, FormatError, Language};

struct ExternalFormatterError;

let options = FormatOptions::default();
assert!(matches!(
    format_text(
        "<script>a</script>",
        Language::Html,
        &options,
        |_, _| Err(ExternalFormatterError),
    ).unwrap_err(),
    FormatError::External(errors) if !errors.is_empty()
));

Dependencies

~3MB
~45K SLoC