From 3b59d34fedf80097aefc7a8525e904911236a672 Mon Sep 17 00:00:00 2001 From: Yuri Karamian Date: Sat, 17 Oct 2026 16:48:30 +0200 Subject: [PATCH] fix: keep links working when notes are renamed or moved Renaming through the title field saved the new title before the file was renamed, so the rename took the new title for the old one and [[Old]] links were never updated. It now uses the old file name. A rename now also updates heading and block links, other capitals, [[Note.md]] and Markdown links to the file. Moving a note, and renaming or moving a notebook, updates links that contain a folder, keeping each link's form, and points the moved notes' own relative links from their new folder. Code is left as written, and a name another note shares is not rewritten. Open notes with unsaved edits follow the same rules, checked on both sides against tests/fixtures/renamed-links.json. --- src-tauri/src/commands.rs | 48 +- src-tauri/src/vault/note_import_tests.rs | 4 +- src-tauri/src/vault/operations.rs | 1017 +++++++++++++++++++--- src/lib/components/Editor.svelte | 34 +- src/lib/editor/renamedLinks.ts | 222 ++++- tests/fixtures/renamed-links.json | 230 +++++ tests/renamed-links.test.mjs | 12 + 7 files changed, 1376 insertions(+), 191 deletions(-) create mode 100644 tests/fixtures/renamed-links.json diff --git a/src-tauri/src/commands.rs b/src-tauri/src/commands.rs index 91f0cd5..bfc149f 100644 --- a/src-tauri/src/commands.rs +++ b/src-tauri/src/commands.rs @@ -72,11 +72,7 @@ struct LinksRewrittenEvent<'a> { path: &'a str, old_raw: &'a str, new_raw: &'a str, - old_ref: &'a str, - new_ref: &'a str, - old_title: &'a str, - new_title: &'a str, - title_rules: bool, + renames: &'a [operations::LinkRename], } // A rename also rewrites the notes that link to the renamed one. Keep their previous state in @@ -85,10 +81,10 @@ struct LinksRewrittenEvent<'a> { fn announce_rewritten_links( app: &AppHandle, vault: &str, - renamed: &operations::RenamedNote, + links: &operations::LinkRewrite, max_versions: u32, ) { - for note in &renamed.links.notes { + for note in &links.notes { let filename = Path::new(¬e.path) .file_name() .unwrap_or_default() @@ -104,11 +100,7 @@ fn announce_rewritten_links( path: ¬e.path, old_raw: ¬e.old_raw, new_raw: ¬e.new_raw, - old_ref: &renamed.links.old_ref, - new_ref: &renamed.links.new_ref, - old_title: &renamed.old_title, - new_title: &renamed.new_title, - title_rules: renamed.links.title_rules, + renames: &links.renames, }, ) { log::error!("Could not notify note windows about updated links: {error}"); @@ -770,9 +762,15 @@ pub async fn rename_notebook( new_name: String, ) -> Result { let search = state.vault_snapshot(&vault_path)?; + let max_versions = state + .config + .lock() + .map_err(|error| error.to_string())? + .max_versions_per_note; let _write_guard = state.note_save.lock().await; - let renamed = operations::rename_notebook(&vault_path, &path, &new_name)?; + let (renamed, links) = operations::rename_notebook(&vault_path, &path, &new_name)?; emit_path_move(&app, &vault_path, &path, &renamed, None); + announce_rewritten_links(&app, &vault_path, &links, max_versions); if let Some(search) = search { if let Err(error) = search.queue_vault_refresh(&vault_path) { log::error!("Failed to queue renamed notebook search refresh: {error}"); @@ -790,11 +788,18 @@ pub async fn move_notebook( dest_parent: String, ) -> Result { let search = state.vault_snapshot(&vault_path)?; + let max_versions = state + .config + .lock() + .map_err(|error| error.to_string())? + .max_versions_per_note; let _write_guard = state.note_save.lock().await; let vault_path = vault_path.as_str(); - let new_full_path = operations::move_notebook(vault_path, ¬ebook_path, &dest_parent)?; + let (new_full_path, links) = + operations::move_notebook(vault_path, ¬ebook_path, &dest_parent)?; emit_path_move(&app, vault_path, ¬ebook_path, &new_full_path, None); + announce_rewritten_links(&app, vault_path, &links, max_versions); // Reconcile renamed paths without blocking the move on a full rebuild. if let Some(search) = search { @@ -1016,7 +1021,7 @@ pub async fn rename_note( let _write_guard = state.note_save.lock().await; let renamed = operations::rename_note(&path, &new_title, &vault_path)?; emit_path_move(&app, &vault_path, &path, &renamed.path, Some(&renamed)); - announce_rewritten_links(&app, &vault_path, &renamed, max_versions); + announce_rewritten_links(&app, &vault_path, &renamed.links, max_versions); if let Some(search) = search { if let Err(error) = search.queue_vault_refresh(&vault_path) { log::error!("Failed to queue renamed note search refresh: {error}"); @@ -1050,14 +1055,23 @@ pub async fn move_note( dest_notebook: String, ) -> Result { let search = state.vault_snapshot(&vault_path)?; + let max_versions = state + .config + .lock() + .map_err(|error| error.to_string())? + .max_versions_per_note; let _write_guard = state.note_save.lock().await; let vault_path = vault_path.as_str(); - let new_full_path = operations::move_note(vault_path, ¬e_path, &dest_notebook)?; + let (new_full_path, links) = operations::move_note(vault_path, ¬e_path, &dest_notebook)?; emit_path_move(&app, vault_path, ¬e_path, &new_full_path, None); + announce_rewritten_links(&app, vault_path, &links, max_versions); index_note_bg(search.clone(), ¬e_path); - index_note_bg(search, &new_full_path); + index_note_bg(search.clone(), &new_full_path); + for note in &links.notes { + index_note_bg(search.clone(), ¬e.path); + } Ok(new_full_path) } diff --git a/src-tauri/src/vault/note_import_tests.rs b/src-tauri/src/vault/note_import_tests.rs index eb9bebd..cc23bd6 100644 --- a/src-tauri/src/vault/note_import_tests.rs +++ b/src-tauri/src/vault/note_import_tests.rs @@ -182,7 +182,9 @@ fn imported_document_links_remain_managed_and_stable_after_note_and_notebook_mov .as_ref(), ); assert_eq!(fs::read(attachment).unwrap(), b"pdf"); - let moved = super::operations::move_note(&vault, ¬e.path, &vault).unwrap(); + let moved = super::operations::move_note(&vault, ¬e.path, &vault) + .unwrap() + .0; assert_eq!(fs::read_to_string(moved).unwrap(), note.raw); // Imported managed paths remain stable when their containing notebook is moved too. let second = import_note( diff --git a/src-tauri/src/vault/operations.rs b/src-tauri/src/vault/operations.rs index e3c1c0f..cec5d9f 100644 --- a/src-tauri/src/vault/operations.rs +++ b/src-tauri/src/vault/operations.rs @@ -1320,12 +1320,10 @@ pub struct RewrittenNote { pub new_raw: String, } -/// What a rename changed in the notes that link to the renamed one. +/// What a rename or move changed in the notes that link to the moved ones. pub struct LinkRewrite { - pub old_ref: String, - pub new_ref: String, - /// Whether links written with the note's title were updated too. - pub title_rules: bool, + /// The changes the notes were rewritten for, so open editors can apply them to unsaved edits. + pub renames: Vec, pub notes: Vec, } @@ -1342,6 +1340,16 @@ pub fn rename_note(path: &str, new_title: &str, vault_path: &str) -> Result Result Result, + by_old_ref: std::collections::HashMap, +} + +impl LinkRenames { + pub(crate) fn new(list: Vec) -> Self { + let by_old_ref = list + .iter() + .enumerate() + .map(|(index, rename)| (rename.old_ref.to_lowercase(), index)) + .collect(); + Self { list, by_old_ref } + } + + fn by_path(&self, reference: &str) -> Option<&LinkRename> { + self.by_old_ref + .get(&reference.to_lowercase()) + .map(|index| &self.list[*index]) + } +} + +fn split_ref(reference: &str) -> (&str, &str) { + reference.rsplit_once('/').unwrap_or(("", reference)) +} + +fn join_ref(dir: &str, name: &str) -> String { + if dir.is_empty() { + name.to_string() + } else { + format!("{dir}/{name}") + } +} + +/// Resolve `.` and `..` in a slash path; `None` when it climbs out of the vault. +fn normalize_link_dir(path: &str) -> Option { + let mut parts: Vec<&str> = Vec::new(); + for part in path.split('/') { + match part { + "" | "." => {} + ".." => { + parts.pop()?; + } + _ => parts.push(part), + } + } + Some(parts.join("/")) +} + +/// The `../`-style prefix that leads from folder `from` to folder `to`, ending in `/` unless empty. +fn relative_link_dir(from: &str, to: &str) -> String { + let from: Vec<&str> = from.split('/').filter(|part| !part.is_empty()).collect(); + let to: Vec<&str> = to.split('/').filter(|part| !part.is_empty()).collect(); + let common = from.iter().zip(&to).take_while(|(a, b)| a == b).count(); + let mut out = "../".repeat(from.len() - common); + for part in &to[common..] { + out.push_str(part); + out.push('/'); + } + out +} + +/// Characters a link destination without `<>` cannot hold as they are. +fn encode_link_file_name(name: &str) -> String { + let mut out = String::with_capacity(name.len()); + for c in name.chars() { + if matches!(c, '%' | ' ' | '\t' | '(' | ')' | '<' | '>' | '#') { + out.push_str(&format!("%{:02X}", c as u32)); + } else { + out.push(c); + } + } + out +} + +fn encode_link_path(path: &str, angled: bool) -> String { + if angled { + return path.to_string(); + } + path.split('/') + .map(encode_link_file_name) + .collect::>() + .join("/") +} + +fn decode_link(value: &str) -> String { + urlencoding::decode(value) + .map(|decoded| decoded.into_owned()) + .unwrap_or_else(|_| value.to_string()) +} + +/// `name` without a trailing `.md` (any capitals), and that extension as written. +fn split_md_ext(name: &str) -> (&str, &str) { + let at = name.len().saturating_sub(3); + if name.len() >= 3 && name.is_char_boundary(at) && name[at..].eq_ignore_ascii_case(".md") { + name.split_at(at) + } else { + (name, "") + } +} + +/// The wiki link target and display after the renames, or `None` when the link is not affected. +/// The target may carry a heading or block (`Note#Part`, `Note^id`) and a `.md` extension. +fn renamed_wiki_link( + reference: &str, + display: Option<&str>, + renames: &LinkRenames, +) -> Option<(String, Option)> { + let anchor_at = reference.find(['#', '^']).unwrap_or(reference.len()); + let (name, anchor) = reference.split_at(anchor_at); + let (base, ext) = split_md_ext(name); + let lower = base.to_lowercase(); + let (rename, next) = if let Some(rename) = renames + .list + .iter() + .find(|rename| rename.title_rules && rename.old_title.to_lowercase() == lower) + { + (rename, rename.new_title.as_str()) + } else { + let rename = renames.by_path(&base.replace('\\', "/"))?; + let stem_changes = split_ref(&rename.old_ref).1 != split_ref(&rename.new_ref).1; + // At the top of the vault the path is the bare name, as unsure as a title link. + if rename.old_ref == rename.new_ref + || !(rename.old_ref.contains('/') || (rename.file_rules && stem_changes)) + { + return None; + } + (rename, rename.new_ref.as_str()) + }; + let follows_title = + display == Some(rename.old_title.as_str()) && rename.old_title != rename.new_title; + let display = if follows_title { + Some(rename.new_title.clone()) + } else { + display.map(str::to_string) + }; + Some((format!("{next}{ext}{anchor}"), display)) +} + +/// A link destination split into what the renames look at. +struct LinkDestination<'a> { + angled: bool, + /// Path before any `#` part, still encoded. + path: &'a str, + anchor: &'a str, +} + +fn link_destination(href: &str) -> Option> { + let angled = href.len() >= 2 && href.starts_with('<') && href.ends_with('>'); + let dest = if angled { + &href[1..href.len() - 1] + } else { + href + }; + let has_scheme = dest.split_once(':').is_some_and(|(scheme, _)| { + scheme + .chars() + .next() + .is_some_and(|c| c.is_ascii_alphabetic()) + && scheme + .chars() + .all(|c| c.is_ascii_alphanumeric() || matches!(c, '+' | '.' | '-')) + }); + if has_scheme || dest.is_empty() || dest.starts_with('#') { + return None; + } + let (path, anchor) = dest.split_at(dest.find('#').unwrap_or(dest.len())); + Some(LinkDestination { + angled, + path, + anchor, + }) +} + +fn wrap_destination(angled: bool, path: String, anchor: &str) -> String { + if angled { + format!("<{path}{anchor}>") + } else { + format!("{path}{anchor}") + } +} + +/// A Markdown link destination after the renames, or `None` when it does not point at a renamed +/// note. `note_dir` is the vault-relative folder of the note holding the link. The link keeps its +/// form: relative stays relative, from the vault root stays so, and a bare name stays bare when it +/// still finds the note. +fn renamed_href(href: &str, note_dir: &str, renames: &LinkRenames) -> Option { + let LinkDestination { + angled, + path, + anchor, + } = link_destination(href)?; + let (raw_dir, raw_file) = match path.rfind('/') { + Some(slash) => (&path[..=slash], &path[slash + 1..]), + None => ("", path), + }; + let (_, ext) = split_md_ext(raw_file); + if ext.is_empty() { + return None; + } + let file = decode_link(raw_file); + let stem = split_md_ext(&file).0; + let dir = decode_link(raw_dir).replace('\\', "/"); + enum Form { + Bare, + Relative, + Root, + } + let found = if dir.is_empty() { + renames + .by_path(&join_ref(note_dir, stem)) + .map(|rename| (rename, Form::Relative)) + .or_else(|| { + // A bare name from another folder finds the note by its unique file name. + renames + .list + .iter() + .find(|rename| { + let (_, old_stem) = split_ref(&rename.old_ref); + rename.file_rules + && old_stem.to_lowercase() == stem.to_lowercase() + && old_stem != split_ref(&rename.new_ref).1 + }) + .map(|rename| (rename, Form::Bare)) + }) + } else { + let relative = (!dir.starts_with('/')) + .then(|| normalize_link_dir(&format!("{note_dir}/{dir}"))) + .flatten() + .and_then(|resolved| renames.by_path(&join_ref(&resolved, stem))) + .map(|rename| (rename, Form::Relative)); + relative.or_else(|| { + normalize_link_dir(&dir) + .and_then(|resolved| renames.by_path(&join_ref(&resolved, stem))) + .map(|rename| (rename, Form::Root)) + }) + }; + let (rename, form) = found?; + if rename.old_ref == rename.new_ref { + return None; + } + let (old_dir, _) = split_ref(&rename.old_ref); + let (new_dir, new_stem) = split_ref(&rename.new_ref); + let file = encode_link_path(&format!("{new_stem}{ext}"), angled); + let prefix = if old_dir.to_lowercase() == new_dir.to_lowercase() { + raw_dir.to_string() + } else { + match form { + Form::Bare => String::new(), + Form::Relative => encode_link_path(&relative_link_dir(note_dir, new_dir), angled), + Form::Root => { + let lead = if dir.starts_with('/') { "/" } else { "" }; + let rest = if new_dir.is_empty() { + String::new() + } else { + format!("{}/", encode_link_path(new_dir, angled)) + }; + format!("{lead}{rest}") + } + } + }; + Some(wrap_destination(angled, format!("{prefix}{file}"), anchor)) +} + +/// A relative link in a note that moved from `old_dir` to `new_dir`, pointed from the new folder at +/// what it reached from the old one (which may have moved too). Links that did not reach an +/// existing file, managed attachments and links from the vault root are left alone. +fn relinked_href( + href: &str, + old_dir: &str, + new_dir: &str, + renames: &LinkRenames, + exists: &dyn Fn(&str) -> bool, +) -> Option { + let LinkDestination { + angled, + path, + anchor, + } = link_destination(href)?; + let decoded = decode_link(path).replace('\\', "/"); + if decoded.starts_with('/') + || decoded + .trim_start_matches("./") + .trim_start_matches("../") + .starts_with(".helixnotes/") + || decoded.contains("/.helixnotes/") + { + return None; + } + // Written from the vault root, the link still works from the new folder. + if normalize_link_dir(&decoded).is_some_and(|root| exists(&root)) { + return None; + } + let target = normalize_link_dir(&format!("{old_dir}/{decoded}"))?; + let (base, ext) = split_md_ext(&target); + let target = match renames.by_path(base) { + Some(rename) if !ext.is_empty() => format!("{}{ext}", rename.new_ref), + _ => target.clone(), + }; + if !exists(&target) { + return None; + } + let (target_dir, target_file) = split_ref(&target); + let next = format!( + "{}{}", + encode_link_path(&relative_link_dir(new_dir, target_dir), angled), + encode_link_path(target_file, angled) + ); + let next = wrap_destination(angled, next, anchor); + (next != href).then_some(next) +} + +/// Byte ranges of `text` outside fenced code blocks and inline code, where links are links. +fn link_prose_ranges(text: &str) -> Vec<(usize, usize)> { + let mut ranges = Vec::new(); + let mut fence: Option<(u8, usize)> = None; + let mut offset = 0; + for line in text.split_inclusive('\n') { + let start = offset; + offset += line.len(); + let content = line.trim_end_matches(['\n', '\r']); + let trimmed = content.trim_start_matches(' '); + let opening = (content.len() - trimmed.len() <= 3) + .then(|| trimmed.as_bytes().first().copied()) + .flatten() + .filter(|c| *c == b'`' || *c == b'~') + .map(|c| (c, trimmed.bytes().take_while(|b| *b == c).count())) + .filter(|(_, run)| *run >= 3); + if let Some((char, length)) = fence { + if let Some((c, run)) = opening { + if c == char && run >= length && trimmed[run..].trim().is_empty() { + fence = None; + } + } + continue; + } + if let Some((c, run)) = opening { + if !(c == b'`' && trimmed[run..].contains('`')) { + fence = Some((c, run)); + continue; + } + } + let bytes = line.as_bytes(); + let mut from = start; + let mut i = 0; + while i < bytes.len() { + if bytes[i] != b'`' { + i += 1; + continue; + } + let run = bytes[i..].iter().take_while(|b| **b == b'`').count(); + let mut j = i + run; + let mut close = None; + while j < bytes.len() { + if bytes[j] == b'`' { + let other = bytes[j..].iter().take_while(|b| **b == b'`').count(); + if other == run { + close = Some(j + other); + break; + } + j += other; + } else { + j += 1; + } + } + match close { + Some(end) => { + ranges.push((from, start + i)); + from = start + end; + i = end; + } + None => i += run, + } + } + ranges.push((from, offset)); + } + ranges +} + +fn wiki_link_pattern() -> &'static regex::Regex { + static PATTERN: std::sync::OnceLock = std::sync::OnceLock::new(); + PATTERN.get_or_init(|| regex::Regex::new(r"\[\[([^\[\]\n]+?)\]\]").unwrap()) +} + +fn markdown_link_pattern() -> &'static regex::Regex { + static PATTERN: std::sync::OnceLock = std::sync::OnceLock::new(); + PATTERN.get_or_init(|| { + regex::Regex::new( + r"\[([^\[\]\n]*)\]\((<[^<>\n]*>|(?:[^\s()<>]|\([^\s()<>]*\))+)([^)\n]*)\)", + ) + .unwrap() + }) +} + +/// Where a note's own relative Markdown links are fixed after the note itself moved. +pub(crate) struct MovedFrom<'a> { + pub old_dir: &'a str, + pub exists: &'a dyn Fn(&str) -> bool, +} + +/// `text` with its links to renamed or moved notes brought in line. A note that moved itself +/// passes `moved`, so its relative Markdown links are pointed from its new folder. +pub(crate) fn rewrite_renamed_links( + text: &str, + renames: &LinkRenames, + note_dir: &str, + moved: Option<&MovedFrom>, +) -> String { + let mut changes: Vec<(usize, usize, String)> = Vec::new(); + for (from, to) in link_prose_ranges(text) { + let prose = &text[from..to]; + for found in wiki_link_pattern().captures_iter(prose) { + let whole = found.get(0).unwrap(); + let inner = &found[1]; + // In a table cell the alias separator is written `\|`, since a bare `|` ends the cell. + let (reference, separator, display) = match inner.find('|') { + Some(bar) if bar > 0 && inner.as_bytes()[bar - 1] == b'\\' => { + (&inner[..bar - 1], "\\|", Some(&inner[bar + 1..])) + } + Some(bar) => (&inner[..bar], "|", Some(&inner[bar + 1..])), + None => (inner, "|", None), + }; + let Some((next, display)) = renamed_wiki_link(reference, display, renames) else { + continue; + }; + let insert = match display { + Some(display) => format!("[[{next}{separator}{display}]]"), + None => format!("[[{next}]]"), + }; + if insert != whole.as_str() { + changes.push((from + whole.start(), from + whole.end(), insert)); + } + } + for found in markdown_link_pattern().captures_iter(prose) { + let whole = found.get(0).unwrap(); + let (href, label) = match moved { + Some(moved) => ( + relinked_href(&found[2], moved.old_dir, note_dir, renames, moved.exists), + &found[1], + ), + None => { + let href = renamed_href(&found[2], note_dir, renames); + // Link text that was the old title follows the new one. + let label = renames + .list + .iter() + .find(|rename| { + rename.old_title == found[1] && rename.old_title != rename.new_title + }) + .filter(|_| href.is_some()) + .map_or(&found[1], |rename| rename.new_title.as_str()); + (href, label) + } + }; + let Some(href) = href else { + continue; + }; + let insert = format!("[{label}]({href}{})", &found[3]); + if insert != whole.as_str() { + changes.push((from + whole.start(), from + whole.end(), insert)); + } + } + } + if changes.is_empty() { + return text.to_string(); + } + changes.sort_by_key(|change| change.0); + let mut result = String::with_capacity(text.len()); + let mut at = 0; + for (start, end, insert) in changes { + if start < at { + continue; + } + result.push_str(&text[at..start]); + result.push_str(&insert); + at = end; + } + result.push_str(&text[at..]); + result +} + +fn vault_notes(vault_path: &str) -> impl Iterator { + let vault = Path::new(vault_path).to_path_buf(); + let hn_dir = helixnotes_dir(vault_path); + WalkDir::new(vault.clone()) + .into_iter() + .filter_entry(move |e| { + e.depth() == 0 || (!is_hidden(e.path()) && !e.path().starts_with(&hn_dir)) + }) + .filter_map(|e| e.ok()) + .filter(|e| { + e.path().is_file() && e.path().extension().and_then(|ext| ext.to_str()) == Some("md") + }) +} + +/// Vault-relative path with forward slashes and without `.md`. +fn vault_ref(vault_path: &str, path: &Path) -> String { + let rel = path + .strip_prefix(vault_path) + .map(|r| r.to_string_lossy().into_owned()) + .unwrap_or_else(|_| path.to_string_lossy().into_owned()) + .replace('\\', "/"); + split_md_ext(&rel).0.to_string() +} + +/// How many notes in the vault have each file name, ignoring capitals. +fn note_name_counts(vault_path: &str) -> std::collections::HashMap { + let mut counts = std::collections::HashMap::new(); + for entry in vault_notes(vault_path) { + if let Some(stem) = entry.path().file_stem().and_then(|s| s.to_str()) { + *counts.entry(stem.to_lowercase()).or_insert(0) += 1; + } + } + counts +} + +/// Rewrite the links in every note of the vault after notes were renamed or moved. `moved` lists +/// notes that changed folder (new path, old vault-relative folder); their own relative links are +/// fixed too. `skip` is left alone entirely. Code blocks and inline code are left as written. +fn rewrite_links_in_vault( vault_path: &str, - old_path: &str, - new_path: &str, - old_title: &str, - new_title: &str, + renames: Vec, + moved: &[(PathBuf, String)], + skip: Option<&str>, ) -> LinkRewrite { let vault = Path::new(vault_path); - let hn_dir = helixnotes_dir(vault_path); - - // Vault-relative path ref (without .md), normalized to forward slashes so it - // matches [[folder/note]] wikilinks on Windows (OS paths use backslashes there). - let rel_ref = |p: &str| -> String { - let rel = Path::new(p) - .strip_prefix(vault) - .map(|r| r.to_string_lossy().into_owned()) - .unwrap_or_else(|_| p.to_string()) - .replace('\\', "/"); - rel.strip_suffix(".md").unwrap_or(&rel).to_string() - }; - let old_rel_ref = rel_ref(old_path); - let new_rel_ref = rel_ref(new_path); - - // Check if another note with the same old_title exists in the vault. - // If so, title-based replacements (rules 1-3) are ambiguous and must be skipped, - // because we can't tell which [[Old Title]] ref points to the renamed note - // vs. the other note with the same title. - let title_is_unique = !WalkDir::new(vault) - .into_iter() - .filter_entry(|e| { - e.depth() == 0 || (!is_hidden(e.path()) && !e.path().starts_with(&hn_dir)) - }) - .filter_map(|e| e.ok()) - .any(|e| { - let p = e.path(); - if !p.is_file() || p.to_string_lossy().as_ref() == new_path { - return false; - } - if p.extension().and_then(|ext| ext.to_str()) != Some("md") { - return false; - } - // Check if this note's filename (without .md) matches the old title - p.file_stem() - .and_then(|s| s.to_str()) - .map(|s| s.eq_ignore_ascii_case(old_title)) - .unwrap_or(false) - }); - let title_rules = old_title != new_title && title_is_unique; + let renames = LinkRenames::new(renames); + let exists = |rel: &str| vault.join(rel).is_file(); let mut notes = Vec::new(); - - for entry in WalkDir::new(vault) - .into_iter() - .filter_entry(|e| { - e.depth() == 0 || (!is_hidden(e.path()) && !e.path().starts_with(&hn_dir)) - }) - .filter_map(|e| e.ok()) - { + for entry in vault_notes(vault_path) { let path = entry.path(); - if !path.is_file() { - continue; - } let path_str = path.to_string_lossy(); - if path.extension().and_then(|e| e.to_str()) != Some("md") { + if skip == Some(path_str.as_ref()) { continue; } - // Skip the renamed note itself - if *path_str == *new_path { - continue; - } - let content = match fs::read_to_string(path) { Ok(c) => c, Err(_) => continue, }; - - let mut result = content.clone(); - - // Notes are saved as markdown with [[ref]] or [[ref|display]] syntax. - // Update all wikilink forms that reference the renamed note: - - // Title-based rules only apply when the old title is unique in the vault. - // If another note shares the same title, these would be ambiguous. - if title_rules { - // 1. Short title ref: [[Old Title]] → [[New Title]] - result = result.replace(&format!("[[{}]]", old_title), &format!("[[{}]]", new_title)); - - // 2. A link to the renamed note that displays its old title follows the new one: - // [[Old Title|Old Title]] → [[New Title|New Title]]. Only links to this note: an - // alias on a link to another note ([[Index|Old Title]]) is that note's wording. - // In a table cell the separator is written `\|`, since a bare `|` ends the cell. - for bar in ["|", "\\|"] { - result = result.replace( - &format!("[[{old_title}{bar}{old_title}]]"), - &format!("[[{new_title}{bar}{new_title}]]"), - ); - - // 3. Short title with any other alias: [[Old Title|display]] → [[New Title|display]] - result = result.replace( - &format!("[[{old_title}{bar}"), - &format!("[[{new_title}{bar}"), - ); - } - } - - // Path-based rules are always safe (paths are unique). - if old_rel_ref != new_rel_ref { - // 4. Path-based ref: [[folder/Old Name]] → [[folder/New Name]] - result = result.replace( - &format!("[[{}]]", old_rel_ref), - &format!("[[{}]]", new_rel_ref), - ); - - // 5. Path-based ref with alias: [[folder/Old Name|display]] → [[folder/New Name|display]] - // Also update the alias if it matches the old title. - // e.g. [[folder/Old Name|Old Title]] → [[folder/New Name|New Title]] - for bar in ["|", "\\|"] { - if old_title != new_title { - result = result.replace( - &format!("[[{old_rel_ref}{bar}{old_title}]]"), - &format!("[[{new_rel_ref}{bar}{new_title}]]"), - ); - } - // For aliases that DON'T match the old title, just update the ref part - result = result.replace( - &format!("[[{old_rel_ref}{bar}"), - &format!("[[{new_rel_ref}{bar}"), - ); - } - } + let note_dir = split_ref(&vault_ref(vault_path, path)).0.to_string(); + let moved_from = + moved + .iter() + .find(|(moved_path, _)| moved_path == path) + .map(|(_, old_dir)| MovedFrom { + old_dir, + exists: &exists, + }); + let result = rewrite_renamed_links(&content, &renames, ¬e_dir, moved_from.as_ref()); if result != content { // Replace the file in one step: a plain write truncates first, so a crash or a full @@ -1545,34 +1987,117 @@ fn update_wikilinks_after_rename( new_raw: result, }), Err(error) => log::error!( - "Could not update links in {} after a rename: {error}", + "Could not update links in {} after a rename or move: {error}", path.display() ), } } } LinkRewrite { - old_ref: old_rel_ref, - new_ref: new_rel_ref, - title_rules, + renames: renames.list, notes, } } -pub fn rename_notebook(vault_path: &str, path: &str, new_name: &str) -> Result { +/// Update the links to a note after it was renamed in its folder: wiki links by title or path +/// (with headings, blocks, any capitals or `.md`) and Markdown links to the file. +fn update_wikilinks_after_rename( + vault_path: &str, + old_path: &str, + new_path: &str, + old_title: &str, + new_title: &str, +) -> LinkRewrite { + let old_ref = vault_ref(vault_path, Path::new(old_path)); + let new_ref = vault_ref(vault_path, Path::new(new_path)); + let old_stem = split_ref(&old_ref).1.to_lowercase(); + let new_stem = split_ref(&new_ref).1.to_lowercase(); + // When another note has the old title or file name, links by title (or by bare file name) + // can't be told apart from links to that note, so they are left alone. Paths are unique. + let counts = note_name_counts(vault_path); + let others = |name: &str| { + let name = name.to_lowercase(); + counts.get(&name).copied().unwrap_or(0) - usize::from(name == new_stem) + }; + let rename = LinkRename { + title_rules: old_title != new_title && others(old_title) == 0, + file_rules: others(&old_stem) == 0, + old_ref, + new_ref, + old_title: old_title.to_string(), + new_title: new_title.to_string(), + }; + rewrite_links_in_vault(vault_path, vec![rename], &[], Some(new_path)) +} + +/// Update links after notes changed folder: `moves` pairs each moved note's old path with its new +/// one. Titles do not change, so only links written with a path follow. +fn update_links_after_moves(vault_path: &str, moves: &[(PathBuf, PathBuf)]) -> LinkRewrite { + let counts = note_name_counts(vault_path); + let mut renames = Vec::new(); + let mut moved = Vec::new(); + for (old_path, new_path) in moves { + let old_ref = vault_ref(vault_path, old_path); + let new_ref = vault_ref(vault_path, new_path); + let stem = split_ref(&new_ref).1.to_lowercase(); + let title = split_ref(&new_ref).1.to_string(); + moved.push((new_path.clone(), split_ref(&old_ref).0.to_string())); + renames.push(LinkRename { + file_rules: counts.get(&stem).copied().unwrap_or(0) == 1, + old_ref, + new_ref, + old_title: title.clone(), + new_title: title, + title_rules: false, + }); + } + rewrite_links_in_vault(vault_path, renames, &moved, None) +} + +/// Every note under `new_dir` with the path it had under `old_dir`. +fn notes_moved_with_notebook(old_dir: &Path, new_dir: &Path) -> Vec<(PathBuf, PathBuf)> { + WalkDir::new(new_dir) + .into_iter() + .filter_map(|e| e.ok()) + .filter(|e| { + e.path().is_file() && e.path().extension().and_then(|ext| ext.to_str()) == Some("md") + }) + .filter_map(|e| { + let suffix = e.path().strip_prefix(new_dir).ok()?; + Some((old_dir.join(suffix), e.path().to_path_buf())) + }) + .collect() +} + +pub fn rename_notebook( + vault_path: &str, + path: &str, + new_name: &str, +) -> Result<(String, LinkRewrite), String> { let validated = ensure_notebook_path(vault_path, Path::new(path))?; let src = validated.as_path(); let new_path = src.parent().unwrap().join(safe_child_name(new_name)?); if src == new_path { - return Ok(path.to_string()); + return Ok(( + path.to_string(), + LinkRewrite { + renames: Vec::new(), + notes: Vec::new(), + }, + )); } move_notebook_with_icons(vault_path, src, &new_path)?; - Ok(new_path.to_string_lossy().to_string()) + let links = update_links_after_moves(vault_path, ¬es_moved_with_notebook(src, &new_path)); + Ok((new_path.to_string_lossy().to_string(), links)) } -pub fn move_note(vault_path: &str, note_path: &str, dest_notebook: &str) -> Result { +pub fn move_note( + vault_path: &str, + note_path: &str, + dest_notebook: &str, +) -> Result<(String, LinkRewrite), String> { let validated = ensure_note_path(vault_path, Path::new(note_path))?; let src = validated.as_path(); @@ -1586,14 +2111,22 @@ pub fn move_note(vault_path: &str, note_path: &str, dest_notebook: &str) -> Resu let dest = dest_dir.join(filename); move_note_with_quick_access(vault_path, src, &dest)?; - Ok(dest.to_string_lossy().to_string()) + let links = if dest == src { + LinkRewrite { + renames: Vec::new(), + notes: Vec::new(), + } + } else { + update_links_after_moves(vault_path, &[(src.to_path_buf(), dest.clone())]) + }; + Ok((dest.to_string_lossy().to_string(), links)) } pub fn move_notebook( vault_path: &str, notebook_path: &str, dest_parent: &str, -) -> Result { +) -> Result<(String, LinkRewrite), String> { let validated = ensure_notebook_path(vault_path, Path::new(notebook_path))?; let src = validated.as_path(); @@ -1619,7 +2152,8 @@ pub fn move_notebook( // Destination reservation is performed atomically by move_notebook_with_icons. move_notebook_with_icons(vault_path, src, &dest)?; - Ok(dest.to_string_lossy().to_string()) + let links = update_links_after_moves(vault_path, ¬es_moved_with_notebook(src, &dest)); + Ok((dest.to_string_lossy().to_string(), links)) } /// Remove a directory inside trash if it's empty (after restoring/deleting its last note). @@ -2637,6 +3171,202 @@ mod rename_link_tests { ); } + // The same cases are checked against the editor's copy of these rules in renamedLinks.ts. + #[test] + fn shared_rename_cases_rewrite_links_like_the_editor() { + let fixtures: Vec = + serde_json::from_str(include_str!("../../../tests/fixtures/renamed-links.json")) + .unwrap(); + assert!(!fixtures.is_empty()); + for fixture in fixtures { + let list = match fixture.get("renames") { + Some(list) => list.as_array().unwrap().clone(), + None => vec![fixture["rename"].clone()], + }; + let rules = super::LinkRenames::new( + list.iter() + .map(|rename| { + let text = |key: &str| rename[key].as_str().unwrap().to_string(); + super::LinkRename { + old_ref: text("old_ref"), + new_ref: text("new_ref"), + old_title: text("old_title"), + new_title: text("new_title"), + title_rules: rename["title_rules"].as_bool().unwrap(), + file_rules: rename["file_rules"].as_bool().unwrap_or(false), + } + }) + .collect(), + ); + assert_eq!( + super::rewrite_renamed_links( + fixture["input"].as_str().unwrap(), + &rules, + fixture["note_dir"].as_str().unwrap(), + None + ), + fixture["expected"].as_str().unwrap(), + "{}", + fixture["name"] + ); + } + } + + #[test] + fn moving_notes_and_notebooks_keeps_path_links_working() { + let vault = tempfile::tempdir().unwrap(); + let root = vault.path(); + let vault_path = root.to_str().unwrap(); + for dir in ["Work", "Other", "Archive"] { + fs::create_dir_all(root.join(dir)).unwrap(); + } + let write = |rel: &str, body: &str| fs::write(root.join(rel), body).unwrap(); + let read = |rel: &str| fs::read_to_string(root.join(rel)).unwrap(); + write( + "Work/Plan.md", + "# Plan\n[s](Sibling.md) [o](../Other/Linking.md#Top) ![](../.helixnotes/attachments/a.png) [w](https://example.com/x.md) [gone](Missing.md)\n", + ); + write( + "Work/Sibling.md", + "# Sibling\n[p](Plan.md) [[Work/Plan]] [[Plan]]\n", + ); + write( + "Other/Linking.md", + "# Linking\n[[Work/Plan#Goals|g]] [a](../Work/Plan.md) [b](Plan.md) [r](/Work/Plan.md) `[[Work/Plan]]`\n", + ); + + let (moved, links) = super::move_note( + vault_path, + root.join("Work/Plan.md").to_str().unwrap(), + root.join("Archive").to_str().unwrap(), + ) + .unwrap(); + assert!(moved.ends_with("Archive/Plan.md")); + assert_eq!(links.notes.len(), 3); + // The moved note's own relative links point from its new folder; the rest stays. + assert_eq!( + read("Archive/Plan.md"), + "# Plan\n[s](../Work/Sibling.md) [o](../Other/Linking.md#Top) ![](../.helixnotes/attachments/a.png) [w](https://example.com/x.md) [gone](Missing.md)\n" + ); + // A bare name in the old folder becomes a path; title links still find the note. + assert_eq!( + read("Work/Sibling.md"), + "# Sibling\n[p](../Archive/Plan.md) [[Archive/Plan]] [[Plan]]\n" + ); + assert_eq!( + read("Other/Linking.md"), + "# Linking\n[[Archive/Plan#Goals|g]] [a](../Archive/Plan.md) [b](Plan.md) [r](/Archive/Plan.md) `[[Work/Plan]]`\n" + ); + + let (renamed, links) = + super::rename_notebook(vault_path, root.join("Work").to_str().unwrap(), "Projects") + .unwrap(); + assert!(renamed.ends_with("Projects")); + assert_eq!(links.renames.len(), 1); + assert_eq!( + read("Archive/Plan.md"), + "# Plan\n[s](../Projects/Sibling.md) [o](../Other/Linking.md#Top) ![](../.helixnotes/attachments/a.png) [w](https://example.com/x.md) [gone](Missing.md)\n" + ); + // Links between notes that moved together still work as they are. + assert_eq!( + read("Projects/Sibling.md"), + "# Sibling\n[p](../Archive/Plan.md) [[Archive/Plan]] [[Plan]]\n" + ); + + fs::create_dir_all(root.join("Group")).unwrap(); + write( + "Other/Index.md", + "[[Projects/Sibling]] [s](../Projects/Sibling.md)\n", + ); + super::move_notebook( + vault_path, + root.join("Projects").to_str().unwrap(), + root.join("Group").to_str().unwrap(), + ) + .unwrap(); + assert_eq!( + read("Other/Index.md"), + "[[Group/Projects/Sibling]] [s](../Group/Projects/Sibling.md)\n" + ); + assert_eq!( + read("Group/Projects/Sibling.md"), + "# Sibling\n[p](../../Archive/Plan.md) [[Archive/Plan]] [[Plan]]\n" + ); + } + + // Renaming through the title field saves the new title first and renames the file after. + #[test] + fn a_rename_after_the_title_was_saved_still_updates_links_by_the_old_name() { + let vault = tempfile::tempdir().unwrap(); + let root = vault.path(); + let old_path = root.join("Old Plan.md"); + fs::write(&old_path, "---\ntitle: New Plan\n---\n# New Plan\n").unwrap(); + let linking = root.join("Linking.md"); + fs::write( + &linking, + "[[Old Plan]] [[Old Plan#Goals|Old Plan]] [a](Old%20Plan.md)\n", + ) + .unwrap(); + let renamed = super::rename_note( + old_path.to_str().unwrap(), + "New Plan", + root.to_str().unwrap(), + ) + .unwrap(); + assert_eq!(renamed.old_title, "Old Plan"); + assert_eq!( + fs::read_to_string(&linking).unwrap(), + "[[New Plan]] [[New Plan#Goals|New Plan]] [a](New%20Plan.md)\n" + ); + } + + #[test] + fn a_rename_reaches_links_in_other_folders_and_leaves_lookalikes() { + let vault = tempfile::tempdir().unwrap(); + let root = vault.path(); + fs::create_dir_all(root.join("Work")).unwrap(); + fs::create_dir_all(root.join("Other")).unwrap(); + let old_path = root.join("Work/Old Plan.md"); + fs::write(&old_path, "---\ntitle: Old Plan\n---\n# Old Plan\n").unwrap(); + let linking = root.join("Other/Linking.md"); + fs::write( + &linking, + "[[Old Plan#Goals]] [[old plan]] [a](../Work/Old%20Plan.md) [b](Old%20Plan.md)\n`[[Old Plan]]`\n", + ) + .unwrap(); + let renamed = super::rename_note( + old_path.to_str().unwrap(), + "New Plan", + root.to_str().unwrap(), + ) + .unwrap(); + assert!(renamed.links.renames[0].title_rules && renamed.links.renames[0].file_rules); + assert_eq!( + fs::read_to_string(&linking).unwrap(), + "[[New Plan#Goals]] [[New Plan]] [a](../Work/New%20Plan.md) [b](New%20Plan.md)\n`[[Old Plan]]`\n" + ); + + // Another note with the same name elsewhere: only links with a path follow. + fs::write(root.join("Other/Plan B.md"), "# Plan B\n").unwrap(); + fs::write(root.join("Plan B.md"), "# Plan B\n").unwrap(); + fs::write( + &linking, + "[[Plan B]] [a](Plan%20B.md) [b](../Plan%20B.md)\n", + ) + .unwrap(); + let renamed = super::rename_note( + root.join("Plan B.md").to_str().unwrap(), + "Plan C", + root.to_str().unwrap(), + ) + .unwrap(); + assert!(!renamed.links.renames[0].title_rules && !renamed.links.renames[0].file_rules); + assert_eq!( + fs::read_to_string(&linking).unwrap(), + "[[Plan B]] [a](Plan%20B.md) [b](../Plan%20C.md)\n" + ); + } + #[test] fn a_rename_updates_links_to_the_note_and_leaves_other_aliases_alone() { let vault = tempfile::tempdir().unwrap(); @@ -2669,9 +3399,12 @@ mod rename_link_tests { rewrite.notes[0].new_raw, fs::read_to_string(&linking).unwrap() ); - assert!(rewrite.title_rules); + assert!(rewrite.renames[0].title_rules); assert_eq!( - (rewrite.old_ref.as_str(), rewrite.new_ref.as_str()), + ( + rewrite.renames[0].old_ref.as_str(), + rewrite.renames[0].new_ref.as_str() + ), ("Home", "Start") ); @@ -3598,7 +4331,8 @@ mod tests { &renamed.path, root.join("Archive").to_str().unwrap(), ) - .unwrap(); + .unwrap() + .0; assert_eq!( super::load_quick_access(vault_path).unwrap()[0], "Archive/Second.md" @@ -3620,7 +4354,8 @@ mod tests { root.join("Archive").to_str().unwrap(), "Journal", ) - .unwrap(); + .unwrap() + .0; assert_eq!( super::load_quick_access(vault_path).unwrap(), [ diff --git a/src/lib/components/Editor.svelte b/src/lib/components/Editor.svelte index ef579cb..36461af 100644 --- a/src/lib/components/Editor.svelte +++ b/src/lib/components/Editor.svelte @@ -69,7 +69,7 @@ import { externalNoteReferences } from '$lib/editor/externalNote'; import { tagIterationKey } from '$lib/utils/tag-styles'; import { replaceWithWikiLink } from '$lib/editor/wikiLinks'; - import { renamedLink, renamedLinkChanges, type LinkRename } from '$lib/editor/renamedLinks'; + import { renamedHref, renamedLink, renamedLinkChanges, type LinkRename } from '$lib/editor/renamedLinks'; import { assetSourceToMarkdown, assetUrlToLocalPath, encodeMarkdownPath, resolveFileReference, noteAssetRoot, isTrashedNote } from '$lib/utils/paths'; import GraphView from './GraphView.svelte'; import TagSuggestInput from './TagSuggestInput.svelte'; @@ -7380,35 +7380,42 @@ else unlistenPathMoved = fn; }).catch(error => console.error('Failed to listen for relocated notes:', error)); - /** Bring the wiki links of the open note in line with a rename of the note they point to. */ - function applyRenamedLinks(rename: LinkRename) { + /** Bring the links of the open note in line with renamed or moved notes they point to. */ + function applyRenamedLinks(renames: LinkRename[], noteDir: string) { if ($sourceMode) { const view = source.view; if (!view) return; - const changes = renamedLinkChanges(view.state.doc.toString(), rename); + const changes = renamedLinkChanges(view.state.doc.toString(), renames, noteDir); if (changes.length) view.dispatch({ changes }); return; } if (!editor) return; const { schema, doc } = editor.state; const wikiLink = schema.marks.wikiLink; + const link = schema.marks.link; const edits: { from: number; to: number; node: any }[] = []; doc.descendants((node: any, pos: number) => { if (!node.isText) return; const text = node.text as string; const mark = wikiLink ? node.marks.find((m: any) => m.type === wikiLink) : null; - if (mark) { + const linkMark = link ? node.marks.find((m: any) => m.type === link) : null; + const href = linkMark ? renamedHref(String(linkMark.attrs.href ?? ''), noteDir, renames) : null; + if (linkMark && href !== null) { + const marks = node.marks.map((m: any) => m === linkMark ? link.create({ ...linkMark.attrs, href }) : m); + const titled = renames.find((rename) => rename.old_title === text && rename.old_title !== rename.new_title); + edits.push({ from: pos, to: pos + node.nodeSize, node: schema.text(titled ? titled.new_title : text, marks) }); + } else if (mark) { const ref = (mark.attrs.title as string | null) || text; - const next = renamedLink(ref, ref === text ? null : text, rename); + const next = renamedLink(ref, ref === text ? null : text, renames); if (!next) return; - const path = String(mark.attrs.path ?? '').replace(/\\/g, '/'); - const attrs = { ...mark.attrs, title: next.ref, - path: path === `${rename.old_ref}.md` ? `${rename.new_ref}.md` : mark.attrs.path }; + const path = String(mark.attrs.path ?? '').replace(/\\/g, '/').toLowerCase(); + const moved = renames.find((rename) => `${rename.old_ref}.md`.toLowerCase() === path); + const attrs = { ...mark.attrs, title: next.ref, path: moved ? `${moved.new_ref}.md` : mark.attrs.path }; const marks = node.marks.map((m: any) => m === mark ? wikiLink.create(attrs) : m); edits.push({ from: pos, to: pos + node.nodeSize, node: schema.text(next.display ?? next.ref, marks) }); } else if (!wikiLink && !node.marks.some((m: any) => m.type.name === 'code')) { // With wiki links switched off they are ordinary text in the note. - for (const change of renamedLinkChanges(text, rename)) { + for (const change of renamedLinkChanges(text, renames, noteDir)) { edits.push({ from: pos + change.from, to: pos + change.to, node: schema.text(change.insert, node.marks) }); } } @@ -7419,7 +7426,7 @@ editor.view.dispatch(tr); } - type LinksRewrittenEvent = LinkRename & { vault_path: string; path: string; old_raw: string; new_raw: string }; + type LinksRewrittenEvent = { vault_path: string; path: string; old_raw: string; new_raw: string; renames: LinkRename[] }; let unlistenLinksRewritten: (() => void) | null = null; listen('vault-links-rewritten', ({ payload }) => { if (editorDisposed || payload.vault_path !== $appConfig?.active_vault @@ -7436,7 +7443,10 @@ if (saves.raw !== payload.new_raw) flagExternalConflict(); return; } - applyRenamedLinks(payload); + const vault = payload.vault_path.replace(/\\/g, '/'); + const notePath = payload.path.replace(/\\/g, '/'); + const relative = notePath.startsWith(`${vault}/`) ? notePath.slice(vault.length + 1) : notePath; + applyRenamedLinks(payload.renames, relative.includes('/') ? relative.slice(0, relative.lastIndexOf('/')) : ''); })(); }).then(fn => { if (editorDisposed) fn(); diff --git a/src/lib/editor/renamedLinks.ts b/src/lib/editor/renamedLinks.ts index 60e2e7d..40099c6 100644 --- a/src/lib/editor/renamedLinks.ts +++ b/src/lib/editor/renamedLinks.ts @@ -1,17 +1,20 @@ /* - * Renaming a note rewrites the wiki links to it in every note that links there (see + * Renaming a note rewrites the links to it in every note that links there (see * `update_wikilinks_after_rename` in the backend). A note that is open with unsaved edits has to * follow in the editor, or its next save would write the old links back. These rules mirror the - * backend's. + * backend's; both are checked against `tests/fixtures/renamed-links.json`. */ export interface LinkRename { + /** Vault-relative path of the note without `.md`, before and after. */ old_ref: string; new_ref: string; old_title: string; new_title: string; - /** Whether links written with the note's title were rewritten, not only path links. */ + /** Links written with the title follow (the title changed and no other note has it). */ title_rules: boolean; + /** No other note has this file name, so a bare file name or top-level path means this note. */ + file_rules?: boolean; } export interface RenamedLink { @@ -19,16 +22,139 @@ export interface RenamedLink { display: string | null; } -/** The link `[[ref]]` or `[[ref|display]]` after the rename, or null when it is not affected. */ -export function renamedLink(ref: string, display: string | null, rename: LinkRename): RenamedLink | null { +const lower = (value: string) => value.toLowerCase(); + +function splitRef(ref: string): [string, string] { + const slash = ref.lastIndexOf('/'); + return slash < 0 ? ['', ref] : [ref.slice(0, slash), ref.slice(slash + 1)]; +} + +const joinRef = (dir: string, name: string) => (dir ? `${dir}/${name}` : name); + +/** `name` without a trailing `.md` (any capitals), and that extension as written. */ +function splitMdExt(name: string): [string, string] { + return /\.md$/i.test(name) ? [name.slice(0, -3), name.slice(-3)] : [name, '']; +} + +const byPath = (renames: readonly LinkRename[], ref: string) => + renames.find((rename) => lower(rename.old_ref) === lower(ref)); + +/** + * The wiki link `[[ref]]` or `[[ref|display]]` after the renames, or null when it is not affected. + * `ref` may carry a heading or block (`Note#Part`, `Note^id`) and a `.md` extension; both are kept. + */ +export function renamedLink(ref: string, display: string | null, renames: LinkRename | readonly LinkRename[]): RenamedLink | null { + const list = Array.isArray(renames) ? renames : [renames as LinkRename]; + const anchorAt = ref.search(/[#^]/); + const name = anchorAt < 0 ? ref : ref.slice(0, anchorAt); + const anchor = anchorAt < 0 ? '' : ref.slice(anchorAt); + const [base, ext] = splitMdExt(name); + let rename = list.find((candidate) => candidate.title_rules && lower(candidate.old_title) === lower(base)); + let next = rename?.new_title; + if (!rename) { + rename = byPath(list, base.replace(/\\/g, '/')); + if (!rename || rename.old_ref === rename.new_ref) return null; + const stemChanges = splitRef(rename.old_ref)[1] !== splitRef(rename.new_ref)[1]; + // At the top of the vault the path is the bare name, as unsure as a title link. + if (!rename.old_ref.includes('/') && !(rename.file_rules && stemChanges)) return null; + next = rename.new_ref; + } const followsTitle = display === rename.old_title && rename.old_title !== rename.new_title; - if (rename.title_rules && ref === rename.old_title) { - return { ref: rename.new_title, display: followsTitle ? rename.new_title : display }; + return { ref: next + ext + anchor, display: followsTitle ? rename.new_title : display }; +} + +/** Resolve `.` and `..` in a slash path; null when it climbs out of the vault. */ +function normalizeDir(path: string): string | null { + const parts: string[] = []; + for (const part of path.split('/')) { + if (!part || part === '.') continue; + if (part === '..') { + if (!parts.length) return null; + parts.pop(); + } else parts.push(part); } - if (rename.old_ref !== rename.new_ref && ref === rename.old_ref) { - return { ref: rename.new_ref, display: followsTitle ? rename.new_title : display }; + return parts.join('/'); +} + +/** The `../`-style prefix that leads from folder `from` to folder `to`, ending in `/` unless empty. */ +function relativeDir(from: string, to: string): string { + const a = from.split('/').filter(Boolean); + const b = to.split('/').filter(Boolean); + let common = 0; + while (common < a.length && common < b.length && a[common] === b[common]) common++; + return '../'.repeat(a.length - common) + b.slice(common).map((part) => `${part}/`).join(''); +} + +function decode(value: string): string { + try { + return decodeURIComponent(value); + } catch { + return value; } - return null; +} + +/** Characters a link destination without `<>` cannot hold as they are. */ +function encodeFileName(name: string): string { + return name.replace(/[% \t()<>#]/g, (char) => `%${char.charCodeAt(0).toString(16).toUpperCase().padStart(2, '0')}`); +} + +const encodePath = (path: string, angled: boolean) => (angled ? path : path.split('/').map(encodeFileName).join('/')); + +/** + * The destination of a Markdown link (`Note.md`, `../Folder/Note%20name.md#Part`, ``) + * after the renames, or null when it does not point at a renamed note. `noteDir` is the + * vault-relative folder of the note holding the link. The link keeps its form: relative stays + * relative, from the vault root stays so, and a bare name stays bare when it still finds the note. + */ +export function renamedHref(href: string, noteDir: string, renames: LinkRename | readonly LinkRename[]): string | null { + const list = Array.isArray(renames) ? renames : [renames as LinkRename]; + const angled = href.length >= 2 && href.startsWith('<') && href.endsWith('>'); + const dest = angled ? href.slice(1, -1) : href; + if (/^[a-z][a-z0-9+.-]*:/i.test(dest) || !dest || dest.startsWith('#')) return null; + const hashAt = dest.indexOf('#'); + const path = hashAt < 0 ? dest : dest.slice(0, hashAt); + const anchor = hashAt < 0 ? '' : dest.slice(hashAt); + const slash = path.lastIndexOf('/'); + const rawDir = slash < 0 ? '' : path.slice(0, slash + 1); + const rawFile = path.slice(slash + 1); + const [, ext] = splitMdExt(rawFile); + if (!ext) return null; + const [stem] = splitMdExt(decode(rawFile)); + const dir = decode(rawDir).replace(/\\/g, '/'); + let rename: LinkRename | undefined; + let form: 'bare' | 'relative' | 'root' = 'relative'; + if (!dir) { + rename = byPath(list, joinRef(noteDir, stem)); + if (!rename) { + // A bare name from another folder finds the note by its unique file name. + rename = list.find((candidate) => { + const [, oldStem] = splitRef(candidate.old_ref); + return candidate.file_rules && lower(oldStem) === lower(stem) && oldStem !== splitRef(candidate.new_ref)[1]; + }); + form = 'bare'; + } + } else { + const relative = dir.startsWith('/') ? null : normalizeDir(`${noteDir}/${dir}`); + rename = relative === null ? undefined : byPath(list, joinRef(relative, stem)); + if (!rename) { + const root = normalizeDir(dir); + rename = root === null ? undefined : byPath(list, joinRef(root, stem)); + form = 'root'; + } + } + if (!rename || rename.old_ref === rename.new_ref) return null; + const [oldDir] = splitRef(rename.old_ref); + const [newDir, newStem] = splitRef(rename.new_ref); + const file = encodePath(newStem + ext, angled); + const prefix = lower(oldDir) === lower(newDir) + ? rawDir + : form === 'bare' + ? '' + : form === 'relative' + ? encodePath(relativeDir(noteDir, newDir), angled) + : (dir.startsWith('/') ? '/' : '') + (newDir ? `${encodePath(newDir, angled)}/` : ''); + const next = prefix + file + anchor; + return angled ? `<${next}>` : next; } export interface TextChange { @@ -37,15 +163,71 @@ export interface TextChange { insert: string; } -/** The edits that bring the wiki links in Markdown text in line with the rename. */ -export function renamedLinkChanges(text: string, rename: LinkRename): TextChange[] { - const changes: TextChange[] = []; - // In a table cell the alias separator is written `\\|`, since a bare `|` ends the cell. - for (const match of text.matchAll(/\[\[([^\[\]|]+?)(?:(\\?\|)([^\[\]]*))?\]\]/g)) { - const next = renamedLink(match[1], match[3] ?? null, rename); - if (!next) continue; - const insert = next.display === null ? `[[${next.ref}]]` : `[[${next.ref}${match[2]}${next.display}]]`; - changes.push({ from: match.index, to: match.index + match[0].length, insert }); +/** Ranges of `text` outside fenced code blocks and inline code, where links are links. */ +function proseRanges(text: string): [number, number][] { + const ranges: [number, number][] = []; + let fence: { char: string; length: number } | null = null; + let offset = 0; + for (const line of text.split(/(?<=\n)/)) { + const start = offset; + offset += line.length; + const opening = /^ {0,3}(`{3,}|~{3,})(.*)$/.exec(line.replace(/\r?\n$/, '')); + if (fence) { + if (opening && opening[1][0] === fence.char && opening[1].length >= fence.length && !opening[2].trim()) fence = null; + continue; + } + if (opening && !(opening[1][0] === '`' && opening[2].includes('`'))) { + fence = { char: opening[1][0], length: opening[1].length }; + continue; + } + let from = start; + const ticks = /`+/g; + let match: RegExpExecArray | null; + while ((match = ticks.exec(line))) { + const closing = new RegExp(`(?\n]*>|(?:[^\s()<>]|\([^\s()<>]*\))+)([^)\n]*)\)/g; + +/** The edits that bring the wiki and Markdown links in Markdown text in line with the rename. */ +export function renamedLinkChanges(text: string, renames: LinkRename | readonly LinkRename[], noteDir = ''): TextChange[] { + const list = Array.isArray(renames) ? renames : [renames as LinkRename]; + const changes: TextChange[] = []; + for (const [from, to] of proseRanges(text)) { + const prose = text.slice(from, to); + for (const match of prose.matchAll(WIKI_LINK)) { + const inner = match[1]; + // In a table cell the alias separator is written `\|`, since a bare `|` ends the cell. + const bar = inner.indexOf('|'); + const escaped = bar > 0 && inner[bar - 1] === '\\'; + const ref = bar < 0 ? inner : inner.slice(0, escaped ? bar - 1 : bar); + const display = bar < 0 ? null : inner.slice(bar + 1); + const next = renamedLink(ref, display, list); + if (!next) continue; + const separator = escaped ? '\\|' : '|'; + const insert = next.display === null ? `[[${next.ref}]]` : `[[${next.ref}${separator}${next.display}]]`; + if (insert !== match[0]) changes.push({ from: from + match.index!, to: from + match.index! + match[0].length, insert }); + } + for (const match of prose.matchAll(MARKDOWN_LINK)) { + const href = renamedHref(match[2], noteDir, list); + if (href === null) continue; + // Link text that was the old title follows the new one. + const titled = list.find((rename) => rename.old_title === match[1] && rename.old_title !== rename.new_title); + const label = titled ? titled.new_title : match[1]; + const insert = `[${label}](${href}${match[3]})`; + if (insert !== match[0]) changes.push({ from: from + match.index!, to: from + match.index! + match[0].length, insert }); + } + } + return changes.sort((a, b) => a.from - b.from); } diff --git a/tests/fixtures/renamed-links.json b/tests/fixtures/renamed-links.json new file mode 100644 index 0000000..ff3ef46 --- /dev/null +++ b/tests/fixtures/renamed-links.json @@ -0,0 +1,230 @@ +[ + { + "name": "title links, aliases and links to other notes", + "rename": { + "old_ref": "Home", + "new_ref": "Start", + "old_title": "Home", + "new_title": "Start", + "title_rules": true, + "file_rules": true + }, + "note_dir": "", + "input": "See [[Home]], [[Home|the start page]] and [[Home|Home]].\nAlso [[Index|Home]] and [[Notes/Home|Home]].\n", + "expected": "See [[Start]], [[Start|the start page]] and [[Start|Start]].\nAlso [[Index|Home]] and [[Notes/Home|Home]].\n" + }, + { + "name": "headings, blocks, other capitals, the .md extension and embeds", + "rename": { + "old_ref": "Home", + "new_ref": "Start", + "old_title": "Home", + "new_title": "Start", + "title_rules": true, + "file_rules": true + }, + "note_dir": "", + "input": "[[Home#Section]] [[Home^abc123]] [[Home#Section|Home]] [[home]] [[HOME|x]] [[Home.md]] [[Home.md#Part]] ![[Home]] ![[Home#Part|300]] [[Homepage]] [[Home Office]]\n", + "expected": "[[Start#Section]] [[Start^abc123]] [[Start#Section|Start]] [[Start]] [[Start|x]] [[Start.md]] [[Start.md#Part]] ![[Start]] ![[Start#Part|300]] [[Homepage]] [[Home Office]]\n" + }, + { + "name": "aliased links in a table cell", + "rename": { + "old_ref": "Home", + "new_ref": "Start", + "old_title": "Home", + "new_title": "Start", + "title_rules": true, + "file_rules": true + }, + "note_dir": "", + "input": "| [[Home\\|Home]] | [[Home\\|start]] and [[Index\\|Home]] | [[Home#A\\|Home]] |\n", + "expected": "| [[Start\\|Start]] | [[Start\\|start]] and [[Index\\|Home]] | [[Start#A\\|Start]] |\n" + }, + { + "name": "links in code stay as written", + "rename": { + "old_ref": "Home", + "new_ref": "Start", + "old_title": "Home", + "new_title": "Start", + "title_rules": true, + "file_rules": true + }, + "note_dir": "", + "input": "Inline `[[Home]]` and ``a ` [[Home]]`` but [[Home]].\n```\n[[Home]] [x](Home.md)\n```\n~~~~md\n[[Home]]\n~~~\n~~~~\nAfter [[Home]] and `unclosed [[Home]]\n", + "expected": "Inline `[[Home]]` and ``a ` [[Home]]`` but [[Start]].\n```\n[[Home]] [x](Home.md)\n```\n~~~~md\n[[Home]]\n~~~\n~~~~\nAfter [[Start]] and `unclosed [[Start]]\n" + }, + { + "name": "Markdown links in the same folder", + "rename": { + "old_ref": "Home", + "new_ref": "Start", + "old_title": "Home", + "new_title": "Start", + "title_rules": true, + "file_rules": true + }, + "note_dir": "", + "input": "[Home](Home.md) [x](Home.md#Part \"title\") [x](./Home.md) [x]() [x](home.md) ![x](Home.md)\nUntouched: [x](https://example.com/Home.md) [x](Other.md) [x](Home) [x](#Home) [Home](Index.md)\n", + "expected": "[Start](Start.md) [x](Start.md#Part \"title\") [x](./Start.md) [x]() [x](Start.md) ![x](Start.md)\nUntouched: [x](https://example.com/Home.md) [x](Other.md) [x](Home) [x](#Home) [Home](Index.md)\n" + }, + { + "name": "a note in a folder, linked from that folder", + "rename": { + "old_ref": "Work/Old Plan", + "new_ref": "Work/New Plan", + "old_title": "Old Plan", + "new_title": "New Plan", + "title_rules": true, + "file_rules": true + }, + "note_dir": "Work", + "input": "[a](Old%20Plan.md) [a]() [[Old Plan]] [[Work/Old Plan#Goals|g]] [[Work\\Old Plan]]\n", + "expected": "[a](New%20Plan.md) [a]() [[New Plan]] [[Work/New Plan#Goals|g]] [[Work/New Plan]]\n" + }, + { + "name": "a note in a folder, linked from another folder", + "rename": { + "old_ref": "Work/Old Plan", + "new_ref": "Work/New Plan", + "old_title": "Old Plan", + "new_title": "New Plan", + "title_rules": true, + "file_rules": true + }, + "note_dir": "Other/Deep", + "input": "[a](../../Work/Old%20Plan.md#Goals) [a](Work/Old%20Plan.md) [a](/Work/Old%20Plan.md) [a](Old%20Plan.md) [a](Elsewhere/Old%20Plan.md) [a](../Old%20Plan.md)\n", + "expected": "[a](../../Work/New%20Plan.md#Goals) [a](Work/New%20Plan.md) [a](/Work/New%20Plan.md) [a](New%20Plan.md) [a](Elsewhere/Old%20Plan.md) [a](../Old%20Plan.md)\n" + }, + { + "name": "another note has the same name: only path links follow", + "rename": { + "old_ref": "Work/Old Plan", + "new_ref": "Work/New Plan", + "old_title": "Old Plan", + "new_title": "New Plan", + "title_rules": false, + "file_rules": false + }, + "note_dir": "Other", + "input": "[[Old Plan]] [[Old Plan|x]] [[Work/Old Plan|Old Plan]] [a](Old%20Plan.md) [a](../Work/Old%20Plan.md)\n", + "expected": "[[Old Plan]] [[Old Plan|x]] [[Work/New Plan|New Plan]] [a](Old%20Plan.md) [a](../Work/New%20Plan.md)\n" + }, + { + "name": "new names with characters a link destination has to encode", + "rename": { + "old_ref": "Plan", + "new_ref": "Plan (v2) #1", + "old_title": "Plan", + "new_title": "Plan (v2) #1", + "title_rules": true, + "file_rules": true + }, + "note_dir": "", + "input": "[a](Plan.md) [a]() [[Plan]]\n", + "expected": "[a](Plan%20%28v2%29%20%231.md) [a]() [[Plan (v2) #1]]\n" + }, + { + "name": "a note at the top of the vault whose name another note shares", + "rename": { + "old_ref": "Plan B", + "new_ref": "Plan C", + "old_title": "Plan B", + "new_title": "Plan C", + "title_rules": false, + "file_rules": false + }, + "note_dir": "Other", + "input": "[[Plan B]] [[Plan B|x]] [[Plan B#Part]] [a](Plan%20B.md) [b](../Plan%20B.md)\n", + "expected": "[[Plan B]] [[Plan B|x]] [[Plan B#Part]] [a](Plan%20B.md) [b](../Plan%20C.md)\n" + }, + { + "name": "a note moved to another folder, linked from its old folder", + "renames": [ + { + "old_ref": "Work/Plan", + "new_ref": "Archive/Plan", + "old_title": "Plan", + "new_title": "Plan", + "title_rules": false, + "file_rules": true + } + ], + "note_dir": "Work", + "input": "[p](Plan.md) [[Work/Plan]] [[Plan]] [q](./Plan.md#Goals)\n", + "expected": "[p](../Archive/Plan.md) [[Archive/Plan]] [[Plan]] [q](../Archive/Plan.md#Goals)\n" + }, + { + "name": "a note moved to another folder, linked from elsewhere", + "renames": [ + { + "old_ref": "Work/Plan", + "new_ref": "Archive/Plan", + "old_title": "Plan", + "new_title": "Plan", + "title_rules": false, + "file_rules": true + } + ], + "note_dir": "Other", + "input": "[[Work/Plan#Goals|g]] [a](../Work/Plan.md) [b](Plan.md) [r](/Work/Plan.md) [c](<../Work/Plan.md>) [d](Work/Plan.md)\n", + "expected": "[[Archive/Plan#Goals|g]] [a](../Archive/Plan.md) [b](Plan.md) [r](/Archive/Plan.md) [c](<../Archive/Plan.md>) [d](Archive/Plan.md)\n" + }, + { + "name": "a renamed notebook moves every note in it", + "renames": [ + { + "old_ref": "Work/A", + "new_ref": "Projects/A", + "old_title": "A", + "new_title": "A", + "title_rules": false, + "file_rules": true + }, + { + "old_ref": "Work/Sub/B", + "new_ref": "Projects/Sub/B", + "old_title": "B", + "new_title": "B", + "title_rules": false, + "file_rules": true + } + ], + "note_dir": "Other", + "input": "[[Work/A]] [[Work/Sub/B|b]] [x](../Work/Sub/B.md) [y](../Work/Other.md) [[A]]\n", + "expected": "[[Projects/A]] [[Projects/Sub/B|b]] [x](../Projects/Sub/B.md) [y](../Work/Other.md) [[A]]\n" + }, + { + "name": "a note moved from the top of the vault into a folder", + "renames": [ + { + "old_ref": "Plan", + "new_ref": "My Folder/Plan", + "old_title": "Plan", + "new_title": "Plan", + "title_rules": false, + "file_rules": true + } + ], + "note_dir": "", + "input": "[[Plan]] [a](Plan.md) [b]()\n", + "expected": "[[Plan]] [a](My%20Folder/Plan.md) [b]()\n" + }, + { + "name": "a note moved to the top of the vault", + "renames": [ + { + "old_ref": "Work/Plan", + "new_ref": "Plan", + "old_title": "Plan", + "new_title": "Plan", + "title_rules": false, + "file_rules": true + } + ], + "note_dir": "Other", + "input": "[[Work/Plan]] [a](../Work/Plan.md) [b](/Work/Plan.md)\n", + "expected": "[[Plan]] [a](../Plan.md) [b](/Plan.md)\n" + } +] diff --git a/tests/renamed-links.test.mjs b/tests/renamed-links.test.mjs index da07de2..8f65a40 100644 --- a/tests/renamed-links.test.mjs +++ b/tests/renamed-links.test.mjs @@ -49,3 +49,15 @@ test('aliased links in a table cell follow a rename', () => { '| [[Start\\|Start]] | [[Start\\|start]] and [[Index\\|Home]] |' ); }); + +// The same cases are checked against the backend rewrite in operations.rs. +const fixtures = JSON.parse(await readFile(new URL('./fixtures/renamed-links.json', import.meta.url), 'utf8')); +for (const fixture of fixtures) { + test(`shared rename case: ${fixture.name}`, () => { + let result = fixture.input; + for (const change of renamedLinkChanges(fixture.input, fixture.renames ?? fixture.rename, fixture.note_dir).reverse()) { + result = result.slice(0, change.from) + change.insert + result.slice(change.to); + } + assert.equal(result, fixture.expected); + }); +}