diff --git a/README.md b/README.md index 8f54296..a87eee6 100644 --- a/README.md +++ b/README.md @@ -2,22 +2,63 @@ ![Plugin Demo](demo.gif) -This is a simple plugin for Obsidian that registers a custom `obsidian://copy` protocol. -It allows you to create internal links that, when clicked, will silently copy a specific text to your clipboard without opening any external applications or showing popups. +## What is this for? -## Usage +Have you ever wanted to create a note that, when you click a link, **automatically copies a snippet of text to your clipboard**? This plugin makes that possible. -Create a link using the following format: -`[Link text](obsidian://copy?text=Your%20text%20here)` +Some use cases: -When you click the link, "Your text here" will be copied to your clipboard, and a small notice will appear to confirm the action. +- A "cheat sheet" note with commands, passwords, or templates — click the link, paste anywhere. +- A collection of code snippets ready to paste into a terminal. +- Frequently used texts (email signatures, boilerplate, addresses) accessible from any note. +- Query strings to paste into other apps (e.g. to search specific emails in Outlook, my primary use case) -Note: Remember to URL-encode your text (e.g., use `%20` instead of spaces). +## How it works + +The plugin registers a custom `obsidian://copy` protocol. When you click a link using this protocol, the text embedded in the URL is silently copied to your clipboard and a small confirmation notice appears — no popups, no external apps. + +## Creating links manually + +```markdown +[Click to copy](obsidian://copy?text=Your-text-here) +``` + +When clicked, **"Your-text-here"** is copied to your clipboard. + +For text containing spaces, wrap the URL in angle brackets: + +```markdown +[Click to copy]() +``` + +Other special characters need to be encoded, and as you can see it can be tedious to do manually, so a command for that is included. + +## Creating links with a keyboard shortcut + +The plugin includes a command called **Copy Protocol: Paste clipboard as copy-protocol link** that automates the process. It reads your current clipboard content and inserts a ready-to-use markdown link at the cursor. If you have text selected in the editor, that selection becomes the link label; otherwise, the clipboard text itself is used as a label. + +### To assign a keyboard shortcut + +1. Open **Settings → Hotkeys**. +2. Search for "Paste clipboard as copy-protocol link". +3. Click the `+` button and press your preferred key combination. + +### Example workflow + +1. Copy a command or text you want to reuse (e.g. `git log --oneline`). +2. In your note, type a label like `Show git log` and select it. +3. Press your hotkey. +4. The selection is replaced with: + ``` + [Show git log]() + ``` +5. Clicking that link copies `git log --oneline` to your clipboard instantly. ## Installation -You can install this plugin via [BRAT](https://github.com/TfTHacker/obsidian42-brat): +You can install this plugin from the Community Plugins settings of Obsidian, or via [BRAT](https://github.com/TfTHacker/obsidian42-brat): + 1. Open the BRAT settings in Obsidian. -2. Click "Add Beta plugin". +2. Click **Add Beta plugin**. 3. Paste the repository URL: `jldiaz/copy-protocol-plugin`. 4. Enable the plugin in your Community Plugins list. diff --git a/package-lock.json b/package-lock.json index d0dac39..813ea32 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { - "name": "obsidian-sample-plugin", - "version": "1.0.0", + "name": "copy-text-protocol", + "version": "1.0.1", "lockfileVersion": 3, "requires": true, "packages": { "": { - "name": "obsidian-sample-plugin", - "version": "1.0.0", + "name": "copy-text-protocol", + "version": "1.0.1", "license": "0-BSD", "dependencies": { "obsidian": "latest" diff --git a/src/main.ts b/src/main.ts index 5f0a3d4..5090c12 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,13 +1,63 @@ -import { Plugin, Notice } from 'obsidian'; +import { Editor, MarkdownView, Plugin, Notice } from 'obsidian'; export default class CopyProtocolPlugin extends Plugin { async onload() { + // Register the obsidian://copy protocol handler this.registerObsidianProtocolHandler('copy', async (params) => { if (params.text) { - const query = decodeURIComponent(params.text); - await navigator.clipboard.writeText(query); + // Obsidian already decodes URL params before calling the handler, + // so no need for an extra decodeURIComponent here. + await navigator.clipboard.writeText(params.text); new Notice(`Copied to clipboard!`); } }); + + // Register the "paste as link" command + this.addCommand({ + id: 'paste-as-copy-link', + name: 'Paste clipboard as copy-protocol link', + editorCallback: async (editor: Editor, _view: MarkdownView) => { + // Read clipboard content + let clipboardText: string; + try { + clipboardText = await navigator.clipboard.readText(); + } catch { + new Notice('Could not read clipboard.'); + return; + } + + if (!clipboardText) { + new Notice('Clipboard is empty.'); + return; + } + + // Get current selection (used as the link label) + const selection = editor.getSelection(); + + // Build the obsidian://copy URL, URL-encoding the clipboard text + const encodedText = encodeURIComponent(clipboardText); + const url = `obsidian://copy?text=${encodedText}`; + + // Build the markdown link: + // [