Paste URL into selection

repository·master·Indexed 19 days ago

https://github.com/denolehov/obsidian-url-into-selection

An Obsidian plugin (v1.11.4) that enables Notion-style link pasting. It allows users to wrap selected text in a URL using standard paste commands or convert URLs into text via the command palette. Features include customizable URL matching via regex, configurable behavior for when no text is selected, and a whitelist for automatic image embed syntax. Requires Obsidian v0.9.8 or higher.

Tokens
1.7K
Snippets
5
Records
10
Agent score
66%

What's inside obsidian-url-into-selection

  1. Use Paste URL into selection for Notion-style linking

    master

    The plugin provides two primary workflows for handling URLs and text:

    1. Link text with a URL: Select a piece of text and use the standard Ctrl/Cmd + V (Paste) command. The plugin will wrap the selected text in a link using the URL currently in your clipboard, similar to how Notion handles pasting links.

    2. Insert text into a URL: Select an existing link (URL) and trigger the plugin's command via the Obsidian Command Palette or a manually assigned hotkey. This will replace the URL with the text currently in your clipboard.

  2. Configure the NothingSelected behavior

    master

    The nothingSelected setting determines how the plugin behaves when you paste a URL and no text is currently selected in Obsidian. You can choose from the following modes via the NothingSelected enum:

    • doNothing: Uses the default Obsidian paste behavior.
    • autoSelect: Automatically selects the word surrounding the cursor before pasting.
    • insertInline: Formats the paste as a Markdown inline link: [](url).
    • insertBare: Inserts the URL as plain text: <url>.
    // Possible values for the nothingSelected setting:
    // NothingSelected.doNothing
    // NothingSelected.autoSelect
    // NothingSelected.insertInline
    // NothingSelected.insertBare
  3. Configure Obsidian plugin settings

    master

    The plugin provides a settings interface via UrlIntoSelectionSettingsTab which allows you to customize how URLs are matched and handled.

    Available Settings:

    1. Fallback Regular expression: A regex used to match URLs when the default matching logic fails.

      • Default: /^[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_\+.~#?&//=]*)$/
    2. Behavior on pasting URL when nothing is selected: Determines the action taken if you paste a URL while no text is currently selected in Obsidian.

      • Options:
        • 0 (Do nothing): No action is taken.
        • 1 (Auto Select): Automatically selects the word surrounding the cursor.
        • 2 (Insert [](url)): Wraps the URL in Markdown link syntax.
        • 3 (Insert <url>): Wraps the URL in angle brackets.
    3. Whitelist for image embed syntax: A list of regex-based rules (one per line) used to determine if a URL should be automatically converted into an image embed format: ![selection](url).

      • Example:
        youtu.?be|vimeo
  4. Determine if text is a valid URL or Wikilink

    master

    The isUrl function determines if a given string should be treated as a URL or a link. It returns true if the text matches any of the following criteria:

    1. It is an Obsidian wikilink format (e.g., [[Link]]).
    2. It is a valid URL with a recognized scheme (e.g., http, https, obsidian, mailto, etc.).
    3. It is a valid file path (Windows or Unix style).
    4. It matches the fallback regular expression provided in the plugin settings (settings.regex).

    Note: To avoid false positives, the plugin only accepts a specific set of VALID_URL_SCHEMES.

    // Example usage (conceptual):
    // isUrl(text, settings) returns true for:
    // - '[[My Note]]'
    // - 'https://example.com'
    // - 'C:\Users\Name\file.txt'
    // - 'file:///path/to/file'
  5. Register the paste-url-into-selection command

    master

    The plugin registers a command that allows users to manually trigger the URL-into-selection logic. When this command is invoked, it reads the current text from the system clipboard and applies it to the current editor selection using the plugin's settings.

    Command Details:

    • ID: paste-url-into-selection
    • Behavior: Reads clipboard text via navigator.clipboard.readText() and passes it to the UrlIntoSelection core function.
  6. Reference: PluginSettings schema

    master

    The PluginSettings object defines the configuration structure for the plugin. Use these keys when programmatically interacting with or extending the settings.

    export interface PluginSettings {
      regex: string; // Fallback regular expression for URL matching
      nothingSelected: NothingSelected; // Behavior when no text is selected (0, 1, 2, or 3)
      listForImgEmbed: string; // Regex-based whitelist for image embeds, split by line breaks
    }
  7. Configure PluginSettings

    master

    The PluginSettings interface defines the configuration schema for the plugin. Use these keys to customize URL detection and pasting behavior:

    • regex: A string representing the regular expression used to identify URLs.
    • nothingSelected: A value from the NothingSelected enum determining behavior when no text is selected.
    • listForImgEmbed: A string used to define the list for image embedding.
    export interface PluginSettings {
      regex: string;
      nothingSelected: NothingSelected;
      listForImgEmbed: string;
    }
  8. Use the UrlIntoSel_Plugin class

    master

    The UrlIntoSel_Plugin is the main entrypoint for the Obsidian plugin. It extends the Obsidian Plugin class and manages the lifecycle of the plugin, including loading settings, registering the settings tab, and setting up event listeners for pasting URLs into a selection.

    Key behaviors:

    • Automatic Paste Handling: It listens for the editor-paste event in the Obsidian workspace. When a paste event occurs, it intercepts the clipboard content and processes it using the UrlIntoSelection core logic.
    • Command Registration: It provides a command with the ID paste-url-into-selection which can be manually triggered to paste the current clipboard text into the editor selection.
    import { UrlIntoSel_Plugin } from 'obsidian-url-into-selection';
    
    // Note: As an Obsidian plugin, this is typically instantiated by the Obsidian app itself.
    // The plugin registers the following command:
    // ID: "paste-url-into-selection"