This sounds like a good plugin idea.
I like this idea too. +1
He’s suggesting this as a possible fix, not saying it works now.
I’ve run into this issue too, I agree that the original text should be kept.
There are situations where you actually want the link text to be rewritten: e.g. when the link is just listed in a “See Also” section. In these cases it’s because the link text is supposed to be the title of the note. The underlying semantics of wikilinks should not change.
If you want stable link text, then the link should have custom text specified:
[[write naturally|write naturally]]
Of course this is a little cumbersome, so perhaps making the input a bit easier would be the best solution:
- Select
write naturally
- Type
[[|
- Autocompletes to
[[write naturally|write naturally]]
It might also be helpful to allow a wikilink with no target and only custom text:
[[|write naturally]]
Such a link should be clickable to create a new note and get replaced with the full link:
[[write naturally|write naturally]]
Another option is to have a setting that defaults to creating wikilinks with identical custom text. Personally I find link text changing to be the more surprising option, so I’d rather make the default link format one that is stable and make unstable link text more of an explicit choice.
It may be that there are two kinds of people with two different default styles of linking notes together.
It would be great as option:
New command Rename and leave alias
.
I’m currently considering renaming the file to UID{{date:YYYYMMDDHHmmss}}, and I wish this feature existed.
create [[any words]] wikilinks on multiple notes and then create [[any words]] notes and rename the file to just a UID, many notes lose their context.
I want an option to use original text as display text when updating internal links.
Workaround… for a workaround discovered:
If we have the link:
[[Apples|Apples]]
And the file Apples.md
gets renamed to Oranges.md
The link will become:
[[Oranges|Oranges]]
But if the file Apples.md
contains the alias Apples
in YAML:
---
aliases: Apples
---
then when Apples.md
is renamed to Oranges.md
the original link [[Apples|Apples]]
will keep its display text, turning into:
[[Oranges|Apples]]
The plugin Link with alias adds a “Create link with alias” command that, when run for selection Hello
in:
Hello, world!
-
Will output:
[[Hello|Hello]], world!
or (depending on plugin setting)
[[|Hello]], world!
with cursor placed before
|
in both cases. -
Once the cursor leaves the line with the link, will put the alias
Hello
into the target note’s YAML:--- aliases: Hello ---
This effectively utilizes the workaround shared above.
After mulling this over, I’m gonna go with the following workaround: to prevent any possible occurrence of the link display text auto-rename introduced in ver. 1.2.1, I will prepend all link display text with a distinguishing character like so:
[[Link target|~ Link display text]]
I also set up regex search-and-replace rule in Linter to automate this process.
The regex is:
(\!?\[\[.*?\|)(?!(?:insert|excluded|aliases|here)\]\])\s*~?\s*(.*?\]\])
With the substitution: $1~ $2
As a silver lining, there’s now a clear distinction in my notes between links with display text and links without it.
Also, for the Web Clipper author property to output e.g. [[Author 1|~ Author 1]], [[Author 2|~ Author 2]]
, the filter is:
{{author|split:", "|wikilink|join|replace:"/\[{2}([^\]]+)\]{2}/g":"[[$1|~ $1]]"}}
Alternatively, for the problem outlined in the OP, one could, whenever creating an unresolved link, duplicate its target into link display text but add a distinguishing character to it like so:
[[unresolved|~ unresolved]]
Here, Claude wrote me a Templater script to perform this action in one go:
Templater script
<%*
/**
* Template Name: Add Unresolved Link
* Description: Creates wikilink with ~prefix alias from selection or prompts for input. If selection is already a wikilink, adds ~prefix to alias.
* Version: 1.0
* Author: Created via Claude
* Source: https://forum.obsidian.md/t/add-option-to-always-preserve-original-link-text-as-display-text-alias-when-the-target-note-is-renamed-dont-change-original-text-when-updating-links/6521/32
* Last Updated: 2024-01-09
*/
const selection = tp.file.selection();
const editor = app.workspace.activeLeaf.view.editor;
const selections = editor.listSelections();
if (selections.length > 1) {
new Notice("Multiple selections or cursors aren't supported");
tR = editor.getSelection();
return;
}
let linkText;
if (selection) {
// Check if selection is already a wikilink
const wikiLinkMatch = selection.match(/\[\[(.*?)\]\]/);
if (wikiLinkMatch) {
linkText = wikiLinkMatch[1];
} else {
linkText = selection;
}
} else {
linkText = await tp.system.prompt("Add unresolved link");
if (!linkText) return;
}
tR = `[[${linkText}|~${linkText}]]`;
tp.file.cursor_append("");
-%>
It’s a dirty workaround but also a feature:
-
If the link ever becomes resolved,
~
will still signify that it was unresolved at the time of link creation. -
The following regex search query will show all such links in vault:
/\[\[.*?\|~[^\]]+?\]\]/
For easy access, the query can be saved to Bookmarks.