Last active
September 5, 2020 01:32
-
-
Save dotherightthing/8d0b290ea5b9198d73d0ad53799c33fe to your computer and use it in GitHub Desktop.
Inject Gist into Codepen
This file contains hidden or 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
function embedGist(url) { | |
// See duplicate in https://codepen.io/dotherightthingnz/pen/mdPqgda | |
// based on https://codersblock.com/blog/customizing-github-gists/ | |
// TODO: Codepen also has an API: https://blog.codepen.io/documentation/prefill/ | |
function handleDone(data) { | |
const filenames = Object.keys(data.files); | |
// const convertor = "https://cdn.jsdelivr.net/npm/[email protected]/dist/remarkable.min.js"; | |
const convertor = | |
"https://cdn.jsdelivr.net/remarkable/1.7.1/remarkable.min.js"; | |
filenames.forEach((filename, index) => { | |
let code = data.files[filename].content; | |
let filenameId = filename.replace(".", "-"); | |
if (filename.match(/.css/)) { | |
$("head").append(`<style id="${filenameId}">${code}</style>`); | |
} else if (filename.match(/.js/)) { | |
$("body").append(`<script id="${filenameId}">${code}<\/script>`); | |
} else if (filename.match(/.md/)) { | |
$("body").append( | |
`<script id="${filenameId}" src="${convertor}"><\/script>` | |
); | |
setTimeout(function () { | |
var md = new Remarkable(); | |
$("body").prepend(md.render(code)); | |
}, 100); | |
} else if (filename.match(/.html/)) { | |
$("body").append(code); | |
} | |
}); | |
} | |
function handleFail(e) { | |
console.log(e); | |
} | |
$.get(url).done(handleDone).fail(handleFail); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment