Skip to content

Instantly share code, notes, and snippets.

@deanmcpherson
Last active February 12, 2024 20:12
Show Gist options
  • Save deanmcpherson/c3db37f76655528896146c9a3f4c4a3a to your computer and use it in GitHub Desktop.
Save deanmcpherson/c3db37f76655528896146c9a3f4c4a3a to your computer and use it in GitHub Desktop.
super quick and dirty code embed support
//<script>
function clearBlock(el) {
const node = el.parentElement.parentElement;
node.innerHTML = '';
return node;
}
const SELECTOR = 'code:not([super-embed-seen])';
function setupEmbeds() {
document.querySelectorAll(SELECTOR).forEach((node) => {
node.setAttribute('super-embed-seen', 1);
if (node.innerText.startsWith('super-embed:')) {
const code = node.innerText.replace('super-embed:', '');
const parentNode = clearBlock(node);
parentNode.innerHTML = code;
parentNode.querySelectorAll('script').forEach((script) => {
if (!script.src && script.innerText) {
eval(script.innerText);
} else {
const scr = document.createElement('script');
scr.src = script.src;
document.body.appendChild(scr);
}
})
}
})
}
setupEmbeds();
var observer = new MutationObserver(function(mutations) {
if (document.querySelector(SELECTOR)) {
setupEmbeds();
}
});
observer.observe(document, {attributes: false, childList: true, characterData: false, subtree:true});
//</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment