Skip to content

Instantly share code, notes, and snippets.

@artpi
Created August 5, 2021 19:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save artpi/1966df96c8e7233e2aef268b41ea37bf to your computer and use it in GitHub Desktop.
Save artpi/1966df96c8e7233e2aef268b41ea37bf to your computer and use it in GitHub Desktop.
document.addEventListener("click", function ( e ) {
const target = e.target;
if (
target.tagName === "BUTTON" &&
target.innerText === 'WordPress'
) {
const id = target.closest('.roam-block').id;
const blockUid = id.substring(id.length - 9, id.length);
const data = window.roamAlphaAPI.q('[ :find (pull ?e [ :block/string :block/heading :block/order :block/children :block/refs :children/view-type {:block/children ...} {:block/refs [*]}]) :in $ ?uid :where [?e :block/uid ?uid]]', blockUid);
const text = blockToHTML( data[0][0] );
console.log( 'Text prepared:', text );
navigator.clipboard.writeText( text ).then(function() {
alert( 'Copied to clipboard!' );
} );
}
});
let findAttributes = function( attrs ) {
// This is hackish and probably will require some warnings.
// This expects each attribute to have three entries.
const ret = {};
attrs.forEach( function( attr ) {
ret[ attr[1].value[1] ] = attr[2].value;
} );
return ret;
};
function getUrlFromAttributeInPageName( block, source ) {
let urlGuid = 'wrtR2OxVI';
let ret = false;
block.refs.forEach( function ( reference ) {
if ( source === reference.title ) {
const urls = findAttributes( reference.attrs );
if( urls[urlGuid] ) {
ret = urls[urlGuid];
}
}
} );
return ret;
}
function blockToHTML ( block ){
let string = '';
if( block.string && block.string !== '{{WordPress}}' ) {
// Poor man's MD parser
string = block.string;
string = string.replace( /\!\[([^\]]*?)\]\(([^\)]+)\)/g, '<img src="$2"/>' );
string = string.replace( /\[([^\]]+)\]\((http|https)([^\)]+)\)/g, '<a href="$2$3">$1</a>' );
string = string.replace( /(^|[^"?/])((evernote|http|https|mailto):[a-zA-Z0-9\/.\?\&=;\-_]+)/g, '$1<a href="$2">$2</a>' );
string = string.replace( /\*\*([^*]+)\*\*/g, '<b>$1</b>' );
string = string.replace( /__([^_]+)__/g, '<i>$1</i>' );
string = string.replace( /\[\*\]\(\(\([^)]+\)\)\)/g, '' ); // The "replace with text and reference"
string = string.replace( /\(<a[^>]+readwise[^>]+>Location [0-9]+<\/a>\)/g, '' ); // Remove readwise links
// References in URLs with the changed name
string = string.replace( /\[([^\]]+)\]\(\[\[([^\]]+)\]\]\)/g, function( all, title, source ) {
if( ! block.refs ) {
return source;
}
const url = getUrlFromAttributeInPageName( block, source );
if( ! url ) {
return source;
}
return `<a href='${url}'>${title}</a>`;;
} );
// Before we continue any further, we have to replace all references to actual URLs.
string = string.replace( /#?\[\[([^\]]+)\]\]/g, function( all, source ) {
if( ! block.refs ) {
return source;
}
const url = getUrlFromAttributeInPageName( block, source );
if( ! url ) {
return source;
}
return `<a href='${url}'>${source}</a>`;;
} );
}
let children = '';
if ( block.children ) {
const childrenItems = block.children
.sort( function( a, b ) {
if( a.order < b.order ) {
return -1;
}
if ( a.order > b.order ) {
return 1;
}
return 0;
} )
.map( function( child ) {
let childHTML = blockToHTML( child );
if ( child['heading'] ) {
const headerNumber = child['heading']+1;
return `<h${headerNumber}>${childHTML}</h${headerNumber}>`;
} else if ( block['view-type'] === 'document' ) {
if ( childHTML.substr(0, 2) === '> ' ) {
return"<blockquote>" + childHTML.substr( 2 ) + "</blockquote>";
}
return `<p>${childHTML}</p>`;
} else {
// For both bullet and numbered
return `<li>${childHTML}</li>`;
}
});
children = childrenItems.join("\r\n");
if ( block['view-type'] === 'numbered' ) {
children = `<ol>${children}</ol>`;
}
if ( !block['view-type'] || block['view-type'] === 'bullet' ) {
children = `<ul>${children}</ul>`;
}
}
return `${string}${children}`;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment