benjypng / chrome-extension-logseq-quickcapture

chrome-extension-logseq-quickcapture

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Preserve links and formatting

zolrath opened this issue · comments

Hello, great extension!

Feels really nice to be able to easily send text and maintain context quickly from your browser.
I'd be interested in also preserving more of the selected content, links, formatting etc.

We can extract the html tags for the given selection by changing the method of retrieving text from using window.getSelection().toString() to using:

      let html = window.getSelection().getRangeAt(0).extractContents();

If we make sure all links are absolute:

      // Convert paths like /wiki/ to https://en.wikipedia.org/wiki/ and #anchor-location to https://en.wikipedia.org/wiki/SomePage#anchor-location
      html.querySelectorAll("a").forEach((a) => {
        a.href = new URL(a, document.baseURI).href;
      });

We can run the html through an HTML to Markdown formatter like turndown and preserve links:
image

I made a quick version of it locally, but just copy pasted all of turndown.js into the chrome.scripting.executeScript call as a test.

Please pull the latest version. I've implemented the preservation of links.

However, for paragraphs, because we are using a URL callback, it is not possible (I think) to preserve actual paragraphs, hence I've actually replaced paragraphs with spaces so that it looks neater.

Looking at other notetaking apps using x-callback-url it seems as if maybe it's an issue that could be corrected on the logseq side, if newlines aren't preserved when sending them over.

- Fixed an issue where creating new notes from x-callback-url didn't show newlines.

Maybe a good issue to raise over there, would be great to also have newlines and preserve things like code blocks etc.