mirror of
https://gitlab.com/ArkHost/HelixNotes.git
synced 2026-11-21 19:04:56 +01:00
fix: keep a single title heading when a note's title changes
The heading that reads as the old title follows a title change, in the editor and on disk, and a title heading is only added to a note that has no top-level heading.
This commit is contained in:
1 parent
66fe9c8344
commit
90795bea73
3 files changed
+126
-10
No files matched your search
@@ -184,18 +184,15 @@ pub fn parse_note(raw: &str, filename: &str) -> (NoteMeta, String) {
|
||||
(meta, content)
|
||||
}
|
||||
|
||||
/// Ensure the body starts with `# Title` heading for external app compatibility.
|
||||
/// If the body already starts with `# Title`, leave it as-is.
|
||||
/// Ensure the body starts with a top-level heading for external app compatibility, adding
|
||||
/// `# Title` when it has none. A body that already opens with a top-level heading is left alone
|
||||
/// even when that heading differs from the title: adding another one above it stacked a new
|
||||
/// heading on every title change (`# New`, `# Old`, ...).
|
||||
fn ensure_title_heading(body: &str, title: &str) -> String {
|
||||
let trimmed = body.trim_start();
|
||||
|
||||
// Check if body already starts with `# Title`
|
||||
if let Some(rest) = trimmed.strip_prefix("# ") {
|
||||
let first_line_end = rest.find('\n').unwrap_or(rest.len());
|
||||
let heading_text = rest[..first_line_end].trim();
|
||||
if heading_text.eq_ignore_ascii_case(title.trim()) {
|
||||
return body.to_string();
|
||||
}
|
||||
if trimmed.starts_with("# ") {
|
||||
return body.to_string();
|
||||
}
|
||||
|
||||
// Don't add heading to empty notes (new/blank notes)
|
||||
@@ -686,4 +683,49 @@ mod tests {
|
||||
assert!(content.ends_with("body"));
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_title_heading_is_added_only_when_the_body_has_no_top_level_heading() {
|
||||
let meta = |title: &str| NoteMeta {
|
||||
id: "id".into(),
|
||||
title: title.into(),
|
||||
tags: vec![],
|
||||
pinned: false,
|
||||
created: Utc::now(),
|
||||
modified: Utc::now(),
|
||||
};
|
||||
let body_of = |raw: String| parse_note(&raw, "x.md").1;
|
||||
// No heading yet: the title becomes one.
|
||||
assert_eq!(
|
||||
body_of(update_note_raw(&meta("New"), "Text.\n")),
|
||||
"# New\n\nText."
|
||||
);
|
||||
// A heading that differs from the title is not buried under a second one.
|
||||
assert_eq!(
|
||||
body_of(update_note_raw(&meta("New"), "# Old\n\nText.\n")),
|
||||
"# Old\n\nText."
|
||||
);
|
||||
let original = "---\nid: id\ntitle: Old\n---\n# Old\n\nText.\n";
|
||||
assert_eq!(
|
||||
body_of(merge_frontmatter(
|
||||
original,
|
||||
&meta("New"),
|
||||
"# Old\n\nText.\n"
|
||||
)),
|
||||
"# Old\n\nText."
|
||||
);
|
||||
// Saving again and again never grows the note.
|
||||
let once = merge_frontmatter(original, &meta("New"), "# Old\n\nText.\n");
|
||||
let (_, body) = parse_note(&once, "x.md");
|
||||
assert_eq!(
|
||||
body_of(merge_frontmatter(&once, &meta("Newer"), &body)),
|
||||
"# Old\n\nText."
|
||||
);
|
||||
// Lower-level headings and empty notes behave as before.
|
||||
assert_eq!(
|
||||
body_of(update_note_raw(&meta("New"), "## Section\n")),
|
||||
"# New\n\n## Section"
|
||||
);
|
||||
assert!(update_note_raw(&meta("New"), "\n").ends_with("---\n\n"));
|
||||
}
|
||||
}
|
||||
@@ -840,7 +840,9 @@ pub fn save_note(
|
||||
}
|
||||
|
||||
updated_meta.modified = Utc::now();
|
||||
let raw = frontmatter::merge_frontmatter(&existing.raw, &updated_meta, body);
|
||||
// The heading that carried the old title follows the title.
|
||||
let body = retitle_leading_heading(body, &existing.meta.title, &updated_meta.title);
|
||||
let raw = frontmatter::merge_frontmatter(&existing.raw, &updated_meta, &body);
|
||||
replace_file_atomically(&target, raw.as_bytes())?;
|
||||
|
||||
Ok(Some(SavedNote {
|
||||
@@ -1279,6 +1281,8 @@ pub fn rename_note(path: &str, new_title: &str, vault_path: &str) -> Result<Rena
|
||||
if meta.id.is_empty() {
|
||||
meta.id = Uuid::new_v4().to_string();
|
||||
}
|
||||
// The heading that carried the old title follows the rename.
|
||||
let content = retitle_leading_heading(&content, &old_title, new_title);
|
||||
let updated = frontmatter::merge_frontmatter(&raw, &meta, &content);
|
||||
|
||||
// Rename file
|
||||
@@ -2495,6 +2499,53 @@ pub fn sanitize_filename(name: &str) -> String {
|
||||
mod rename_link_tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn renaming_and_retitling_keep_a_single_title_heading() {
|
||||
let vault = tempfile::tempdir().unwrap();
|
||||
let root = vault.path().to_str().unwrap();
|
||||
let note = vault.path().join("Alpha.md");
|
||||
fs::write(
|
||||
¬e,
|
||||
"---\nid: the-id\ntitle: Alpha\n---\n# Alpha\n\nText.\n",
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
// Renamed twice from the sidebar: the heading follows, nothing stacks.
|
||||
let renamed = rename_note(note.to_str().unwrap(), "Beta", root).unwrap();
|
||||
let renamed = rename_note(&renamed.path, "Gamma", root).unwrap();
|
||||
let raw = fs::read_to_string(&renamed.path).unwrap();
|
||||
assert!(raw.trim_end().ends_with("---\n# Gamma\n\nText."), "{raw}");
|
||||
assert_eq!(raw.matches("\n# ").count(), 1, "{raw}");
|
||||
|
||||
// A heading the user worded differently is theirs and stays.
|
||||
let other = vault.path().join("Other.md");
|
||||
fs::write(
|
||||
&other,
|
||||
"---\nid: other\ntitle: Other\n---\n# My own heading\n\nText.\n",
|
||||
)
|
||||
.unwrap();
|
||||
let renamed = rename_note(other.to_str().unwrap(), "Renamed", root).unwrap();
|
||||
let raw = fs::read_to_string(&renamed.path).unwrap();
|
||||
assert!(
|
||||
raw.trim_end().ends_with("---\n# My own heading\n\nText."),
|
||||
"{raw}"
|
||||
);
|
||||
|
||||
// The save path: a body still carrying the old title's heading is retitled with the title.
|
||||
assert_eq!(
|
||||
retitle_leading_heading("# Gamma\n\nText.\n", "Gamma", "Delta"),
|
||||
"# Delta\n\nText.\n"
|
||||
);
|
||||
assert_eq!(
|
||||
retitle_leading_heading("# Gamma\n\nText.\n", "Gamma", "Gamma"),
|
||||
"# Gamma\n\nText.\n"
|
||||
);
|
||||
assert_eq!(
|
||||
retitle_leading_heading("# Mine\n\nText.\n", "Gamma", "Delta"),
|
||||
"# Mine\n\nText.\n"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_rename_updates_links_to_the_note_and_leaves_other_aliases_alone() {
|
||||
let vault = tempfile::tempdir().unwrap();
|
||||
|
||||
Reference in new issue
Block a user