feat: allow running code in playground

This commit is contained in:
Hatem Hosny committed 2025-01-01 20:05:18 +02:00
1 parent ec29d719ff
commit 7603972889
3 files changed
+66

No files matched your search

+1
View File
@@ -11,6 +11,7 @@ intro: |
A JavaScript cheat sheet with the most important concepts, functions, methods, and more. A complete quick reference for beginners.
plugins:
- copyCode
- runCode
---
## Getting Started
+1
View File
@@ -11,6 +11,7 @@ intro: |
The [Python](https://www.python.org/) cheat sheet is a one-page reference sheet for the Python 3 programming language.
plugins:
- copyCode
- runCode
---
## Getting Started
+64
View File
@@ -72,3 +72,67 @@
});
</script>
<% } %>
<% if (is_post() && page.plugins != undefined && page.plugins.includes("runCode") ){ %>
<script src="https://cdn.jsdelivr.net/npm/livecodes@0.7.2/livecodes.umd.js"></script>
<script>
// Get all pre > code elements
const code_Blocks = document.querySelectorAll('pre > code');
const runInnerHTML = '<svg height="1em" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg"><g stroke-width="0"></g><g stroke-linecap="round" stroke-linejoin="round"></g><g><path d="M16.6582 9.28638C18.098 10.1862 18.8178 10.6361 19.0647 11.2122C19.2803 11.7152 19.2803 12.2847 19.0647 12.7878C18.8178 13.3638 18.098 13.8137 16.6582 14.7136L9.896 18.94C8.29805 19.9387 7.49907 20.4381 6.83973 20.385C6.26501 20.3388 5.73818 20.0469 5.3944 19.584C5 19.053 5 18.1108 5 16.2264V7.77357C5 5.88919 5 4.94701 5.3944 4.41598C5.73818 3.9531 6.26501 3.66111 6.83973 3.6149C7.49907 3.5619 8.29805 4.06126 9.896 5.05998L16.6582 9.28638Z" stroke="currentColor" stroke-width="2" stroke-linejoin="round"></path></g></svg>';
code_Blocks.forEach(function (codeBlock) {
// Create a run button element
const runButton = document.createElement('button');
runButton.className = 'bg-emerald-500 p-2 text-white fadeIn shadow-xl rounded flex items-center absolute top-12 right-2';
runButton.innerHTML = runInnerHTML;
runButton.style.opacity = "0";
const parent = codeBlock.parentNode;
parent.style = "position: relative;"
parent.insertBefore(runButton, codeBlock);
let content = codeBlock.textContent;
let language = [...codeBlock.classList].filter(c => c.startsWith("language-"))[0].replace("language-", "");
const editorId = ['javascript', 'python'].includes(language) ? 'script' : 'markup';
const title = codeBlock.closest('.h3-wrap')?.querySelector('h3')?.textContent.replace('#', '') || '';
if (language === 'python') {
if (content.includes('>>> ')) {
content = content
.trim()
.split('\n')
.map(line => line.startsWith('>>> ') ? line.replace('>>> ', '') : `# ${line}`)
.join('\n');
}
if (content.includes('from typing import')) {
language = 'python-wasm';
}
}
// Add click event listener to run button
runButton.addEventListener('click', () => {
const config = {
title,
activeEditor: editorId,
[editorId]: {
language,
content,
},
tools: {
status: 'open',
}
}
const plaugroundUrl = livecodes.getPlaygroundUrl({config});
window.open(plaugroundUrl, '_blank');
});
parent.addEventListener('mouseenter', () => {
runButton.style.opacity = "1";
});
parent.addEventListener('mouseleave', () => {
runButton.style.opacity = "0";
});
});
</script>
<% } %>