Skip to content

Commit

Permalink
feat: auto extract tweets
Browse files Browse the repository at this point in the history
  • Loading branch information
8bitgentleman committed Jul 15, 2023
1 parent 50fe5c3 commit 3f9bfab
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 2 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@

To trigger the tweet extraction you have two options: Right click plugin or keyboard shortcut

To trigger via a keyboard shortcut click into the block and hit a `CTRL` + `SHIFT` + `E`. (will be remapable soon)
To trigger via a keyboard shortcut click into the block and hit a `CTRL` + `SHIFT` + `E`.

## Auto Extract
If set, tagged tweets and threads will be automatically extracted when roam loads. This is particularly useful when using a Quick Caption solution on mobile. When browsing twitter share a tweet to roam and tag the block before sending it. Next time you load up your graph the tweet or thread will be automatically extracted.

## Example
<img src="https://github.com/8bitgentleman/roam-depot-tweet-extract/raw/main/example.gif" max-width="400"></img>
Expand Down
50 changes: 49 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ function getimageLocation(extensionAPI) {
return extensionAPI.settings.get('image-location') || "child block"
}

function getAutoExtractTag(extensionAPI) {
return extensionAPI.settings.get('auto-extract-tag') || "tweet-extract"
}

const panelConfig = {
tabTitle: "Tweet Extract",
settings: [
Expand All @@ -27,7 +31,19 @@ const panelConfig = {
description: "If there are images attached to a tweet where should they be added",
action: {type: "select",
items: ["child block", "inline", "skip images"],
}}
}},
{id: "auto-extract",
name: "Auto Extract",
description: "When Roam loads if there are blocks tagged with the `Auto Tweet Extract Tag` or `Auto Thread Extract Tag` they will be automatically extracted. Useful with Quick Capture solutions.",
action: {type: "switch"}},
{id: "auto-extract-tag",
name: "Auto Tweet Extract Tag",
description: "",
action: {type: "input",
placeholder: "tweet-extract",
onChange: (evt) => {
template = evt.target.value;
}}}
]
};

Expand Down Expand Up @@ -329,6 +345,19 @@ async function extractTweet(uid, tweet, template, imageLocation){
removeSpinner(uid)
}

function getPageRefs(page){
let query = `[:find (pull ?refs [:block/string :node/title :block/uid])
:in $ ?namespace
:where
[?e :node/title ?namespace]
[?refs :block/refs ?e]
]`;

let result = window.roamAlphaAPI.q(query,page).flat();

return result;
}

// move onload to async function
async function onload({extensionAPI}) {
console.log("load tweet extract plugin")
Expand All @@ -352,6 +381,25 @@ async function onload({extensionAPI}) {
label: "Extract Tweet",
callback: (e) => extractTweet(e['block-uid'], e['block-string'], getTweetTemplate(extensionAPI), getimageLocation(extensionAPI))
})
// auto extract
if (extensionAPI.settings.get('auto-extract')) {
let tweets = await getPageRefs(getAutoExtractTag(extensionAPI));

// single tweets
for (const tweet of tweets) {
try {
await extractTweet(
tweet.uid,
tweet.string,
getTweetTemplate(extensionAPI),
getimageLocation(extensionAPI)
)
} catch (error) {
console.error(error, tweet)
}

}
}
}

function onunload() {
Expand Down

0 comments on commit 3f9bfab

Please sign in to comment.