dvargas92495 / SmartBlocks

Useful examples from developer community for Roam42 SmartBlocks
147 stars 7 forks source link

Book Summary #102

Open zsviczian opened 3 years ago

zsviczian commented 3 years ago

UPDATED on 29 January. Additional features + errors corrected

✂️ Copy of your #42SmartBlock from Roam

JSON file: ProgressiveSummarization.zip

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.

GitMurf commented 3 years ago

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

kerim commented 3 years ago

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!

johngage commented 3 years ago

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]]

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

And thank you for your amazing work.

ChloeZhou1997 commented 3 years ago

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

eurunuela commented 3 years ago

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 commented 3 years ago

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.

kerim commented 3 years ago

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 ...

zsviczian commented 3 years ago

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

kerim commented 3 years ago

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 commented 3 years ago

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
zsviczian commented 3 years ago

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

On Fri, Jan 29, 2021, 05:21 P. Kerim Friedman notifications@github.com 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 commented 3 years ago

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

kerim commented 3 years ago

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

sc0ttwad3 commented 3 years ago

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)]
zsviczian commented 3 years ago

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

On Tue, 2 Feb 2021 at 20:00, Scott Wade notifications@github.com 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 .

sc0ttwad3 commented 3 years ago

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