forked from koajs/koajs.com
-
Notifications
You must be signed in to change notification settings - Fork 1
/
pug-options.js
72 lines (55 loc) · 2.31 KB
/
pug-options.js
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
{
filters:
{
markdown: function (text)
{
var marked = require("marked");
var renderer = new marked.Renderer();
// var highlight = require('highlight.js').highlight;
var runkitRegExp = /^<!--\s*runkit:endpoint((.|\n)*)-->(.|\n)*$/;
var runkitContext = { options: '{}', activated: false };
renderer.code = function(code, lang, escaped) {
if (!global.exampleCount)
global.exampleCount = 0;
// var out = highlight(lang, code).value || code;
var out = code
var escaped = out !== code ? out : escapeCode(out, true);
var id = "example-" + (global.exampleCount++);
var script = runkitContext.activated ? "<script>(" + endpoint + ")(\"" + id + "\")</script>" : "";
runkitContext.activated = false;
return "<div id = \"" + id + "\"><pre><code class = \"lang-" + lang + "\">" + escaped + "</code></pre></div>" + script;
};
renderer.html = function(text) {
var result = runkitRegExp.exec(text);
if (!result) return text;
runkitContext.activated = true;
return text;
};
return marked(text, { renderer: renderer });
function endpoint(id, count) {
if (!window.RunKit)
if (typeof count === "undefined" || count < 20)
return setTimeout(endpoint, 500, id, count || 0 + 1);
else
return;
var parent = document.getElementById(id);
var source = parent.textContent;
parent.innerHTML = "";
RunKit.createNotebook({
element: parent,
nodeVersion: "8.x.x",
source: source,
mode: "endpoint"
});
}
function escapeCode(code) {
return code
.replace(/&/g, '&')
.replace(/</g, '<')
.replace(/>/g, '>')
.replace(/"/g, '"')
.replace(/'/g, ''');
}
}
}
}