-
Notifications
You must be signed in to change notification settings - Fork 101
/
index.js
210 lines (167 loc) · 4.54 KB
/
index.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
(function(){
function titleToId(title) {
return title.trim().replace(/^\$\.|\(\)$/g, "");
}
// Add ids and implementation to all functions
$$("#docs article > h1").forEach(function(h1) {
var article = h1.parentNode;
h1.firstChild.textContent = h1.firstChild.textContent.trim();
var fn = titleToId(h1.firstChild.textContent);
if (article && !article.id) {
article.id = "fn-" + fn;
}
$.create("a", {
href: "#" + article.id,
around: h1.firstChild
});
article._.contents({
tag: "button",
textContent: "Show implementation",
className: "implementation",
onclick: function(){
var pre = this.nextElementSibling;
if (!pre || !pre.matches("pre")) {
pre = document.createElement("pre")._.after(this);
if (fn === "$" || fn === "$$") {
var source = self[fn] + "";
}
else if (fn.indexOf("hooks.") === 0) {
var source = $.hooks[fn.replace(/^hooks./, "")] + "";
}
else {
var source = ($.sources[fn] || $[fn]) + "";
}
source = source.replace(/^\t/gm, "");
var code = $.create("code", {
textContent: source,
inside: pre
});
Prism.highlightElement(pre);
}
this.textContent = (pre.classList.contains("visible")? "Show" : "Hide") + " implementation";
pre.classList.toggle("visible");
}
});
});
// Add ids to all other sections
$$("section h1").forEach(function(h1) {
var section = h1.closest("section, article");
if (section && !section.id) {
section.id = h1.textContent.toLowerCase().replace(/[^\w\s-]/g, "").replace(/\s+/g, "-");
}
});
if (/\/docs(?:\.html)?$/.test(location.pathname)) {
// Table of Contents
var tocList = $("#toc ol");
$$("body > section > h1").forEach(function(h1) {
var section = h1.parentNode;
function toLi(h1) {
return $.create("li", {
contents: {
"tag": "a",
"href": "#" + h1.parentNode.id,
"textContent": h1.firstChild.textContent
}
});
}
var li = toLi(h1);
var subheadings = $$("body > section section h1, body > section article h1", section);
if (subheadings.length) {
$.create("ol", {
contents: subheadings.map(toLi),
inside: li
});
}
tocList.appendChild(li);
});
$$("article[id^='fn-'] h1.set").forEach(function(h1){
var id = h1.parentNode.id;
$.set(document.createDocumentFragment(), {
contents: [
{tag: "dt",
contents: {tag: "a",
href: "#" + id,
textContent: id.replace(/^fn-/, "")
}
},
{tag: "dd",
textContent: h1.nextElementSibling.textContent
}
],
start: $("#fn-set .args dl")
});
});
$$("a.jq").forEach(function(a){
if (!a.href) {
var content = a.textContent;
var url = "http://api.jquery.com/";
var fn = content.match(/jQuery(?:\.fn)?\.([a-z]+)/i)[1];
if (content.indexOf('jQuery.fn') === -1) {
url += "jQuery."
}
a.href = url + fn;
}
a.texContent += "()";
});
}
// Find references to Bliss functions and make them links to the docs
$$(":not(pre) > code").forEach(function(code){
if (/\$.*\(\)/.test(code.textContent)) {
$.create("a", {
href: "#fn-" + titleToId(code.textContent),
around: code
});
}
});
$$("#download input[type=radio]")._.events({click: function(){
var elements = this.form.elements;
$("#download a[download]").href = "bliss" + elements.type.value + elements.compression.value + ".js";
}});
$$(".runnable").forEach(function(p){
$.create("button", {
textContent: "Run",
className: "run",
once: {
click: function(){
$.create("script", {
textContent: $("pre.bliss code", p.nextElementSibling).textContent,
after: p
});
$.remove(this);
}
},
inside: p
})
});
// Linkify types to MDN
var urls = {};
// Add global objects
["Object", "String", "Array", "Number", "Function", "RegExp", "Boolean"].forEach(function(type){
urls[type] = "https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/" + type;
});
// Add APIs
["Node", "Element", "Text", "Document", "Promise"].forEach(function(type){
urls[type] = "https://developer.mozilla.org/en-US/docs/Web/API/" + type;
});
self.typeRegex = RegExp("\\b(?:" + Object.keys(urls).join("|") + ")\\b", "g");
$$(".args dt[class]").forEach(function(dt){
var types = ["Type: "];
$$(dt.classList).forEach(function(clas, i, arr){
var type = (clas + "").replace(/^./, function($0) { return $0.toUpperCase() });
if (urls[type]) {
if (i > 0) {
types.push(i === arr.length - 1? " or " : ", ");
}
types.push($.create("a", {
href: urls[type],
textContent: type
}));
}
});
$.create("dd", {
className: "type",
contents: types,
after: dt
});
});
})();