1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
use dprint_core::formatting::*;
use dprint_core::configuration::{resolve_new_line_kind};
use super::configuration::Configuration;
use super::parsing::{parse_cmark_ast, parse_yaml_header, parse_node, Context};
pub fn format_text(
file_text: &str,
config: &Configuration,
format_code_block_text: Box<dyn Fn(&str, &str, u32) -> Result<String, String>>,
) -> Result<String, String> {
let yaml_header = parse_yaml_header(file_text);
let markdown_text = match &yaml_header {
Some(yaml_header) => &file_text[yaml_header.range.end..],
None => file_text,
};
let source_file = match parse_cmark_ast(markdown_text) {
Ok(source_file) => {
let mut source_file = source_file;
source_file.yaml_header = yaml_header;
source_file
},
Err(error) => {
return Err(dprint_core::formatting::utils::string_utils::format_diagnostic(
Some((error.range.start, error.range.end)),
&error.message,
file_text
));
}
};
Ok(dprint_core::formatting::format(|| {
let mut context = Context::new(markdown_text, config, format_code_block_text);
let print_items = parse_node(&source_file.into(), &mut context);
print_items
}, PrintOptions {
indent_width: 1,
max_width: config.line_width,
use_tabs: false,
new_line_text: resolve_new_line_kind(file_text, config.new_line_kind),
}))
}