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

markup_fmt

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

30 releases (17 breaking)

0.18.0 Dec 2, 2024
0.17.0 Nov 28, 2024
0.16.0 Nov 20, 2024
0.11.0 Jul 19, 2024
0.2.1 Nov 11, 2023

#28 in Template engine

Download history 680/week @ 2024-08-23 896/week @ 2024-08-30 1104/week @ 2024-09-06 819/week @ 2024-09-13 1462/week @ 2024-09-20 982/week @ 2024-09-27 1355/week @ 2024-10-04 1705/week @ 2024-10-11 1522/week @ 2024-10-18 1822/week @ 2024-10-25 1438/week @ 2024-11-01 1082/week @ 2024-11-08 1119/week @ 2024-11-15 1118/week @ 2024-11-22 1291/week @ 2024-11-29 1135/week @ 2024-12-06

4,780 downloads per month
Used in 9 crates (6 directly)

MIT license

235KB
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