Skip to content

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

Rust

web: strip leading H1 from page body to stop title duplication

AkitaOnRails3f96e46b4e19
diff --git a/crates/ai-memory-web/src/markdown.rs b/crates/ai-memory-web/src/markdown.rs
index cbabe41c..0530ab7f 100644
--- a/crates/ai-memory-web/src/markdown.rs
+++ b/crates/ai-memory-web/src/markdown.rs
@@ -23,6 +23,33 @@ pub fn render(body: &str) -> String {
out
}
+/// Drop the leading H1 from a markdown body if present. Static-site
+/// convention: the first H1 IS the page title, and the page template
+/// already renders the title in its header — leaving it in the body
+/// duplicates it on screen. No-op when the body doesn't start with
+/// an H1 (handles `# Title`, both ATX `# Title` and setext
+/// `Title\n=====` forms).
+#[must_use]
+pub fn strip_leading_h1(body: &str) -> &str {
+ // Skip any leading blank lines.
+ let trimmed = body.trim_start_matches(['\n', '\r']);
+ // ATX form: `# Title` (one `#`, NOT `## …`).
+ if let Some(rest) = trimmed.strip_prefix("# ") {
+ let after_line = rest.find('\n').map_or("", |nl| &rest[nl + 1..]);
+ return after_line.trim_start_matches(['\n', '\r']);
+ }
+ // 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 == '=')
+ {
+ return after_second.trim_start_matches(['\n', '\r']);
+ }
+ body
+}
+
#[cfg(test)]
mod tests {
use super::*;
@@ -41,4 +68,41 @@ mod tests {
assert!(html.contains("<table>"));
assert!(html.contains("<td>1</td>"));
}
+
+ #[test]
+ fn strip_atx_h1_drops_first_heading() {
+ let out = strip_leading_h1("# Title\n\nbody text\n");
+ assert_eq!(out, "body text\n");
+ }
+
+ #[test]
+ fn strip_atx_h1_tolerates_leading_blank_lines() {
+ let out = strip_leading_h1("\n\n# Title\n\nbody\n");
+ assert_eq!(out, "body\n");
+ }
+
+ #[test]
+ fn strip_atx_h1_leaves_h2_alone() {
+ let out = strip_leading_h1("## Subhead\n\nbody\n");
+ assert_eq!(out, "## Subhead\n\nbody\n");
+ }
+
+ #[test]
+ fn strip_atx_h1_leaves_body_without_title_alone() {
+ let out = strip_leading_h1("just a paragraph\n");
+ assert_eq!(out, "just a paragraph\n");
+ }
+
+ #[test]
+ fn strip_setext_h1_drops_first_heading() {
+ let out = strip_leading_h1("Title\n=====\n\nbody\n");
+ assert_eq!(out, "body\n");
+ }
+
+ #[test]
+ fn strip_does_not_eat_setext_h2() {
+ // `----` underlines are H2, not H1. Leave them alone.
+ let out = strip_leading_h1("Title\n----\n\nbody\n");
+ assert_eq!(out, "Title\n----\n\nbody\n");
+ }
}
diff --git a/crates/ai-memory-web/src/routes/page.rs b/crates/ai-memory-web/src/routes/page.rs
index e6d292f1..e9e44fa3 100644
--- a/crates/ai-memory-web/src/routes/page.rs
+++ b/crates/ai-memory-web/src/routes/page.rs
@@ -33,7 +33,9 @@ pub(crate) async fn handler(
Err(_) => return not_found_response(),
};
- let body_html = markdown::render(&markdown_doc.body);
+ // Drop the leading H1 — the template already renders the title
+ // in its header, so leaving it in the body duplicates it.
+ let body_html = markdown::render(markdown::strip_leading_h1(&markdown_doc.body));
match (PageView {
workspace,