Last active
October 15, 2022 02:52
-
-
Save ggicci/c85b4665e959a10fdbe0c97b33f44eb0 to your computer and use it in GitHub Desktop.
goplay for ghost blog code injection
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<style> | |
.goplay-container { margin-top: 8px; } | |
.goplay-run { margin-right: 5px; } | |
.goplay-output .stderr { color: #b50000; } | |
.goplay-output .system { color: green; } | |
</style> | |
<script type="module"> | |
import { GoPlayProxy } from "https://unpkg.com/@ggicci/goplay/dist/index.js"; | |
const goplay = new GoPlayProxy("https://goplay.ggicci.me"); | |
function initGoPlay(container) { | |
const targetPre = container.previousElementSibling; | |
if (!targetPre) { | |
console.warn("goplay: no target pre node (previous pre sibling of .goplay-container), skip init."); | |
return; | |
} | |
const code = targetPre.textContent.trim(); | |
const runButton = document.createElement("button"); | |
runButton.classList.add("goplay-run"); | |
runButton.textContent = "🚀 Run"; | |
container.append(runButton); | |
const shareButton = document.createElement("button"); | |
shareButton.classList.add("goplay-share"); | |
shareButton.textContent = "Share"; | |
container.append(shareButton); | |
runButton.onclick = function() { | |
let outputContainer = container.querySelector(".goplay-output"); | |
if (!outputContainer) { | |
const outputPre = document.createElement("pre"); | |
outputContainer = document.createElement("code"); | |
outputContainer.classList.add("goplay-output", "hljs"); | |
outputPre.appendChild(outputContainer); | |
container.prepend(outputPre); | |
} | |
goplay.renderCompile(outputContainer, code); | |
}; | |
shareButton.onclick = async function() { | |
const shareUrl = await goplay.share(code); | |
window.open(shareUrl, "_blank").focus(); | |
}; | |
} | |
document.querySelectorAll('pre+.goplay-container').forEach(initGoPlay); | |
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- add a HTML section in your post right after your code block --> | |
<div class="goplay-container"></div> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment