-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathindex.js
More file actions
76 lines (63 loc) · 2.38 KB
/
index.js
File metadata and controls
76 lines (63 loc) · 2.38 KB
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
'use strict';
const {join} = require('path');
const {readFileSync} = require('fs');
const docinfoFunctions = require('./src/docinfo.js');
const css = readFileSync(join(__dirname, 'src', 'style.css')).toString();
// The default version of the runner is based on the version of Node running this code
const DEFAULT_NODE_VERSION = process.version.split('.').shift();
const isInteractive = (type) => {
return (block) => block.isAttribute('language', type) && block.isOption('interactive');
};
const interactiveRunnerTreeProcessor = function(){
const self = this;
self.process(function(doc){
doc.findBy({ context: 'listing' }, isInteractive('javascript'))
.forEach(block => {
const nodeVersion = doc.getAttribute('runner-node') || DEFAULT_NODE_VERSION;
block.addRole('interactive');
block.addRole('interactive--javascript');
block.addRole(`interactive--runtime--node-${nodeVersion}`);
block.setAttribute('runtime', 'runkit');
if (block.isOption('endpoint')) {
block.addRole('interactive--endpoint');
}
});
});
};
const interactiveRunnerDocinfoProcessor = function(){
const self = this;
self.process(function(doc){
const blocks = doc.findBy({ context: 'listing' }, b => b.isAttribute('runtime', 'runkit'));
if (blocks.length === 0 || doc.backend !== 'html5') {
return '';
}
return `<style type="text/css" class="extension-interactive-runner">${css}</style>
<script class="extension-interactive-runner">
(function(d){
document.addEventListener('DOMContentLoaded', function(){
const script = d.createElement('script');
script.src = 'https://embed.runkit.com/';
script.async = true;
script.onload = function(){
${docinfoFunctions.map(f => f.toString()).join('\n')}
installEvents();
document.body.dataset.interactiveRuntime = 'loaded';
};
document.body.appendChild(script);
});
})(document);</script>`;
});
};
module.exports = function (registry) {
registry.treeProcessor(interactiveRunnerTreeProcessor)
registry.docinfoProcessor(interactiveRunnerDocinfoProcessor)
}
module.exports.register = function register (registry) {
if (typeof registry.register === 'function') {
registry.register(function () {
this.treeProcessor(interactiveRunnerTreeProcessor)
this.docinfoProcessor(interactiveRunnerDocinfoProcessor)
})
}
return registry
};