Skip to content

Commit b7bbcaa

Browse files
authored
enhance(macros/APIRef): show badges on all links (#9757)
* feat(macros/APIRef): show badges on all links * test(macros/APIRef): mock wiki.getPage() * fix(macros/APIRef): fall back to en-US to determine badges
1 parent 4585241 commit b7bbcaa

File tree

2 files changed

+23
-10
lines changed

2 files changed

+23
-10
lines changed

Diff for: kumascript/macros/APIRef.ejs

+20-10
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,17 @@ function getPageTitle(page) {
165165
return title;
166166
}
167167
168+
async function smartLinkWithBadges(url, content) {
169+
let aPage = await wiki.getPage(url);
170+
if (!aPage.title && env.locale !== 'en-US') {
171+
const originalUrl = url.replace(`/${env.locale}/`, "/en-US/");
172+
aPage = await wiki.getPage(originalUrl);
173+
}
174+
const link = web.smartLink(url, null, content, APIHref, null, "APIRef");
175+
const badges = (await page.badges(aPage)).join("");
176+
return link + badges;
177+
}
178+
168179
async function buildSublist(pages, title) {
169180
var result = '<li class="toggle"><details open><summary>' + title + '</summary><ol>';
170181
@@ -185,8 +196,7 @@ async function buildSublist(pages, title) {
185196
if (slug == aPage.slug) {
186197
result += `<em><code>${title}</code> ${pageBadges}</em>`
187198
} else {
188-
result += web.smartLink(url, null, `<code>${title}</code>`, APIHref, null, "APIRef");
189-
result += pageBadges;
199+
result += await smartLinkWithBadges(url, `<code>${title}</code>`);
190200
}
191201
192202
if (rtlLocales.indexOf(locale) != -1) {
@@ -201,13 +211,13 @@ async function buildSublist(pages, title) {
201211
return result;
202212
}
203213
204-
function buildIFList(interfaces, title) {
205-
var result = '<li class="toggle"><details open><summary>' + title + '</summary><ol>';
214+
async function buildIFList(interfaces, title) {
215+
let result = '<li class="toggle"><details open><summary>' + title + '</summary><ol>';
206216
207217
for (var i = 0; i < interfaces.length; i++) {
208218
var url = APIHref + '/' + interfaces[i].replace('()', '').replace('.', '/');
209219
if (!url.endsWith(slug)) {
210-
result += `<li>${web.smartLink(url, null, `<code>${interfaces[i]}</code>`, APIHref, null, "APIRef")}</li>`;
220+
result += `<li>${await smartLinkWithBadges(url, `<code>${interfaces[i]}</code>`)}</li>`;
211221
}
212222
}
213223
@@ -219,10 +229,10 @@ function buildIFList(interfaces, title) {
219229
// output
220230
output = '<section id="Quick_links" data-macro="APIRef"><ol>';
221231
if (group && webAPIGroups[0][group] && webAPIGroups[0][group].overview) {
222-
output += `<li class="section">${web.smartLink(APIHref + '/' + webAPIGroups[0][group].overview[0].replace(/ /g, '_'), null, webAPIGroups[0][group].overview[0], APIHref, null, "APIRef")}</li>`;
232+
output += `<li class="section">${await smartLinkWithBadges(APIHref + '/' + webAPIGroups[0][group].overview[0].replace(/ /g, '_'), webAPIGroups[0][group].overview[0])}</li>`;
223233
}
224234
225-
output += `<li class="section">${web.smartLink(APIHref + '/' + mainIF, null, `<code>${mainIF}</code>`, APIHref, null, "APIRef")}</li>`;
235+
output += `<li class="section">${await smartLinkWithBadges(APIHref + '/' + mainIF, `<code>${mainIF}</code>`)}</li>`;
226236
227237
if (ctors.length > 0) {
228238
output += await buildSublist(ctors, text['Constructor']);
@@ -243,14 +253,14 @@ if (events.length > 0) {
243253
output += await buildSublist(events, text['Events']);
244254
}
245255
if (inh.length > 0) {
246-
output += buildIFList(inheritedIF, text['Inheritance']);
256+
output += await buildIFList(inheritedIF, text['Inheritance']);
247257
}
248258
if (implementedBy.length > 0) {
249-
output += buildIFList(implementedBy, text['Implemented_by']);
259+
output += await buildIFList(implementedBy, text['Implemented_by']);
250260
}
251261
252262
if (related.length > 0) {
253-
output += buildIFList(related, text['Related']);
263+
output += await buildIFList(related, text['Related']);
254264
}
255265
256266
output += '</ol></section>';

Diff for: kumascript/tests/macros/apiref.test.ts

+3
Original file line numberDiff line numberDiff line change
@@ -685,6 +685,9 @@ describeMacro("APIRef", function () {
685685
expect(page).toEqual("/en-US/docs/Web/API/TestInterface");
686686
return subpagesFixture;
687687
});
688+
macro.ctx.wiki.getPage = jest.fn((url) => {
689+
return subpagesFixture.find((doc) => doc.url === url) ?? {};
690+
});
688691
});
689692

690693
// Test with current page as main interface page

0 commit comments

Comments
 (0)