Skip to content

Commit 0807413

Browse files
authored
fix(kumascript): add translations() to page info (#8241)
* feat(kumascript): add translations to page info * fix(macros): do not get translations' urls * define `translations` as a normal function * fix api sidebar and apiref tests
1 parent 0e9d1f1 commit 0807413

11 files changed

+57
-70
lines changed

Diff for: kumascript/macros/APIRef.ejs

-10
Original file line numberDiff line numberDiff line change
@@ -172,16 +172,6 @@ async function buildSublist(pages, title) {
172172
var aPage = pages[i];
173173
var url = aPage.url.replace('en-US', locale);
174174
175-
var translated = false;
176-
if (locale != 'en-US') {
177-
aPage.translations.forEach(function(translation){
178-
if(translation.locale === locale) {
179-
url = translation.url;
180-
translated = true;
181-
}
182-
});
183-
}
184-
185175
const title = getPageTitle(aPage);
186176
187177
result += '<li>';

Diff for: kumascript/macros/CSSRef.ejs

+4-4
Original file line numberDiff line numberDiff line change
@@ -1002,14 +1002,14 @@ function smartLink(href, title, content, subpath) {
10021002
*/
10031003
async function makePageLink(aPage) {
10041004
const url = aPage.url.replace('en-US', locale);
1005-
const title = htmlEscape(aPage.title);
1005+
let title = htmlEscape(aPage.title);
10061006
1007-
if (locale != 'en-US') {
1008-
aPage.translations.forEach(function(translation){
1007+
if (locale !== 'en-US') {
1008+
for (const translation of aPage.translations()) {
10091009
if (translation.locale === locale) {
10101010
title = htmlEscape(translation.title);
10111011
}
1012-
});
1012+
}
10131013
}
10141014
10151015
const pageBadges = (await page.badges(aPage)).join("");

Diff for: kumascript/macros/DefaultAPISidebar.ejs

+6-7
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,12 @@ function buildSublist(pages, title) {
4949
var url = aPage.url.replace('en-US', locale);
5050
var title = htmlEscape(aPage.title);
5151
52-
if (locale != 'en-US' && aPage.translations) {
53-
aPage.translations.forEach(function(translation){
54-
if(translation.locale === locale) {
55-
url = translation.url;
56-
title = htmlEscape(translation.title);
57-
}
58-
});
52+
if (locale !== 'en-US') {
53+
for (const translation of aPage.translations()) {
54+
if (translation.locale === locale) {
55+
title = htmlEscape(translation.title);
56+
}
57+
}
5958
}
6059
6160
result += '<li>';

Diff for: kumascript/macros/JSRef.ejs

+1-1
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ for (const key in source) {
178178
async function buildSublist(pages, title, opened) {
179179
const getTitle = locale === "en-US"
180180
? page => page.title
181-
: page => page.translations.find(translation => translation.locale === locale)?.title ?? page.title;
181+
: page => page.translations().find(translation => translation.locale === locale)?.title ?? page.title;
182182
183183
const items = pages.map(async aPage => {
184184
const url = aPage.url.replace("en-US", locale);

Diff for: kumascript/macros/ListSubpagesForSidebar.ejs

+1-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ async function createLink(aPage) {
7373
let title = htmlEscape(aPage.title);
7474
7575
if (locale !== 'en-US') {
76-
for (const translation of aPage.translations) {
76+
for (const translation of aPage.translations()) {
7777
if (translation.locale === locale) {
7878
title = htmlEscape(trimTitle(translation.title));
7979
}

Diff for: kumascript/macros/WebExtAPISidebar.ejs

+3-4
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,12 @@ async function buildSublist(pages, title, ignoreBadges) {
3838
apiComponentName = titlePieces[titlePieces.length - 1];
3939
}
4040
41-
if (locale != "en-US") {
42-
aPage.translations.forEach(function (translation) {
41+
if (locale !== "en-US") {
42+
for (const translation of aPage.translations()) {
4343
if (translation.locale === locale) {
44-
url = translation.url;
4544
title = htmlEscape(translation.title);
4645
}
47-
});
46+
}
4847
}
4948
5049
result += "<li>";

Diff for: kumascript/src/info.ts

+8-4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import cheerio from "cheerio";
22

33
import * as Parser from "./parser.js";
44
import { Document, Redirect } from "../../content/index.js";
5+
import { translationsOf } from "../../content/translations.js";
56
import { isValidLocale } from "../../libs/locale-utils/index.js";
67
import { m2hSync } from "../../markdown/index.js";
78
import { findPostFileBySlug, getSlugByBlogPostUrl } from "../../build/utils.js";
@@ -107,7 +108,7 @@ export const info = {
107108
},
108109

109110
// TODO
110-
getTranslations(url) {
111+
getTranslations(url: string) {
111112
// function buildTranslationObjects(data) {
112113
// // Builds a list of translation objects suitable for
113114
// // consumption by Kumascript macros, using the translation
@@ -147,11 +148,11 @@ export const info = {
147148
// }
148149
// return result;
149150
// }
150-
return info.getPageByURL(url, { throwIfDoesNotExist: true }).translations;
151+
return info.getPageByURL(url, { throwIfDoesNotExist: true }).translations();
151152
},
152153

153154
getPageByURL(
154-
url,
155+
url: string,
155156
{ throwIfDoesNotExist = false, followRedirects = true } = {}
156157
) {
157158
// Always start by looking it up *without* following redirects.
@@ -194,7 +195,10 @@ export const info = {
194195
status: status || [],
195196
tags: tags || [],
196197
pageType: document.metadata["page-type"],
197-
translations: [], // TODO Object.freeze(buildTranslationObjects(data)),
198+
// Let translations be lazy loaded.
199+
translations() {
200+
return translationsOf(document.metadata.slug, document.metadata.locale);
201+
},
198202
summary() {
199203
// Back in the old Kuma days we used to store the summary as another piece
200204
// of metadata on each document. It was always available, with any kumascript

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

+8-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,13 @@ import fs from "node:fs";
22
import { JSDOM } from "jsdom";
33
import { jest } from "@jest/globals";
44

5-
import { beforeEachMacro, describeMacro, itMacro, lintHTML } from "./utils.js";
5+
import {
6+
beforeEachMacro,
7+
describeMacro,
8+
itMacro,
9+
lintHTML,
10+
parsePagesFixture,
11+
} from "./utils.js";
612

713
/**
814
* Load all the fixtures.
@@ -12,7 +18,7 @@ const pagesFixturePath = new URL(
1218
"./fixtures/defaultapisidebar/pages.json",
1319
import.meta.url
1420
);
15-
const pagesJSON = JSON.parse(fs.readFileSync(pagesFixturePath, "utf-8"));
21+
const pagesJSON = parsePagesFixture(pagesFixturePath);
1622
const subpagesJSON = [
1723
pagesJSON["/en-US/docs/Web/API/TestInterface_API/MyGuidePage1"],
1824
pagesJSON["/en-US/docs/Web/API/TestInterface_API/MyGuidePage2"],

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

+8-4
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,13 @@ import fs from "node:fs";
33
import { JSDOM } from "jsdom";
44
import { jest } from "@jest/globals";
55

6-
import { beforeEachMacro, describeMacro, itMacro, lintHTML } from "./utils.js";
6+
import {
7+
beforeEachMacro,
8+
describeMacro,
9+
itMacro,
10+
lintHTML,
11+
parsePagesFixture,
12+
} from "./utils.js";
713

814
/**
915
* Load all the fixtures.
@@ -13,9 +19,7 @@ const subpagesFixturePath = new URL(
1319
"./fixtures/apiref/subpages.json",
1420
import.meta.url
1521
);
16-
const subpagesFixture = JSON.parse(
17-
fs.readFileSync(subpagesFixturePath, "utf-8")
18-
);
22+
const subpagesFixture = parsePagesFixture(subpagesFixturePath);
1923
const commonl10nFixturePath = new URL(
2024
"./fixtures/apiref/commonl10n.json",
2125
import.meta.url

Diff for: kumascript/tests/macros/fixtures/apiref/subpages.json

-33
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,6 @@
77
"translations": [
88
{
99
"title": "MyTestStaticProperty1",
10-
"url": "/ja/docs/Web/API/TestInterface/MyTestStaticProperty1",
11-
"tags": [],
12-
"summary": "The <code><strong>MyTestStaticProperty1</strong></code> property of the <a href=\"/ja/docs/Web/API/TestInterface\" title=\"TestInterface summary stuff\"><code>TestInterface</code></a> interface has no badges (ja translation).",
1310
"locale": "ja"
1411
}
1512
],
@@ -27,9 +24,6 @@
2724
"translations": [
2825
{
2926
"title": "MyTestInstanceProperty1",
30-
"url": "/ja/docs/Web/API/TestInterface/MyTestInstanceProperty1",
31-
"tags": [],
32-
"summary": "The <code><strong>MyTestInstanceProperty1</strong></code> property of the <a href=\"/ja/docs/Web/API/TestInterface\" title=\"TestInterface summary stuff\"><code>TestInterface</code></a> interface has no badges (ja translation).",
3327
"locale": "ja"
3428
}
3529
],
@@ -47,9 +41,6 @@
4741
"translations": [
4842
{
4943
"title": "MyTestStaticMethod1",
50-
"url": "/ja/docs/Web/API/TestInterface/MyTestStaticMethod1",
51-
"tags": [],
52-
"summary": "The <code><strong>MyTestStaticMethod1</strong></code> property of the <a href=\"/ja/docs/Web/API/TestInterface\" title=\"TestInterface summary stuff\"><code>TestInterface</code></a> interface is experimental (ja translation).",
5344
"locale": "ja"
5445
}
5546
],
@@ -67,9 +58,6 @@
6758
"translations": [
6859
{
6960
"title": "MyTestStaticMethod2",
70-
"url": "/ja/docs/Web/API/TestInterface/MyTestStaticMethod2",
71-
"tags": [],
72-
"summary": "The <code><strong>MyTestStaticMethod2</strong></code> property of the <a href=\"/ja/docs/Web/API/TestInterface\" title=\"TestInterface summary stuff\"><code>TestInterface</code></a> interface is deprecated and non-standard (ja translation).",
7361
"locale": "ja"
7462
}
7563
],
@@ -87,9 +75,6 @@
8775
"translations": [
8876
{
8977
"title": "MyTestStaticMethod3",
90-
"url": "/ja/docs/Web/API/TestInterface/MyTestStaticMethod3",
91-
"tags": [],
92-
"summary": "The <code><strong>MyTestStaticMethod3</strong></code> property of the <a href=\"/ja/docs/Web/API/TestInterface\" title=\"TestInterface summary stuff\"><code>TestInterface</code></a> interface has all the badges (ja translation).",
9378
"locale": "ja"
9479
}
9580
],
@@ -107,9 +92,6 @@
10792
"translations": [
10893
{
10994
"title": "MyTestInstanceMethod1",
110-
"url": "/ja/docs/Web/API/TestInterface/MyTestInstanceMethod1",
111-
"tags": [],
112-
"summary": "The <code><strong>MyTestInstanceMethod1</strong></code> property of the <a href=\"/ja/docs/Web/API/TestInterface\" title=\"TestInterface summary stuff\"><code>TestInterface</code></a> interface is experimental (ja translation).",
11395
"locale": "ja"
11496
}
11597
],
@@ -127,9 +109,6 @@
127109
"translations": [
128110
{
129111
"title": "MyTestInstanceMethod2",
130-
"url": "/ja/docs/Web/API/TestInterface/MyTestInstanceMethod2",
131-
"tags": [],
132-
"summary": "The <code><strong>MyTestInstanceMethod2</strong></code> property of the <a href=\"/ja/docs/Web/API/TestInterface\" title=\"TestInterface summary stuff\"><code>TestInterface</code></a> interface is deprecated and non-standard (ja translation).",
133112
"locale": "ja"
134113
}
135114
],
@@ -147,9 +126,6 @@
147126
"translations": [
148127
{
149128
"title": "MyTestInstanceMethod3",
150-
"url": "/ja/docs/Web/API/TestInterface/MyTestInstanceMethod3",
151-
"tags": [],
152-
"summary": "The <code><strong>MyTestInstanceMethod3</strong></code> property of the <a href=\"/ja/docs/Web/API/TestInterface\" title=\"TestInterface summary stuff\"><code>TestInterface</code></a> interface has all the badges (ja translation).",
153129
"locale": "ja"
154130
}
155131
],
@@ -167,9 +143,6 @@
167143
"translations": [
168144
{
169145
"title": "MyTestEvent1",
170-
"url": "/ja/docs/Web/API/TestInterface/TestEvent1",
171-
"tags": [],
172-
"summary": "The <code><strong>MyTestEvent1</strong></code> event of the <a href=\"/ja/docs/Web/API/TestInterface\" title=\"TestInterface summary stuff\"><code>TestInterface</code></a> interface has no badges (ja translation).",
173146
"locale": "ja"
174147
}
175148
],
@@ -187,9 +160,6 @@
187160
"translations": [
188161
{
189162
"title": "MyTestEvent2",
190-
"url": "/ja/docs/Web/API/TestInterface/TestEvent2",
191-
"tags": [],
192-
"summary": "The <code><strong>MyTestEvent2</strong></code> event of the <a href=\"/ja/docs/Web/API/TestInterface\" title=\"TestInterface summary stuff\"><code>TestInterface</code></a> interface is deprecated and non-standard (ja translation).",
193163
"locale": "ja"
194164
}
195165
],
@@ -207,9 +177,6 @@
207177
"translations": [
208178
{
209179
"title": "MyTestEvent3",
210-
"url": "/ja/docs/Web/API/TestInterface/TestEvent3",
211-
"tags": [],
212-
"summary": "The <code><strong>MyTestEvent3</strong></code> event of the <a href=\"/ja/docs/Web/API/TestInterface\" title=\"TestInterface summary stuff\"><code>TestInterface</code></a> interface has no badges (ja translation).",
213180
"locale": "ja"
214181
}
215182
],

Diff for: kumascript/tests/macros/utils.ts

+18
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { HtmlValidate } from "html-validate";
44

55
import Environment from "../../src/environment.js";
66
import Templates from "../../src/templates.js";
7+
import fs from "node:fs";
78

89
// When we were doing mocha testing, we used this.macro to hold this.
910
// But Jest doesn't use the this object, so we just store the object here.
@@ -154,6 +155,23 @@ export function afterEachMacro(teardown) {
154155
});
155156
}
156157

158+
/**
159+
* This function parses a JSON file containing pages fixture data.
160+
* It returns the parsed object, with the "translations" property
161+
* replaced by a function that returns the translations object.
162+
*/
163+
export function parsePagesFixture(file: URL) {
164+
return JSON.parse(
165+
fs.readFileSync(fileURLToPath(file), { encoding: "utf-8" }),
166+
(key, value) => {
167+
if (key === "translations") {
168+
return () => value;
169+
}
170+
return value;
171+
}
172+
);
173+
}
174+
157175
/**
158176
* This function validates its input as HTML. By default, it assumes the input
159177
* is an HTML fragment, wrapping it to make a complete HTML document, but the

0 commit comments

Comments
 (0)