Bookmarklet: Send to Obsidian

Great stuff!

Here’s my version plucking up the parts that can be customized:

javascript: (function () {
  const vault = "zettelkasten";
  const prefix = "Clips/";
  const tag = "#saved_with_bookmarklet";

  const title = document.title;
  const file = title.replaceAll(/[^\w]/gi, "-").replaceAll(/[_-]+/g, "-");
  const selectedTextNotGoogleDocs = window.getSelection().toString().trim();
  const url = document.location.href;
  const content = `# ${title}${
    selectedTextNotGoogleDocs != ""
      ? `${"\n"} > ${selectedTextNotGoogleDocs.replaceAll("\n", "\n> ")}`
      : ""
  }${"\n\n"}- [${title}](${url})${"\n\n"}${tag}\n`;
  document.location.href = `obsidian://new?name=${prefix}${encodeURIComponent(
    file
  )}&content=${encodeURIComponent(content)}&vault=${vault}`;
})();

Note: quoting the selectedText doesn’t work in Google apps. See https://stackoverflow.com/a/11288003 for an explanation of why

4 Likes