-
Notifications
You must be signed in to change notification settings - Fork 474
/
JSRef.ejs
251 lines (214 loc) · 8.05 KB
/
JSRef.ejs
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
<%
// Generate quick links for JavaScript standard objects docs
//
// TODO: Ordering of prototype / static methods (bug 948576)
//
// Parameters are OBSOLETE. DO NOT USE ANYMORE.
//
// $0 - JavaScript reference section (e.g Global_Objects) – OBSOLETE
// $1 - JavaScript reference object/subject (e.g. Number, Array, Function) – OBSOLETE
// $2 - Related objects to display (comma separated) – OBSOLETE
// Strings
const commonl10n = web.getJSONData("L10n-Common");
const jsl10n = web.getJSONData("L10n-JavaScript");
const text = {
stdlib: mdn.getLocalString(jsl10n, "stdlib"),
Constructor: mdn.getLocalString(commonl10n, "Constructor"),
Properties: mdn.getLocalString(commonl10n, "Properties"),
Methods: mdn.getLocalString(commonl10n, "Methods"),
Inheritance: mdn.getLocalString(commonl10n, "Inheritance"),
Related: mdn.getLocalString(commonl10n, "Related_pages_wo_group"),
};
// Variables
const slug = env.slug;
const locale = env.locale;
const rtlLocales = ["ar", "he", "fa"];
const slugStdlib = `/${env.locale}/docs/Web/JavaScript/Reference/Global_Objects`;
const subPath = slug.replace("Web/JavaScript/Reference/Global_Objects/", "")
let subPathParts = subPath.split("/");
// For Intl, we should use the first two parts of the slug if has
// e.g. subPath startswith "Intl/Collator" we should use "Intl.Collator"
// note that Intl owns some methods, we have some special handling for it later
subPathParts = subPathParts.slice(0, subPathParts[0] === "Intl" ? 2 : 1);
let mainObj = subPathParts.join(".");
// Data for inheritance chain
const inheritanceData = {
Math: ["Object"],
Date: ["Object"],
Function: ["Object"],
Object: [],
JSON: ["Object"],
Intl: ["Object"],
arguments: [],
Reflect: ["Object"],
Proxy: [],
Atomics: ["Object"]
};
const inheritance = inheritanceData[mainObj.split(".")[0]] ?? ["Object", "Function"];
// Data for related pages
const groupData = {
Error: ["Error", "EvalError", "InternalError", "RangeError", "ReferenceError", "SyntaxError", "TypeError", "URIError"],
Intl: ["Intl", "Intl.Collator", "Intl.DateTimeFormat", "Intl.ListFormat", "Intl.NumberFormat", "Intl.PluralRules", "Intl.RelativeTimeFormat", "Intl.Segmenter"],
TypedArray: ["TypedArray", "Int8Array", "Uint8Array", "Uint8ClampedArray", "Int16Array", "Uint16Array",
"Int32Array", "Uint32Array", "Float32Array", "Float64Array", "BigInt64Array", "BigUint64Array"],
Proxy: ["Proxy", "handler"]
};
// Exceptions, we want the main object in the sidebar (e.g. Int8Array -> TypedArray)
if (groupData.TypedArray.includes(mainObj)) {
mainObj = "TypedArray";
} else if (groupData.Error.includes(mainObj)) {
mainObj = "Error";
} else if (groupData.Proxy.includes(mainObj)) {
mainObj = "Proxy/handler";
} else if (mainObj.startsWith("Intl") && !groupData.Intl.includes(mainObj)) {
// Special case for Intl, we should make sure that the main object is Intl
// as Intl owns some methods (e.g. Intl.getCanonicalLocales)
mainObj = "Intl";
}
// Get related pages from groups and exclude self
let group = [];
for(const key in groupData) {
const index = groupData[key].indexOf(mainObj);
if(index !== -1) {
group = groupData[key];
group.splice(index, 1);
break;
}
}
const mainObjPath = mainObj.replaceAll(".", "/"); // e.g. Intl.Collator -> Intl/Collator
// Collect pages
const source = {};
source[mainObj] = await page.subpagesExpand(`/en-US/docs/Web/JavaScript/Reference/Global_Objects/${mainObjPath}`);
const result = {};
result[mainObj] = {
title: mainObj,
subPath: mainObjPath,
constructors: [],
methods: [],
properties: [],
defaultOpened: true
};
if (inheritance.includes("Function")) {
source.iFunction = await page.subpagesExpand('/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function');
result.iFunction = {
title: "Function",
subPath: "Function",
constructors: [],
methods: [],
properties: [],
defaultOpened: false
};
}
if (inheritance.includes("Object")) {
source.iObject = await page.subpagesExpand('/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object');
result.iObject = {
title: "Object",
subPath: "Object",
constructors: [],
methods: [],
properties: [],
defaultOpened: false
};
}
function isPrototypeMemberPage(page) {
return [
"javascript-instance-accessor-property",
"javascript-instance-data-property",
"javascript-instance-method",
].includes(page.pageType);
}
function isPropertyPage(page) {
return [
"javascript-static-accessor-property",
"javascript-static-data-property",
"javascript-instance-accessor-property",
"javascript-instance-data-property",
].includes(page.pageType);
}
function isMethodPage(page) {
return [
"javascript-static-method",
"javascript-instance-method",
].includes(page.pageType);
}
function isConstructorPage(page) {
return "javascript-constructor" === page.pageType;
}
for (const key in source) {
let pageList = source[key];
if (key === "iObject") {
// For objects, we're only interested in prototype members.
pageList = pageList.filter(page => isPrototypeMemberPage(page));
}
for (const page of pageList) {
if (isConstructorPage(page)) {
result[key].constructors.push(page);
}
if (isPropertyPage(page)) {
result[key].properties.push(page);
}
if (isMethodPage(page)) {
result[key].methods.push(page);
}
}
}
// Output helper
async function buildSublist(pages, title, opened) {
const getTitle = locale === "en-US"
? page => page.title
: page => page.translations().find(translation => translation.locale === locale)?.title ?? page.title;
const items = pages.map(async aPage => {
const url = aPage.url.replace("en-US", locale);
const title = getTitle(aPage);
const pageBadges = (await page.badges(aPage)).join("");
const start = rtlLocales.includes(locale) ? "<li><bdi>" : "<li>";
const end = rtlLocales.includes(locale) ? "</bdi></li>" : "</li>";
return `${start}${web.smartLink(url, null, `<code>${title}</code>`, aPage.slug, slugStdlib, "JSRef")}${pageBadges}${end}`
})
// Wait for all the links to be built
const resolvedItems = await Promise.all(items);
const start = opened ? "<li><details open>" : "<li><details>";
const end = "</details></li>";
const result = `${start}<summary>${title}</summary></a><ol>${resolvedItems.join("")}</ol>${end}`
return result;
}
// Output
async function renderOutput() {
let output = '<section class="Quick_links" id="Quick_links" data-macro="JSRef"><ol>';
const link = web.smartLink(slugStdlib, null, text.stdlib, slugStdlib, slugStdlib, "JSRef");
output += `<li class="section">${link}</li>`;
for (const [index, item] of Object.values(result).entries()) {
const resultTitle = item.title ?? "";
const resultSubPath = item.subPath ?? "";
const resultConstructors = item.constructors ?? [];
const resultProperties = item.properties ?? [];
const resultMethods = item.methods ?? [];
const resultOpen = item.defaultOpened ?? false;
if (index === 1) {
output += `<li class="section no-link">${text.Inheritance}</li>`;
}
const link = web.smartLink(`${slugStdlib}/${resultSubPath}`, null, `<code>${resultTitle}</code>`, null, slugStdlib, "JSRef");
output += `<li class="section">${link}</li>`;
if (resultConstructors.length > 0) {
output += await buildSublist(resultConstructors, text.Constructor, resultOpen);
}
if (resultProperties.length > 0) {
output += await buildSublist(resultProperties, text.Properties, resultOpen);
}
if (resultMethods.length > 0) {
output += await buildSublist(resultMethods, text.Methods, resultOpen);
}
if (index === 0 && group.length > 0) {
output += `<li class="section no-link">${text.Related}</li>`;
for (const groupItem of group) {
const link = web.smartLink(`${slugStdlib}/${groupItem.replace(".", "/")}`, null, `<code>${groupItem}</code>`, null, slugStdlib, "JSRef");
output += `<li class="section">${link}</li>`;
}
}
}
output += "</ol></section>";
return output;
}
const output = await renderOutput();
%>
<%-output%>