Last active
July 26, 2021 14:11
-
-
Save artpi/99044b619e009aa403f56560a90e10b0 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Troamback by @artpi - see: | |
* https://piszek.com/roam/troamback | |
* For installation, put in your [[roam/js]]. Detailed instructions above. | |
* | |
* This script will put a reference in your daily page to a random block with a special tag. | |
* For example, I will get a reference on my daily page to a random block tagged with "Grateful". | |
*/ | |
// These are configurable: | |
// A block with one of this tags will be randomly selected and put in your daily page every day. | |
const throwbackReferences = [ 'Review', 'Grateful' ]; | |
// How should these throwbacks be marked? | |
const throwbackTag = 'Troamback'; | |
//---------------------------- | |
function prepareDailyPageWithThrowbacks() { | |
const todayUID = getTodayUid(); | |
const existingThrowback = window.roamAlphaAPI.q( | |
`[:find ?text :in $ ?uid ?re :where [?block :block/parents ?page] [?block :block/string ?text] [?page :block/uid ?uid] [?block :block/refs ?refer] [?refer :node/title ?re]]`, | |
todayUID, | |
throwbackTag | |
); | |
if ( existingThrowback.length > 0 ) { | |
// Throwback already done todayUID. quitting. | |
return; | |
} | |
insertRandomThrowbackToDaily( throwbackReferences, todayUID ); | |
//TODO 6 months ago, year ago and 3 years ago dailies | |
// Let's check every hour. | |
window.setTimeout( prepareDailyPageWithThrowbacks, 60 * 1000 ); | |
} | |
// Lets start this! | |
window.setTimeout( prepareDailyPageWithThrowbacks, 10 * 1000 ); | |
// Utility functions | |
function getReferencesToPageTitled( page ) { | |
return window.roamAlphaAPI | |
.q( | |
`[:find ?text ?uid ?srctitle ?time :in $ ?title :where [?page :node/title ?title] [?e :block/refs ?page] [?e :block/string ?text] [?e :block/uid ?uid] [?e :create/time ?time] [?src :node/title ?srctitle] [?e :block/page ?src] ]`, | |
page | |
) | |
.filter( ( b ) => b[ 0 ].indexOf( '[[query]]' ) === -1 ); | |
} | |
function insertRandomThrowbackToDaily( referencedPages, todayUID ) { | |
let references = []; | |
referencedPages.forEach( function ( page ) { | |
references = references.concat( getReferencesToPageTitled( page ) ); | |
} ); | |
if ( ! references ) { | |
return; | |
} | |
const ref = references[ Math.floor( Math.random() * references.length ) ]; | |
if ( | |
ref[ 0 ].indexOf( 'Tags::' ) !== -1 || | |
ref[ 0 ] === `[[${ ref[ 2 ] }]]` || | |
ref[ 0 ] === `#${ ref[ 2 ] }` || | |
ref[ 0 ] === `#[[${ ref[ 2 ] }]]` | |
) { | |
// The reference is just a tag without any content, so we want to link the page itself. | |
window.roamAlphaAPI.createBlock( { | |
location: { 'parent-uid': todayUID, order: 0 }, | |
block: { string: `#[[${throwbackTag}]] [[${ ref[ 2 ] }]]` }, | |
} ); | |
} else { | |
// Reference is a block, so we embed. | |
window.roamAlphaAPI.createBlock( { | |
location: { 'parent-uid': todayUID, order: 0 }, | |
block: { string: `#[[${throwbackTag}]] {{embed: ((${ ref[ 1 ] }))}}` }, | |
} ); | |
} | |
} | |
function zeroPad( data ) { | |
if ( data < 10 ) { | |
return '0' + data; | |
} | |
return '' + data; | |
} | |
function getTodayUid() { | |
const d = new Date(); | |
return ( | |
zeroPad( d.getMonth() + 1 ) + | |
'-' + | |
zeroPad( d.getDate() ) + | |
'-' + | |
d.getFullYear() | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment