Menu

Bookmarklet: Quote to Markdown

I thought it would be handy to have a bookmarklet which would take a selected quote from a webpage and give a markdown blockquote with a link back to the original page. A bit of vibe coding with ChatGPT produced the following.

javascript:(function(){
    function escapeMarkdown(text) {
        return text.replace(/^/gm, '> ').replace(/`/g, '\\`');
    }
    const selection = window.getSelection().toString().trim();
    if (!selection) {
        alert('Please select some text first.');
        return;
    }
    const title = document.title;
    const url = location.href;
    const md = `${escapeMarkdown(selection)}\n\n— [${title}](${url})`;

    // Create temporary textarea and copy
    const textarea = document.createElement('textarea');
    textarea.value = md;
    document.body.appendChild(textarea);
    textarea.select();
    try {
        const successful = document.execCommand('copy');
        if (successful) {
            alert('Markdown quote copied to clipboard!');
        } else {
            throw new Error('Copy command failed');
        }
    } catch (err) {
        console.error('Copy failed', err);
        prompt('Copy the Markdown quote manually:', md);
    }
    document.body.removeChild(textarea);
})();

Running this through Bookmarklet Maker produced the following obfuscated minified one-liner:

javascript:(function()%7Bjavascript%3A(function()%7B%0A%20%20%20%20function%20escapeMarkdown(text)%20%7B%0A%20%20%20%20%20%20%20%20return%20text.replace(%2F%5E%2Fgm%2C%20'%3E%20').replace(%2F%60%2Fg%2C%20'%5C%5C%60')%3B%0A%20%20%20%20%7D%0A%20%20%20%20const%20selection%20%3D%20window.getSelection().toString().trim()%3B%0A%20%20%20%20if%20(!selection)%20%7B%0A%20%20%20%20%20%20%20%20alert('Please%20select%20some%20text%20first.')%3B%0A%20%20%20%20%20%20%20%20return%3B%0A%20%20%20%20%7D%0A%20%20%20%20const%20title%20%3D%20document.title%3B%0A%20%20%20%20const%20url%20%3D%20location.href%3B%0A%20%20%20%20const%20md%20%3D%20%60%24%7BescapeMarkdown(selection)%7D%5Cn%5Cn%E2%80%94%20%5B%24%7Btitle%7D%5D(%24%7Burl%7D)%60%3B%0A%0A%20%20%20%20%2F%2F%20Create%20temporary%20textarea%20and%20copy%0A%20%20%20%20const%20textarea%20%3D%20document.createElement('textarea')%3B%0A%20%20%20%20textarea.value%20%3D%20md%3B%0A%20%20%20%20document.body.appendChild(textarea)%3B%0A%20%20%20%20textarea.select()%3B%0A%20%20%20%20try%20%7B%0A%20%20%20%20%20%20%20%20const%20successful%20%3D%20document.execCommand('copy')%3B%0A%20%20%20%20%20%20%20%20if%20(successful)%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20alert('Markdown%20quote%20copied%20to%20clipboard!')%3B%0A%20%20%20%20%20%20%20%20%7D%20else%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20throw%20new%20Error('Copy%20command%20failed')%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%7D%20catch%20(err)%20%7B%0A%20%20%20%20%20%20%20%20console.error('Copy%20failed'%2C%20err)%3B%0A%20%20%20%20%20%20%20%20prompt('Copy%20the%20Markdown%20quote%20manually%3A'%2C%20md)%3B%0A%20%20%20%20%7D%0A%20%20%20%20document.body.removeChild(textarea)%3B%0A%7D)()%3B%7D)()%3B

And here is the bookmarklet as a link: quote-to-md. To “install” it, just drag that link in to your bookmarks tool bar.


Published: May 14, 2025 @ 16:01.
Last Modified: May 26, 2025 @ 17:15.

Tags:

#meta

Navigation Menu:

Home / Now / Blog / Notes / Reading / Office Camera / Tags / Bookmarks / RSS Feeds / Top of Page

Thanks for reading! If you have any comments or questions about the content, please let me know. Anyone can contact me by email.