Skip to content

Commit

Permalink
fix: ignore empty files (#29)
Browse files Browse the repository at this point in the history
  • Loading branch information
hougesen authored Mar 8, 2024
1 parent 7094106 commit 8648d8c
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ pub fn format_file(config: &MdsfConfig, path: &std::path::Path) -> Result<(), Md

let input = std::fs::read_to_string(path)?;

if input.is_empty() {
println!("{path:#?} was not changed");
return Ok(());
}

let parser = pulldown_cmark::Parser::new_ext(&input, pulldown_cmark::Options::all());

let mut output = String::with_capacity(input.len() + 128);
Expand Down Expand Up @@ -71,11 +76,11 @@ pub fn format_file(config: &MdsfConfig, path: &std::path::Path) -> Result<(), Md
s.finalize(&mut output).map_err(MdsfError::from)?;
}

if config.markdown.enabled {
if config.markdown.enabled && !output.is_empty() {
output = format_snippet(config, &Language::Markdown, &output);
}

if input != output {
if input != output && (input.trim().is_empty() == output.trim().is_empty()) {
println!("{path:#?} was formatted");
return std::fs::write(path, output).map_err(MdsfError::from);
}
Expand Down

0 comments on commit 8648d8c

Please sign in to comment.