Make "insert markdown link" more functional

Use case or problem

The builtin command “insert markdown link” (Ctrl-K) allows to add the markdown syntax for adding a link to selected text, which is very useful.

For instance, if you select the word “text”, and then press Ctrl-K, you get

[text]()

and the cursor is placed inside the braces so that you add or paste a hyperlink.

Sometimes however, you have the opposite problem, you already have a hyperlink and want to add text to it. As far as I see, there is currently no command for this, and the existing Ctrl-K hotkey does not work very well for this case.

For instance, if you select the link https://obsidian.md and then press Ctrl-K, you get

[https://obsidian.md]()

This is usually not what you want. Instead, you probably want this

[](https://obsidian.md)

and the cursor be placed inside the brackets so that you can enter a link text like “Obsidian homepage”.

Proposed solution

The “insert markdown link” command should check if the selected text is a hyperlink. In this case, it should put the hyperlink in braces and add add empty brackets, instead of the normal behavior.

6 Likes

Workaround: GitHub - zolrath/obsidian-auto-link-title: Automatically fetch the titles of pasted links

1 Like

Yes, the auto-link-title plugin is useful here. However, often I want to add my own text and avoid making an Internet request.

1 Like

Exactly what I wanted to ask. Just detect if selection is a link or not. dude. :blush:

No need for a plugin to improve an existing command.
it just few lines of code to add. get selection, a regex to check if selection is a link, if so insert selection between ( ) instead of […].