Skip to content

Solution for long term memory for agent coding CLIs and to facilitate handoff between different agent vendors

Rust

fix(web): support CRLF and trailing whitespace in setext h1 stripping

Vinícius DS Andrade47ec28b8fdb6
diff --git a/crates/ai-memory-web/src/markdown.rs b/crates/ai-memory-web/src/markdown.rs
index aa8083a4..a7f0c89e 100644
--- a/crates/ai-memory-web/src/markdown.rs
+++ b/crates/ai-memory-web/src/markdown.rs
@@ -480,12 +480,14 @@ pub fn strip_leading_h1<'a>(body: &'a str, title: &str) -> &'a str {
// Setext form: `Title\n====…` (1+ equals signs). Look ahead.
if let Some((first_line, after_first)) = trimmed.split_once('\n')
&& !first_line.is_empty()
- && let Some((second_line, after_second)) = after_first.split_once('\n')
- && !second_line.is_empty()
- && second_line.chars().all(|c| c == '=')
&& first_line.trim() == title
{
- return after_second.trim_start_matches(['\n', '\r']);
+ let (second_line, after_second) =
+ after_first.split_once('\n').unwrap_or((after_first, ""));
+ let underline = second_line.trim_end_matches(['\r', ' ', '\t']);
+ if !underline.is_empty() && underline.chars().all(|c| c == '=') {
+ return after_second.trim_start_matches(['\n', '\r']);
+ }
}
body
}
@@ -653,6 +655,25 @@ mod tests {
assert_eq!(out, "Title\n----\n\nbody\n");
}
+ #[test]
+ fn strip_setext_h1_handles_crlf_and_trailing_whitespace() {
+ let out = strip_leading_h1("Title\r\n=====\r\n\r\nbody\r\n", "Title");
+ assert_eq!(out, "body\r\n");
+
+ let out = strip_leading_h1("Title\n===== \t\n\nbody\n", "Title");
+ assert_eq!(out, "body\n");
+
+ let out = strip_leading_h1("Title\r\n===== \r\n\r\nbody\r\n", "Title");
+ assert_eq!(out, "body\r\n");
+ }
+
+ #[test]
+ fn strip_setext_h1_handles_eof_without_trailing_newline() {
+ assert_eq!(strip_leading_h1("Title\n=====", "Title"), "");
+ assert_eq!(strip_leading_h1("Title\r\n=====\r", "Title"), "");
+ assert_eq!(strip_leading_h1("\r\n\r\nTitle\r\n=====", "Title"), "");
+ }
+
#[test]
fn wikilink_resolves_against_current_project() {
let html = render("see [[notes/foo]] here", "default", "scratch");