format_text

Function format_text 

Source
pub fn format_text<E, F>(
    code: &str,
    language: Language,
    options: &FormatOptions,
    external_formatter: F,
) -> Result<String, FormatError<E>>
where F: for<'a> FnMut(&'a str, Hints<'_>) -> Result<Cow<'a, str>, E>,
Expand description

Format the given source code.

An external formatter is required for formatting code inside <script> or <style> tag. If you don’t need to format them or you don’t have available formatters, you can pass a closure that returns the original code. (see example below)

use markup_fmt::{format_text, Language};

let code = r#"
<html>
   <head>
     <title>Example</title>
     <style>button { outline: none; }</style>
  </head>
  <body><script>const a = 1;</script></body>
</html>"#;

let formatted = format_text(
    code,
    Language::Html,
    &Default::default(),
    |code, _| Ok::<_, std::convert::Infallible>(code.into()),
).unwrap();

For the external formatter closure,

  • The first argument is code that needs formatting.
  • The second argument is hints which contains useful information for external formatters, such as file extension and print width.