SmartBlocks icon indicating copy to clipboard operation
SmartBlocks copied to clipboard

Book Summary

Open zsviczian opened this issue 4 years ago • 16 comments

UPDATED on 29 January. Additional features + errors corrected

✂️ Copy of your #42SmartBlock from Roam

JSON file: ProgressiveSummarization.zip

  • #42SmartBlock Summary v2
    • <%NOBLOCKOUTPUT%><%JA:```javascript var order = 0; var rootUID;

async function print(s,uid) { let reg = new RegExp('\^\^(.?)\^\^','g'); let ht = ''; while (match = reg.exec(s)) { ht += (ht=='') ? '' : highlightSeparator; highlight = match[1]; if (removeBold) highlight = highlight.replaceAll('**',''); ht += highlight; } if(ht != '') { ht += ' []((('+ uid +')))'; roam42.common.createBlock(rootUID, order, ht); order += order + 1; } }

async function sortObjectsByOrder(o) { return o.sort(function(a, b) { return a[0].order - b[0].order; }); }

async function printChildren(o) { await print(o.string,o.uid); if(o.children) { var b = await roam42.common.getBlockInfoByUID(o.uid,true); var c = await roam42.common.sortObjectsByOrder(b[0][0].children);
for(var i=0;i<c.length;i++) await printChildren(c[i]); } }

//read source page link from parent block var UID; UID = document.querySelector("textarea.rm-block-input").id; UID = UID.substring( UID.length -9); q = [:find ?s ?uid :where [?b :block/uid "${UID}"] [?p :block/children ?b] [?p :block/string ?s] [?p :block/uid ?uid]]; blockInfo = window.roamAlphaAPI.q(q); roam42.common.deleteBlock(UID); if(blockInfo.length != 1) { alert('Nest command under block containing the [[Page Link]] to your highlighted notes'); return null; } rootUID = blockInfo[0][1]; var pageTitle = blockInfo[0][0].match('\[{2}(.*)\]{2}'); if(pageTitle == null) { alert('Can't find [[Page Link]] in parent block'); return null; } pageTitle = pageTitle[1];

var highlightSeparator = prompt('Set text for separating highlights within the paragraph\nFor example " " or " ... "',' ... '); var removeBold = prompt('Do you want to remove bolding of text?\ni.e. Make TEXT into TEXT\n[Y]/[N]','Y'); removeBold = (removeBold=='Y' || removeBold=='y') ? true : false;

//query for source page var rule = '[[(ancestor ?b ?a)[?a :block/children ?b]]]'; var query = [:find (pull ?block [:block/string :block/uid :block/order :block/heading :block/children]) :in $ ?page_title % :where [?page :node/title ?page_title] (ancestor ?block ?page)]; var results = await window.roamAlphaAPI.q(query, pageTitle, rule); results = await sortObjectsByOrder(results); for (j=0;j<results.length;j++) { await printChildren(results[j][0]); }```%>

📋 Describe the SmartBlock

Takes a Page_title through the clipboard, reads the page and outputs only the highlighted text from blocks on the page including a [*](((block reference))) to the original block. Useful for making summaries from literature notes if you follow Tiago Forte's Progressive Summarization approach

✅ Describe any prerequisites or dependencies that are required for this SmartBlock

Only Roam42 is required.

📷 Screenshot of your #42SmartBlock workflow/template from Roam

image

💡 Additional Info

read my blogpost on the topic Check out https://youtu.be/zgyME1HUQLo to see it working.

zsviczian avatar Dec 10 '20 12:12 zsviczian

This is such good example code to build from! Awesome job @zsviczian !

GitMurf avatar Dec 10 '20 19:12 GitMurf

This is great. I might try to update it so you can just enter a page title on a block and then run the script, rather than using the clipboard, but otherwise it is perfect. Thanks!

kerim avatar Dec 12 '20 03:12 kerim

As @Murf pointed out, copy and paste from Slack and Roam often miss the three backquotes or backticks. So this listing misses

``` javascript

just after the <%JA:

and just before the javascript print statement.

If the leading ``` match the trailing ```, all is well.

It's quite clear on your blog post:

- #42SmartBlock Summary - [[[[pattern]]/Progressive Summarization]] - <%NOBLOCKOUTPUT%><%JA:```javascript

