ReedyBear's Blog

BearBlog: Auto-generate markdown links

1. Create a bookmark with the name 'Page to MD Link' and for the url, use this javascript code: (Explained below)

javascript: navigator.clipboard.writeText("["+document.querySelector("title").textContent.trim().replace(/<[^>]*>?/gm, '')+"]("+window.location.href+")");

2. Visit a page & click the bookmark

3. Paste into your blog post where you want the link to be.

Explanation

Expanded version with comments:

navigator.clipboard.writeText(// copy to your clipboard
   "[" +
    document.querySelector("title") // select the <title>Page Title</title>
        .textContent // Get just the "Page Title" part
        .trim()  // Remove any spaces around the title
        .replace(/<[^>]*>?/gm, '') // Remove HTML tags. See https://stackoverflow.com/a/822464
    + "]("
    + window.location.href  // the current page's URL
    + ")");

As for the regex /<[^>]*>?/gm:

/      # Starts a regex, is not part of the regex
<      # Find a less-than character
[^>]   # Find the next character if it is NOT a greater-than character
*      # Do the last step zero or more times
>      # Find the next character if it is a greater-than character
?      # The previous character (>) is optional & does not have to be present
/      # Ends a regex, is not part of the regex
gm     # configurations that tell the regex engine how to parse.
         # I believe g is "global" and m is "multiline" but idr how they work

#bearblog