-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.html
78 lines (68 loc) · 2.46 KB
/
index.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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
<script type="module">
// version 1.0.0
// load the observable runtime
import {Runtime, Inspector} from "https://cdn.jsdelivr.net/npm/@observablehq/runtime@5/dist/runtime.js";
// globally signal that this notebook is being rendered in notebook-view
window.__notebook_view = true;
// track children added to body
const children = {};
// render the notebook
const renderNotebook = notebook => {
log(notebook);
// Load the notebook, and render any cells which start with two underscores: "__".
document.body.innerHTML = '';
(new Runtime).module(notebook, name => {
log('cell name', name);
if (name && (name.startsWith('__') || name.startsWith('viewof __'))) {
return {
fulfilled: value => {
log(name, 'value:', value);
if (value instanceof HTMLElement || value instanceof SVGElement) {
if (children[name]) {
document.body.replaceChild(value, children[name]);
log('replace:', name);
} else {
document.body.appendChild(value);
log('append:', name);
}
children[name] = value;
} else {
log('unexpected cell type', name, value);
}
},
rejected: error => {
log('rejected cell', name, error.message);
}
};
}
});
}
// support url param extraction
const getUrlParams = () => {
var params = {};
window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, (m, key, value) => params[key] = value);
return params;
}
const urlParams = getUrlParams();
const getUrlParam = (name, defaultValue) => urlParams[name] || defaultValue;
// establish param based values
const debug = getUrlParam('debug', false);
const url = `https://api.observablehq.com/${getUrlParam('name', 'd')}/${getUrlParam('notebook', 'ad46968afb5a1946')}.js?v=4`;
log('loading notebook:', url);
// import the notebook
import(url).then(module => renderNotebook(module.default));
// conditionaly log debugging messages
function log() {
if (debug) {
console.log.apply(this, arguments)
}
}
</script>
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-31130304-2"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'UA-31130304-2');
</script>