* Rewrite every `[[wikilink]]` across the vault that pointed to a note's old * name so it points to the new one. `notesBefore` is the pre-rename snapshot * (the renamed note still under `oldPath`), used so links resolve to what they * currently target. Only notes that actually link to it are read
( root: string, notesBefore: NoteMeta[], oldPath: string, newTitle: string )
| 3160 | * currently target. Only notes that actually link to it are read and rewritten. |
| 3161 | */ |
| 3162 | async function updateInboundWikilinks( |
| 3163 | root: string, |
| 3164 | notesBefore: NoteMeta[], |
| 3165 | oldPath: string, |
| 3166 | newTitle: string |
| 3167 | ): Promise<void> { |
| 3168 | const refs: RenameNoteRef[] = notesBefore.map((n) => ({ |
| 3169 | path: n.path, |
| 3170 | title: n.title, |
| 3171 | folder: n.folder |
| 3172 | })) |
| 3173 | const candidates = notesBefore.filter( |
| 3174 | (n) => |
| 3175 | n.path !== oldPath && |
| 3176 | n.folder !== 'trash' && |
| 3177 | (n.wikilinks ?? []).some((t) => resolveWikilinkTarget(refs, t)?.path === oldPath) |
| 3178 | ) |
| 3179 | for (const candidate of candidates) { |
| 3180 | try { |
| 3181 | const content = await readNote(root, candidate.path) |
| 3182 | const { body, changed } = rewriteWikilinksForRename(content.body, refs, oldPath, newTitle) |
| 3183 | if (changed > 0) await writeNote(root, candidate.path, body) |
| 3184 | } catch (err) { |
| 3185 | console.error('updateInboundWikilinks: failed for', candidate.path, err) |
| 3186 | } |
| 3187 | } |
| 3188 | } |
| 3189 | |
| 3190 | /** |
| 3191 | * The note's directory relative to its top-level folder root ('' when |
no test coverage detected