clipboard-text-parser
v1.0.2
Published
A lightweight, zero-dependency TypeScript library for parsing, cleaning, and extracting metadata from raw clipboard payloads.
Maintainers
Readme
clipboard-text-parser
A lightweight, zero-dependency TypeScript library for parsing, cleaning, and extracting metadata from raw clipboard payloads.
🚀 See this library in action! Try Textunnel - Cross-Device Clipboard Sync.
Features
- URL & Email Extraction: Automatically detect and extract arrays of URLs and emails from copied text.
- HTML Stripping: Fast, RegEx/DOM-based utility to strip formatting from
text/htmlclipboard payloads (a very common developer pain point). - Code Snippet Detection: Uses heuristics to detect if the clipboard data is likely a programming language snippet.
- Type Detection: Identifies if the payload is purely a single URL, email, code snippet, or general text.
Installation
npm install clipboard-text-parserQuick Start
import { parseClipboardText, stripHtmlFromClipboard } from 'clipboard-text-parser';
// 1. Parsing raw text
const rawText = "Check out my new repo at https://github.com and email me at [email protected]";
const result = parseClipboardText(rawText);
console.log(result.urls); // ['https://github.com']
console.log(result.emails); // ['[email protected]']
// 2. Stripping HTML from rich-text clipboard
const richText = "<p>Hello <b>World</b>!</p><br/>Textunnel is great.";
const cleanText = stripHtmlFromClipboard(richText);
// Output: "Hello World!\nTextunnel is great."Why we built this
Handling raw clipboard data from navigator.clipboard.readText() or onPaste events is notoriously messy. We built this package to power the real-time parsing engine behind Textunnel - P2P File Sharing. It provides reliable, cross-device parsing logic directly in the browser to ensure that complex clipboard data (like rich HTML or mixed code snippets) is accurately processed before peer-to-peer transmission.
API Reference
parseClipboardText(text: string): ClipboardParsedResult
Returns { raw, urls, emails, isCodeSnippet }
stripHtmlFromClipboard(html: string): string
Returns clean, unformatted plain text while preserving paragraph breaks.
detectClipboardType(data: string): 'empty' | 'url' | 'email' | 'code' | 'text'
Returns the dominant type of the clipboard string.
Built with ❤️ by the team at Textunnel - Secure Paste Sharing.