print = async (s,uid) => { and so forth...

And thank you for your amazing work.

johngage avatar Dec 14 '20 21:12 johngage

Hi, is the code only executable when the format of the literature notes stays the same as your template? I tried to follow the same naming structure of the notes but used highlights I imported from Readwise, the code is not working

ChloeZhou1997 avatar Dec 16 '20 21:12 ChloeZhou1997

I still haven't found a way to make it work either. Not sure if it's a copy/pasting issue (I think I fixed all those) or I'm incorrectly using the SB.

Edit: I had to cmd + shift + v otherwise the code got pasted incorrectly. Thank you so much for this amazing SB!

eurunuela avatar Dec 17 '20 10:12 eurunuela

In case someone has issues with this SB after the latest roam42 update. I also commented the original line that took the title from the clipboard, and added an automatic way of retrieving the page title.

  • Highlights #42SmartBlock
    
    • <%NOBLOCKOUTPUT%><%JA:```javascript
      print = async (s,uid) => {
      	var b = s.match(/\^\^.*?\^\^/g);
      	var ht = '';
      	if(b) {
      		for(var h of b) ht += h.replaceAll('^^','') + ' ';
              if(ht != '') {
                  ht += ' [*]((('+ uid +')))';
         			await roam42.smartBlocks.activeWorkflow.outputAdditionalBlock(ht, false);
        			// await roam42.smartBlocks.outputArrayWrite();
              }      
          } 
      }
      
      sortObjectsByOrder = async o => {
          return o.sort(function(a, b) {
            return a[0].order - b[0].order;
          });
      };  
      
      printChildren = async o => {
          await print(o.string,o.uid);
          if(o.children) {
        		var b = await roam42.common.getBlockInfoByUID(o.uid,true);
              var c = await roam42.common.sortObjectsByOrder(b[0][0].children);    
              for(var i=0;i<c.length;i++) 
                 	await printChildren(c[i]);
         	} 
      }
      
      // var page_title = await navigator.clipboard.readText();
      var page_title = document.title;
      var rule = '[[(ancestor ?b ?a)[?a :block/children ?b]]]';
      var query = `[:find  (pull ?block [:block/string :block/uid :block/order 
                                         :block/heading :block/children])
                    :in $ ?page_title %
                    :where
                    [?page :node/title ?page_title]
                    (ancestor ?block ?page)]`;
      var results = await window.roamAlphaAPI.q(query, page_title, rule);
      results = await sortObjectsByOrder(results);
      for (j=0;j<results.length;j++) {
         await printChildren(results[j][0]);
      }```%>
      

eurunuela avatar Dec 20 '20 11:12 eurunuela

Is this working for others? I had it working before, but can't get it to do anything now. I tried the version from the website, which has a convenient JSON import, but that one keeps giving me an error saying that I need to nest it under a link to the page (it is). The latest version here just does nothing ...

kerim avatar Jan 28 '21 03:01 kerim

Works for me... Can you send a screenshot or screen recording of what is happening? I just tried this: https://zsviczian.github.io/ProgressiveSummarization.zip

  • [[Page with highlighted notes]]
    • jjSummary

zsviczian avatar Jan 28 '21 12:01 zsviczian

I installed this version. No error message now, but nothing happens. Just a blinking cursor after the SmartBlock is invoked. Waited a long time, but ... nothing. Here are some pictures.

Screen Shot 2021-01-29 at 12 11 05 PM Screen Shot 2021-01-29 at 12 10 56 PM

kerim avatar Jan 29 '21 04:01 kerim

Oh, I discovered the way to get the error message though. It seems it isn't enough for the command to be entered on an indent under the page link, the link itself has to be a top-level link. If you are indented twice, it won't work. Screen Shot 2021-01-29 at 12 21 17 PM

kerim avatar Jan 29 '21 04:01 kerim

everyday a new learning. I'll fix that! thanks

On Fri, Jan 29, 2021, 05:21 P. Kerim Friedman [email protected] wrote:

Oh, I discovered the way to get the error message though. It seems it isn't enough for the command to be entered on an indent under the page link, the link itself has to be a top-level link. If you are indented twice, it won't work. [image: Screen Shot 2021-01-29 at 12 21 17 PM] https://user-images.githubusercontent.com/59976/106231363-7f5fcd00-622c-11eb-93fe-592b127056e3.png

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/roamhacker/SmartBlocks/issues/102#issuecomment-769565135, or unsubscribe https://github.com/notifications/unsubscribe-auth/ADNRO6RY3XMJOYPPWZ6FUD3S4IZVRANCNFSM4UU7JIXA .

zsviczian avatar Jan 29 '21 10:01 zsviczian

@kerim I fixed the issue, as well as changed output to new createBlock API. Runs lightning fast now. Updated JSON and code above.

zsviczian avatar Jan 29 '21 14:01 zsviczian

Oh wow. This works again for me! And even better than before. Thanks so much. This tool is so essential to my workflow!

kerim avatar Jan 29 '21 14:01 kerim

Awesome. Is there documentation anywhere for these querying capabilities? When I started with Roam, I thought we were stuck using this style of querying: {{query: {and: [[TODO]] {not: [[query]] }}}} before the alpha of the Roam API was available.

After, I've been struggling still for ways to query Roam, but didn't know what operators you could pass in with window.roamAlphaAPI.q(), or even that there was a :find, :in, :where, and so on. Where can I find out what's available to use?

[:find  (pull ?block [:block/string :block/uid :block/order 
                                   :block/heading :block/children])
              :in $ ?page_title %
              :where
              [?page :node/title ?page_title]
              (ancestor ?block ?page)]

sc0ttwad3 avatar Feb 02 '21 19:02 sc0ttwad3

https://www.zsolt.blog/2021/01/Roam-Data-Structure-Query.html

On Tue, 2 Feb 2021 at 20:00, Scott Wade [email protected] wrote:

Awesome. Is there documentation anywhere for these querying capabilities? When I started with Roam, I thought we were stuck using this style of querying: {{query: {and: [[TODO]] {not: [[query]] }}}} before the alpha of the Roam API was available.

After, I've been struggling still for ways to query Roam, but didn't know what operators you could pass in with window.roamAlphaAPI.q(), or even that there was a :find, :in, :where, and so on. Where can I find out what's available to use?

[:find (pull ?block [:block/string :block/uid :block/order :block/heading :block/children]) :in $ ?page_title % :where [?page :node/title ?page_title] (ancestor ?block ?page)]

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/roamhacker/SmartBlocks/issues/102#issuecomment-771893523, or unsubscribe https://github.com/notifications/unsubscribe-auth/ADNRO6TFUGRPNC4JY7KUZX3S5BDWPANCNFSM4UU7JIXA .

zsviczian avatar Feb 02 '21 20:02 zsviczian

@zsviczian Thanks! Your blog articles are extremely helpful. I look forward to each one.

sc0ttwad3 avatar Feb 08 '21 19:02 sc0ttwad3