Skip to content

Commit 2714813

Browse files
committed
Initial upload of basic scripts and docs
1 parent 4ae7999 commit 2714813

File tree

7 files changed

+146
-1
lines changed

7 files changed

+146
-1
lines changed

README.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,6 @@
11
# roam-snippets
2-
A collection of code snippets to extend UX in Roam Research
2+
An index repository of personal code snippets I use in my Roam Research database
3+
4+
- **roam-copy-ref** : provides a copy-to-clipboard button to select blocks (made with the lightweight library [clipboard.js](https://clipboardjs.com)). It's the formatted (not raw) content that is copied : bold, italics, etc. are preserved. (I use it to copy formatted literature references, hence the name)
5+
- **roam-get-linked-refs-data** : adds a data-... attribute to the top block of each page listed in Linked References, containing the page's title. This is handy for applying a specific formatting to certain results (e.g, don't show the breadcrumbs for results from pages called XXX) or adding some database-related functionality.
6+
- **roam-get-paper-data** : adds a data-... attribute to particular page references, containing a piece of information related to that page. This is handy for displaying some page-specific information next to each reference instance, for example, or adding some database-related functionality. (I use it to add information about the full literature citation for every paper I reference, and display it on hover)

roam-copy-ref/README.md

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
##roam-copy-ref
2+
Provides a copy-to-clipboard button (through lightweight library [clipboard.js](https://clipboardjs.com)) attached to each block nested under the `Citations::` attribute.

roam-copy-ref/roam-copy-ref.js

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
2+
/* How to import the clipboard.js library from CDN */
3+
4+
var s = document.createElement("script");
5+
s.src = "https://unpkg.com/clipboard@2/dist/clipboard.min.js";
6+
s.id = "clipboard-js";
7+
s.type = "text/javascript";
8+
document.getElementsByTagName("head")[0].appendChild(s);
9+
10+
/* How to create a button template */
11+
var btn = document.createElement("button");
12+
btn.textContent = "Copy ref";
13+
btn.classList.add("ref-btn");
14+
15+
/* How to collect reference blocks on the page & add a clone of the button to each block */
16+
function addCopyButton(refblock) {
17+
if (refblock.previousSibling.nodeName != "BUTTON") {
18+
var parentDiv = refblock.parentNode;
19+
parentDiv.insertBefore(btn.cloneNode(true), refblock);
20+
}
21+
}
22+
function checkIfRef(button) {
23+
if(!(button.closest('.roam-block-container').dataset.pathPageLinks.includes("Citations"))){
24+
button.parentNode.removeChild(button);
25+
}
26+
}
27+
28+
setInterval(function(){
29+
document.querySelectorAll('.rm-block[data-page-links*="Citations"] > .rm-block-children > .rm-block > .rm-block-main > .roam-block').forEach(addCopyButton);
30+
document.querySelectorAll('button.ref-btn').forEach(checkIfRef);
31+
32+
if(typeof(document.getElementById('clipboard-js')) !== 'undefined')
33+
new ClipboardJS('.ref-btn', {
34+
target: function(trigger) {
35+
return trigger.nextElementSibling;
36+
}
37+
})
38+
39+
}, 1000)
40+
41+
/* How to use clipboard.js dynamically */
42+
43+
document.addEventListener('hashchange', function () {
44+
clipboard.destroy();
45+
})
46+

roam-get-linked-refs-data/README.md

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
##roam-get-linked-refs-data
2+
Adds a `data-page-title` attribute to the top element for each page listed in the Linked References section. The value is equal to the title of that page in the database, and is accessible directly through JavaScript and CSS.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
2+
function checkLinkedRefs() {
3+
var linkedRefsCaret = document.querySelectorAll('.rm-reference-main .rm-reference-container > .flex-h-box > span > span.bp3-icon-caret-down')[0];
4+
var linkedRefsDiv = document.querySelectorAll('.rm-mentions.refs-by-page-view')[0];
5+
6+
/* If the Linked References panel is already open, add the data right away */
7+
if(linkedRefsCaret.classList.contains("rm-caret-open") & typeof(linkedRefsDiv) !== 'undefined'){
8+
let linkedRefsTitles = linkedRefsDiv.querySelectorAll('a span.rm-page__title');
9+
linkedRefsTitles.forEach(function (titleSpan) {
10+
titleSpan.closest('div.rm-ref-page-view').dataset.pageTitle = titleSpan.textContent;
11+
})
12+
}
13+
14+
/* Watch for future events that hide/show Linked References */
15+
linkedRefsCaret.addEventListener('click', function () {
16+
if (linkedRefsCaret.classList.contains("rm-caret-closed")) {
17+
18+
while (typeof (linkedRefsDiv) === 'undefined') {
19+
setTimeout(function () {
20+
linkedRefsDiv = document.querySelectorAll('.rm-mentions.refs-by-page-view')[0];
21+
}, 500)
22+
}
23+
24+
let linkedRefsTitles = linkedRefsDiv.querySelectorAll('a span.rm-page__title');
25+
linkedRefsTitles.forEach(function (titleSpan) {
26+
titleSpan.closest('div.rm-ref-page-view').dataset.pageTitle = titleSpan.textContent;
27+
})
28+
}
29+
});
30+
31+
}
32+
33+
function isPage(){
34+
return(window.location.href.includes("/page/"))
35+
}
36+
37+
window.onload = function(){
38+
console.log('New load event');
39+
if(isPage()){
40+
checkLinkedRefs();
41+
}
42+
}
43+
44+
window.onhashchange = function(){
45+
console.log('New hash change event');
46+
if(isPage()){
47+
checkLinkedRefs();
48+
}
49+
}
50+

roam-get-paper-data/README.md

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
##roam-get-paper-data
2+
Adds a `data-paper-title` attribute to each page reference that links to a page whose title starts with `@`. The value is equal to the contents of the block with attribute `Title::` located on that page, and is accessible directly in JavaScript and CSS.
+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
2+
document.addEventListener('mouseover', (e) => {
3+
let target = e.target;
4+
5+
if (target.classList.contains("rm-page-ref")) {
6+
console.log('Checking if this is a paper...');
7+
console.log(target);
8+
9+
let parentDiv = target.parentElement;
10+
11+
let isPageRef = null;
12+
let isPageTag = null;
13+
14+
if (typeof (parentDiv.dataset.linkTitle) !== 'undefined') {
15+
isPageRef = parentDiv.dataset.linkTitle.startsWith("@");
16+
console.log('It looks like a page reference ?');
17+
}
18+
if (typeof (target.dataset.tag) !== 'undefined') {
19+
isPageTag = parentDiv.dataset.tag.startsWith("@");
20+
console.log('It looks like a tag ?');
21+
}
22+
23+
if (isPageRef | isPageTag) {
24+
console.log("Let's look for a paper title...");
25+
if (typeof (parentDiv.dataset.paperTitle) === 'undefined') {
26+
console.log('This link has no title info yet');
27+
let pageName = null;
28+
if (isPageRef) { pageName = parentDiv.dataset.linkTitle; }
29+
if (isPageTag) { pageName = parentDiv.dataset.tag; }
30+
31+
let attrTitle = window.roamAlphaAPI.q('[:find ?bString :in $ ?pageName :where [?page :node/title ?pageName] [?page :block/children ?titleBlock] [?titleBlock :block/string ?bString] [?titleBlock :block/refs ?bAttr] [?bAttr :node/title "Title"]]', pageName);
32+
if (typeof (attrTitle[0] !== 'undefined')) {
33+
console.log('Now adding the title that was found');
34+
parentDiv.dataset.paperTitle = attrTitle[0][0].replace("Title:: ", "");
35+
}
36+
}
37+
}
38+
}
39+
})

0 commit comments

Comments
 (0)