From c1224edeaace5b0bc3315a9ff9e8709a6724164e Mon Sep 17 00:00:00 2001 From: Adam Israel Date: Sat, 8 Aug 2026 11:23:06 -0400 Subject: [PATCH] fix(em-dash): Strip the spacing before/after an em-dash Fix the formatting of existing em-dash's. --- src/markdown.rs | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/markdown.rs b/src/markdown.rs index ea78846..1bf15e0 100644 --- a/src/markdown.rs +++ b/src/markdown.rs @@ -30,6 +30,15 @@ fn strip_comments(mut content: String) -> String { fn convert_hyphens_to_em_dashes(mut content: String) -> String { let re = Regex::new(r"(\s+--\s+)").unwrap(); content = Regex::replace_all(&re, content.as_str(), "—").to_string(); + + content.trim().to_string() +} + +/// Format em-dashes correctly by stripping the space before/after +fn format_em_dashes(mut content: String) -> String { + let re = Regex::new(r"(\s+—\s+)").unwrap(); + content = Regex::replace_all(&re, content.as_str(), "—").to_string(); + content.trim().to_string() } @@ -51,6 +60,9 @@ fn content_to_paragraphs(mut content: String) -> Vec { // Replace double-hyphens to em-dashes content = convert_hyphens_to_em_dashes(content); + // Strip spaces before/after an em-dash + content = format_em_dashes(content); + // Convert smart quotes to straight quotes content = convert_smart_quotes(content); @@ -356,6 +368,13 @@ mod tests { assert!(content == "This is a test—only a test—he was told."); } + #[test] + fn test_format_em_dash() { + let content = format_em_dashes("This is a test — only a test — he was told.".to_string()); + println!("{content}"); + assert!(content == "This is a test—only a test—he was told."); + } + #[test] fn test_trim_doublespace() { let s = "This is a test. This is only a test.\nIf this were an actual emergency, you would be instructed where to go and what to do.";