From 8c0c7b4f09f44162d5947e40c7c5367a5429c633 Mon Sep 17 00:00:00 2001 From: Claas Augner Date: Mon, 2 Oct 2023 16:17:22 +0200 Subject: [PATCH 1/3] feat(macros/APIRef): show badges on all links --- kumascript/macros/APIRef.ejs | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/kumascript/macros/APIRef.ejs b/kumascript/macros/APIRef.ejs index 6e7e59fe535f..1908d405ab6d 100644 --- a/kumascript/macros/APIRef.ejs +++ b/kumascript/macros/APIRef.ejs @@ -165,6 +165,13 @@ function getPageTitle(page) { return title; } +async function smartLinkWithBadges(url, content) { + const aPage = await wiki.getPage(url); + const link = web.smartLink(url, null, content, APIHref, null, "APIRef"); + const badges = (await page.badges(aPage)).join(""); + return link + badges; +} + async function buildSublist(pages, title) { var result = '
  • ' + title + '
      '; @@ -195,8 +202,7 @@ async function buildSublist(pages, title) { if (slug == aPage.slug) { result += `${title} ${pageBadges}` } else { - result += web.smartLink(url, null, `${title}`, APIHref, null, "APIRef"); - result += pageBadges; + result += await smartLinkWithBadges(url, `${title}`); } if (rtlLocales.indexOf(locale) != -1) { @@ -211,12 +217,12 @@ async function buildSublist(pages, title) { return result; } -function buildIFList(interfaces, title) { - var result = '
    1. ' + title + '
        '; +async function buildIFList(interfaces, title) { + let result = '
      1. ' + title + '
          '; for (var i = 0; i < interfaces.length; i++) { var url = interfaces[i].replace('()', '').replace('.', '/'); - result += `
        1. ${web.smartLink(APIHref + '/' + url, null, `${interfaces[i]}`, APIHref, null, "APIRef")}
        2. `; + result += `
        3. ${await smartLinkWithBadges(APIHref + '/' + url, `${interfaces[i]}`)}
        4. `; } result += '
      2. '; @@ -227,10 +233,10 @@ function buildIFList(interfaces, title) { // output output = ''; From 9d29d47ed099ba54b4b1907591dfb55f12238b3f Mon Sep 17 00:00:00 2001 From: Claas Augner Date: Thu, 29 Feb 2024 23:43:22 +0100 Subject: [PATCH 2/3] test(macros/APIRef): mock wiki.getPage() --- kumascript/tests/macros/apiref.test.ts | 3 +++ 1 file changed, 3 insertions(+) diff --git a/kumascript/tests/macros/apiref.test.ts b/kumascript/tests/macros/apiref.test.ts index 14aeb7f4ddd2..ef72f99965ea 100644 --- a/kumascript/tests/macros/apiref.test.ts +++ b/kumascript/tests/macros/apiref.test.ts @@ -681,6 +681,9 @@ describeMacro("APIRef", function () { expect(page).toEqual("/en-US/docs/Web/API/TestInterface"); return subpagesFixture; }); + macro.ctx.wiki.getPage = jest.fn((url) => { + return subpagesFixture.find((doc) => doc.url === url) ?? {}; + }); }); // Test with current page as main interface page From 55971dd279f7380f0df54389f3f3323b4eeefb83 Mon Sep 17 00:00:00 2001 From: Claas Augner Date: Thu, 29 Feb 2024 23:44:08 +0100 Subject: [PATCH 3/3] fix(macros/APIRef): fall back to en-US to determine badges --- kumascript/macros/APIRef.ejs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/kumascript/macros/APIRef.ejs b/kumascript/macros/APIRef.ejs index 1908d405ab6d..f98cd8907b1c 100644 --- a/kumascript/macros/APIRef.ejs +++ b/kumascript/macros/APIRef.ejs @@ -166,7 +166,11 @@ function getPageTitle(page) { } async function smartLinkWithBadges(url, content) { - const aPage = await wiki.getPage(url); + let aPage = await wiki.getPage(url); + if (!aPage.title && env.locale !== 'en-US') { + const originalUrl = url.replace(`/${env.locale}/`, "/en-US/"); + aPage = await wiki.getPage(originalUrl); + } const link = web.smartLink(url, null, content, APIHref, null, "APIRef"); const badges = (await page.badges(aPage)).join(""); return link + badges;