Paste Link
approvedby Jose Elias Alvarez
Intelligently paste Markdown links.
Paste Link
Intelligently paste Markdown links.
Usage
Automatic
When you paste your clipboard's content into Obsidian, the plugin will check to see if it's a URL. If so, it'll insert the content as a Markdown link, using any selected text as a title. Otherwise, it'll just paste the text as normal.
Manual
If you prefer to use a separate command, you can disable the Override paste handler setting, restart Obsidian, and then use the Paste Markdown link command, which can be bound to any key combination of your choice in Obsidian's settings.
This command inserts a link if the clipboard contains a URL and behaves like the standard Insert Markdown link command otherwise, so if you like, you can replace the default Insert Markdown link keybinding with the command provided by this plugin.
Paste as plain text
If your clipboard contains a URL but you don't want to paste it as a Markdown link, use the Paste URL as plain text command, which is bound by default to Shift + Command + V on macOS / iOS and to Shift + Control + V everywhere else.
Notes
The plugin was originally inspired by obsidian-url-into-selection and seeks to improve on it in the following ways:
- An option to disable overriding Obsidian's paste handler
- Separate commands to control pasting behavior
- Cleaner code, which also resulted in fixing bugs / edge cases
The ability to fetch page titles was inspired by obsidian-auto-link-title and incorporates the following improvements:
- Using
fetch()vs. an Electron window to get HTTP page titles, which is faster and more predictable - Page-specific handlers for sites that don't normally expose titles (e.g. Reddit)
- An option to clean page titles using page-specific regular expressions
100% feature parity with these plugins is not a goal, but if you have a feature request or find a bug, please open an issue.
URLs
The plugin uses JavaScript's URL constructor to validate URLs. This means the following strings are considered URLs:
https://example.com
http://example.com
file:///path/to/file
But the following are not:
wwww.example.com
example.com
Additionally, URLs containing newlines are not handled, since Obsidian doesn't support multiline links.
Fetch page titles
If the Fetch page titles on paste setting is enabled, the plugin will attempt to fetch page titles from HTTP URLs and use them as link titles when Override paste handler is enabled. Alternatively, you can also fetch page titles on demand using the Paste link and fetch page title command.
Note that some pages (e.g. SPAs) may not include the full title in their HTML, and behavior may be inconsistent across platforms. Exceptions are handled on a best-case basis, and contributions to add new handlers are very welcome.
Page title regexes
You can optionally clean page titles before pasting using regular expressions. Each row in the the plugin's settings has three components, from left to right:
- Page regex: matches against the page's full URL to determine if this rule applies
- Title regex: extracts the desired part(s) from either the page title or URL
- Template (optional): formats the extracted parts using $1, $2, etc. for capture groups
This is how the plugin cleans page titles:
- The plugin checks each rule from top to bottom, stopping at the first match
- If the Page regex matches the current page URL, it applies the Title regex to the page's title (or to the URL itself if it's a non-HTTP URL)
- The Title regex extracts parts of the title (or URL) using capture groups
- If a Template is provided, it's filled in with captures from Title regex; otherwise, the first capture group ($1) is used (and any extra whitespace is trimmed)
This is not as scary as seems! Let's look at some examples:
-
GitHub issues: to extract just the issue title from
Add dark mode support · Issue #123 · raycast/extensions:- Page regex:
^https?://(?:www\.)?github\.com/.+?/issue - Title regex:
^(.+?)\s*· - Result:
Add dark mode support
- Page regex:
-
YouTube videos: to remove the "- YouTube" suffix from
Rust Tutorial Full Course - YouTube:- Page regex:
^https?://(?:www\.)?youtube\.com - Title regex:
(.+?)(?:\s*-[^-]*$|$) - Result:
Rust Tutorial Full Course
- Page regex:
-
General separators: to extract content before common separators such as
-,|,•:- Page regex:
.+?(matches any URL) - Title regex:
^(.+?)\s*[|–—•·-] - Result:
How to use promises(fromHow to use promises - Learn web development | MDN)
- Page regex:
-
Stack Overflow template: to reformat titles like
javascript - How to format dates? - Stack Overflowto emphasize the technology:- Page regex:
^https?://(?:www\.)?stackoverflow\.com/questions - Title regex:
([^-]+) - (.+?) - Stack Overflow - Template:
[$1] $2 - Result:
[javascript] How to format dates?
- Page regex:
-
VS Code URLs: to create titles from VS Code file URLs:
- Page regex:
^vscode://file/ - Title regex:
^vscode://file/.*/(.+?):(\d+)(since this is a non-HTTP URL, this matches against the URL itself) - Template:
$1 (line $2) - Result:
script.js (line 42)(fromvscode://file/Users/username/project/script.js:42)
- Page regex:
Additional notes:
- To debug regexes, use the
Check URLfield in the plugin's settings to see if the provided URL produces the expected title. - If you're not good with regular expressions, copy-paste the documentation above and ask ChatGPT. (Naming things is hard.)
- Since regexes are matched in order from top to bottom, you can set specific rules followed by general fallbacks.
- To temporarily bypass any configured regexes, use the
Paste link and fetch full page titlecommand. - Regexes are parsed using the Javascript RegExp() constructor. Use regex101 for validation.
Edge cases
The paste handler tries to handle these edge cases intelligently:
- If your cursor / selection is inside of an existing link, it'll paste normally, to allow you to edit the existing link more easily.
- If you've selected an existing link, it'll replace the link's URL with the clipboard's content, keeping its title.
These edge cases are ignored when using the Paste Markdown link command to better replicate the behavior of the default Insert Markdown link command.
I've found these behaviors useful in my own testing, but if you run into problems with your workflow, please open an issue.
For plugin developers
Search results and similarity scores are powered by semantic analysis of your plugin's README. If your plugin isn't appearing for searches you'd expect, try updating your README to clearly describe your plugin's purpose, features, and use cases.