-
-
Notifications
You must be signed in to change notification settings - Fork 768
/
embed-scripts.html
33 lines (30 loc) · 1.4 KB
/
embed-scripts.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
<script>
function htmlDecode(input) {
var doc = new DOMParser().parseFromString(input, "text/html");
return doc.documentElement.textContent;
}
</script>
{% for example in page.examples %}
{% assign filename = 'examples/' | append: example | append: '.test.js' %}
<template data-example-script="{{ example }}">{% include_relative {{filename}} %}</template>
<script>
(function(){
// get script example div
var element = document.querySelector("[data-example-id={{ example }}]");
// get script example source from the template
var scriptTemplate = document.querySelector("[data-example-script={{ example }}]");
// all content in a HtmlTemplate is HTML, hence we need to unescape
// special characters to get a normal string back
var javascriptAsString = htmlDecode(scriptTemplate.innerHTML);
var notebook = RunKit.createNotebook({
// the parent element for the new notebook
element: element,
// specify the source of the notebook
source: javascriptAsString,
theme: "untilted-c3qcs6lijfuc",
// some examples use ES2015 syntax not supported by the default Node 10
nodeVersion: "18.x.x"
});
})(); // IIFE - to avoid leaking variables
</script>
{% endfor %}