Skip to content

Allow org-roam-protocol to capture the webpage's selection#1239

Merged
zaeph merged 7 commits intoorg-roam:masterfrom
zaeph:orp-link
Nov 8, 2020
Merged

Allow org-roam-protocol to capture the webpage's selection#1239
zaeph merged 7 commits intoorg-roam:masterfrom
zaeph:orp-link

Conversation

@zaeph
Copy link
Copy Markdown
Member

@zaeph zaeph commented Nov 7, 2020

I believe we supported this feature a while ago, but an iteration must have overridden it.

You can now use %i in org-roam-capture-ref-templates to insert the selected text on the web-page you are capturing. Note that it can either appear in :head (expanded once on file creation) or in the body of the template (expanded every time the ORP is called on a given webpage). The latter makes it a good option for incremental reading.

Here's a use-case:

(defun zp/org-protocol-insert-selection-dwim (selection)
  "Insert SELECTION as an org blockquote."
  (unless (string= selection "")
    (format "#+begin_quote\n%s\n#+end_quote" selection)))

(setq org-roam-capture-ref-templates
      '(("r" "ref" plain
         (function org-roam-capture--get-point)
         ""
         :file-name "web/${slug}"
         :head "#+title: ${title}
#+roam_key: ${ref}
#+created: %u
#+last_modified: %U

%(zp/org-protocol-insert-selection-dwim \"%i\")"
         :unnarrowed t)

        ("i" "incremental" plain
         (function org-roam-capture--get-point)
         "* %?\n%(zp/org-protocol-insert-selection-dwim \"%i\")"
         :file-name "web/${slug}"
         :head "#+title: ${title}
#+roam_key: ${ref}
#+created: %u
#+last_modified: %U\n\n"
         :unnarrowed t
         :empty-lines-before 1)))

You need to make sure that you have the proper bookmarklet (note the template=r to specify which template to use`):

javascript:location.href = 'org-protocol://roam-ref?template=r&ref='%20+%20encodeURIComponent(location.href)%20+%20'&title='%20+%20encodeURIComponent(document.title)%20+%20'&body='%20+%20encodeURIComponent(window.getSelection())

zaeph added 3 commits November 7, 2020 17:19
* org-roam-protocol.el (org-roam-protocol-open-ref): Replicate default
`org-protocol' behaviour temporarily for storing links
* org-roam-capture.el (org-roam-capture--capture): Prevent stored link from
being reset

When capturing a web-page with org-roam-protocol, a link is now temporarily
stored in `org-store-link-plist' via `org-link-store-props'.  This is to allow
the forwarding of properties to `org-capture', one of them being `:initial'
which contains the content of the selected text in the browser.
* org-roam-protocol.el (org-roam-protocol-store-links): Add new toggle
(org-roam-protocol-open-ref): Conditionally store link for later used

Building up on b2ee5f2, the user can now
decide whether to store links when capturing with org-roam-protocol (default
nil).
@zaeph
Copy link
Copy Markdown
Member Author

zaeph commented Nov 7, 2020

Using the incremental template to create a file almost works, but the lines in :empty-lines-before are inserted before :head. There might be an easy way to fix this, but I think we're pushing the limits of our double-expansion system here.

@jethrokuan
Copy link
Copy Markdown
Member

See also #1088

As seen in org-roam#1088.

Co-authored-by: Russell Sim <russell.sim@gmail.com>
- Use let-alist to reduce redundancy
- Use decoded-alist to avoid parsing INFO a 2nd time

Co-authored-by: Russell Sim <russell.sim@gmail.com>
@zaeph
Copy link
Copy Markdown
Member Author

zaeph commented Nov 8, 2020

Thanks for pointing it out, @jethrokuan, hadn't caught up on that PR yet.

Considering the wild variety of notes people are making, I think we shouldn't include %i in the default template:

  • People have a set expectation of what is going to happen when they use the bookmarklet, and changing the default would clash with that.
  • %i is a default org-capture feature, but modularising it inside a single template requires Elisp, which people aren't likely to have.
  • It creates expectations for incremental reading (i.e. appending notes to a file that has already been created), and this isn't ready yet.

I vote for merging this now, and addressing the incremental reading stuff in another PR.

I believe we’re always going to have a ref, so no need to check for nil.
Comment thread org-roam-protocol.el
Added by lispy, but this is probably an edge-case that it isn’t managing.
@zaeph zaeph merged commit 7602b8c into org-roam:master Nov 8, 2020
@zaeph zaeph deleted the orp-link branch November 8, 2020 14:34
@zaeph zaeph mentioned this pull request Nov 10, 2020
@github-actions github-actions Bot locked as resolved and limited conversation to collaborators Jun 7, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants