-
Notifications
You must be signed in to change notification settings - Fork 513
/
WebExtAPISidebar.ejs
167 lines (138 loc) · 4.18 KB
/
WebExtAPISidebar.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
<%
var output = "";
var pageModule = page;
var slug = env.slug;
var locale = env.locale;
var baseURL = "/" + locale + "/docs/";
var baseAPIPage = baseURL + "Mozilla/Add-ons/WebExtensions/API";
var htmlEscape = mdn.htmlEscape;
var rtlLocales = ["ar", "he", "fa"];
let commonl10n = web.getJSONData("L10n-Common");
let text = {
translate: mdn.getLocalString(commonl10n, "[Translate]"),
title: mdn.getLocalString(commonl10n, "TranslationCTA"),
Methods: mdn.getLocalString(commonl10n, "Methods"),
Properties: mdn.getLocalString(commonl10n, "Properties"),
Types: mdn.getLocalString(commonl10n, "Types"),
Events: mdn.getLocalString(commonl10n, "Events"),
};
async function buildSublist(pages, title, ignoreBadges) {
var result =
'<li class="section"><a href="#">' +
title +
"</strong></a><ol>";
for (var i in pages) {
var aPage = pages[i];
var url = aPage.url.replace("en-US", locale);
var title = htmlEscape(aPage.title);
var apiComponentName = title;
var titlePieces = title.split(".");
if (titlePieces.length > 1) {
apiComponentName = titlePieces[titlePieces.length - 1];
}
if (locale !== "en-US") {
for (const translation of aPage.translations()) {
if (translation.locale === locale) {
title = htmlEscape(translation.title);
}
}
}
result += "<li>";
let pageBadges = "";
if (!ignoreBadges) {
pageBadges = (await page.badges(aPage)).join("");
}
if (rtlLocales.indexOf(locale) != -1) {
result += "<bdi>";
}
if (slug == aPage.slug) {
result += `<em><code>${apiComponentName}</code> ${pageBadges}</em>`;
} else {
result += `<a href="${url}"><code>${apiComponentName}</code></a>${pageBadges}`;
}
if (rtlLocales.indexOf(locale) != -1) {
result += "</bdi>";
}
result += "</li>";
}
result += "</ol></li>";
return result;
}
function classifyPages(subpages) {
const collection = {
properties: [],
methods: [],
events: [],
types: [],
};
for (const subpage of subpages) {
switch (subpage.pageType) {
case "webextension-api-property":
collection.properties.push(subpage);
break;
case "webextension-api-function":
collection.methods.push(subpage);
break;
case "webextension-api-type":
collection.types.push(subpage);
break;
case "webextension-api-event":
collection.events.push(subpage);
break;
}
}
return collection;
}
async function buildSidebarForSingleAPI(apiPath) {
output += "<ol>";
var subpages = await pageModule.subpagesExpand(apiPath);
var pageCollection = classifyPages(subpages);
if (pageCollection.methods.length > 0) {
output += await buildSublist(pageCollection.methods, text["Methods"], true);
}
if (pageCollection.properties.length > 0) {
output += await buildSublist(
pageCollection.properties,
text["Properties"],
true
);
}
if (pageCollection.types.length > 0) {
output += await buildSublist(pageCollection.types, text["Types"], true);
}
if (pageCollection.events.length > 0) {
output += await buildSublist(pageCollection.events, text["Events"], true);
}
output += "</ol>";
}
async function buildSidebarForAllAPIs() {
output = "<ol>";
var browserSupportURL =
baseURL +
"Mozilla/Add-ons/WebExtensions/Browser_support_for_JavaScript_APIs";
var browserSupportTitle = "Browser support for JavaScript APIs";
output +=
'<li><a href="' + browserSupportURL + '">' + browserSupportTitle + "</a>";
var subpages = await pageModule.subpagesExpand(baseAPIPage);
for (var i = 0; i < subpages.length; i++) {
var apiPage = subpages[i];
var pieces = apiPage.url.split("/");
var apiName = pieces.slice(-1);
if (slug.indexOf(apiPage.slug) != -1) {
output +=
'<li class="webextension-api-sidebar"><a href="' +
apiPage.url +
'">' +
apiName +
"</a>";
await buildSidebarForSingleAPI(baseURL + apiPage.slug);
} else {
output += '<li><a href="' + apiPage.url + '">' + apiName + "</a>";
}
output += "</li>";
}
output += "</ol>";
}
await buildSidebarForAllAPIs(baseURL);
%>
<%-output%>