Crate markup_fmt
source ·Expand description
markup_fmt is a configurable HTML/Vue/Svelte/Astro/Jinja/Twig/Nunjucks/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::<_, ()>(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::<_, ()>(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(ExternalFormatterError, _)
));
Modules§
- Types about configuration.
Structs§
- Syntax error when parsing tags, not
<script>
or<style>
tag.
Enums§
- The error type for markup_fmt.
- Supported languages.
Functions§
- Detect language from file extension.
- Format the given source code.