diff --git a/src-tauri/src/commands.rs b/src-tauri/src/commands.rs index f7b7610..91f0cd5 100644 --- a/src-tauri/src/commands.rs +++ b/src-tauri/src/commands.rs @@ -1188,6 +1188,8 @@ fn extract_wiki_links(body: &str) -> Vec { if i + 1 < len { let link_raw = &body[start..i]; let link = link_raw.split('|').next().unwrap_or(link_raw); + // In a table cell the alias separator is written `\|`. + let link = link.strip_suffix('\\').unwrap_or(link); let link = link.split('#').next().unwrap_or(link); let link = link.split('^').next().unwrap_or(link); links.push(link.trim().to_lowercase()); @@ -1200,6 +1202,19 @@ fn extract_wiki_links(body: &str) -> Vec { links } +#[cfg(test)] +mod graph_link_tests { + use super::extract_wiki_links; + + #[test] + fn aliased_links_in_a_table_cell_are_found() { + let body = + "| a |\n| --- |\n| [[Home\\|start]] and [[Other]] and [[Deep/Note#part\\|x]] |\n"; + assert_eq!(extract_wiki_links(body), ["home", "other", "deep/note"]); + assert_eq!(extract_wiki_links("[[Home|start]]"), ["home"]); + } +} + fn scan_graph_data(vault_path: &str) -> Result { use rayon::prelude::*; use std::collections::{HashMap, HashSet}; diff --git a/src-tauri/src/vault/operations.rs b/src-tauri/src/vault/operations.rs index bbd75ff..e3c1c0f 100644 --- a/src-tauri/src/vault/operations.rs +++ b/src-tauri/src/vault/operations.rs @@ -1494,13 +1494,19 @@ fn update_wikilinks_after_rename( // 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. - result = result.replace( - &format!("[[{old_title}|{old_title}]]"), - &format!("[[{new_title}|{new_title}]]"), - ); + // 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), &format!("[[{}|", 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). @@ -1514,17 +1520,19 @@ fn update_wikilinks_after_rename( // 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]] - if old_title != 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, old_title), - &format!("[[{}|{}]]", new_rel_ref, new_title), + &format!("[[{old_rel_ref}{bar}"), + &format!("[[{new_rel_ref}{bar}"), ); } - // For aliases that DON'T match the old title, just update the ref part - result = result.replace( - &format!("[[{}|", old_rel_ref), - &format!("[[{}|", new_rel_ref), - ); } if result != content { @@ -2636,7 +2644,7 @@ mod rename_link_tests { let linking = vault.path().join("Linking.md"); fs::write( &linking, - "See [[Home]], [[Home|the start page]] and [[Home|Home]].\nAlso [[Index|Home]] and [[Notes/Home|Home]].\n", + "See [[Home]], [[Home|the start page]] and [[Home|Home]].\nAlso [[Index|Home]] and [[Notes/Home|Home]].\n\n| a | b |\n| --- | --- |\n| [[Home\\|Home]] | [[Home\\|start]] and [[Index\\|Home]] |\n", ) .unwrap(); let untouched = vault.path().join("Untouched.md"); @@ -2671,7 +2679,7 @@ mod rename_link_tests { fs::read_to_string(&linking).unwrap(), // The alias on the link to Index, and on the link to another note called Home in a // folder, is not this note's title and stays. - "See [[Start]], [[Start|the start page]] and [[Start|Start]].\nAlso [[Index|Home]] and [[Notes/Home|Home]].\n" + "See [[Start]], [[Start|the start page]] and [[Start|Start]].\nAlso [[Index|Home]] and [[Notes/Home|Home]].\n\n| a | b |\n| --- | --- |\n| [[Start\\|Start]] | [[Start\\|start]] and [[Index\\|Home]] |\n" ); assert_eq!( fs::metadata(&untouched).unwrap().modified().unwrap(), diff --git a/src/lib/editor/renamedLinks.ts b/src/lib/editor/renamedLinks.ts index 5fa03b9..60e2e7d 100644 --- a/src/lib/editor/renamedLinks.ts +++ b/src/lib/editor/renamedLinks.ts @@ -40,10 +40,11 @@ export interface TextChange { /** 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[] = []; - for (const match of text.matchAll(/\[\[([^\[\]|]+)(?:\|([^\[\]]*))?\]\]/g)) { - const next = renamedLink(match[1], match[2] ?? null, rename); + // 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}|${next.display}]]`; + 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 }); } return changes; diff --git a/tests/renamed-links.test.mjs b/tests/renamed-links.test.mjs index ea35a99..da07de2 100644 --- a/tests/renamed-links.test.mjs +++ b/tests/renamed-links.test.mjs @@ -41,3 +41,11 @@ test('nothing changes when no link points at the renamed note', () => { const rename = { old_ref: 'Home', new_ref: 'Start', old_title: 'Home', new_title: 'Start', title_rules: true }; assert.deepEqual(renamedLinkChanges('Home is [[Elsewhere]] and `code`.', rename), []); }); + +test('aliased links in a table cell follow a rename', () => { + const rename = { old_ref: 'Home', new_ref: 'Start', old_title: 'Home', new_title: 'Start', title_rules: true }; + assert.equal( + apply('| [[Home\\|Home]] | [[Home\\|start]] and [[Index\\|Home]] |', rename), + '| [[Start\\|Start]] | [[Start\\|start]] and [[Index\\|Home]] |' + ); +});