Skip to content

Commit c297754

Browse files
fix: fallback to English remix metadata for localized examples
Co-authored-by: pgzcoa <patriciaguerrero01@gmail.com>
1 parent 2738278 commit c297754

3 files changed

Lines changed: 59 additions & 4 deletions

File tree

src/layouts/ExampleLayout.astro

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import EditableSketch from "@components/EditableSketch/index.astro";
1212
import RelatedItems from "@components/RelatedItems/index.astro";
1313
import OutdatedTranslationBanner from "@components/OutdatedTranslationBanner/index.astro";
1414
import { checkTranslationBanner } from "../utils/translationBanner";
15+
import { getFallbackRemixData } from "../pages/_utils";
1516
1617
interface Props {
1718
example: CollectionEntry<"examples">;
@@ -45,8 +46,15 @@ const relatedReferences =
4546
4647
const { Content } = await example.render();
4748
49+
// Use the fallback function to retrieve English remix data if the current locale doesn't have any
50+
let remixData = (await getFallbackRemixData(
51+
example.id,
52+
currentLocale,
53+
example.data.remix
54+
)) as typeof example.data.remix;
55+
4856
// Extract the collective attribution year. If multiple provided, uses last shown.
49-
const collectivelyAttributedSince = example.data.remix?.reduce(
57+
const collectivelyAttributedSince = remixData?.reduce(
5058
(acc: number | null, item) => {
5159
if (item.collectivelyAttributedSince) {
5260
return item.collectivelyAttributedSince;
@@ -57,7 +65,7 @@ const collectivelyAttributedSince = example.data.remix?.reduce(
5765
);
5866
5967
// Boolean value on whether the remix history contains links to code
60-
const remixHistoryHasCodeLinks = example.data.remix?.some(
68+
const remixHistoryHasCodeLinks = remixData?.some(
6169
(item) => Array.isArray(item.code) && item.code.length > 0
6270
);
6371
@@ -97,7 +105,7 @@ const { showBanner, englishUrl } = checkTranslationBanner(
97105

98106
<a href={Astro.url.pathname}>{example.data.title}</a>:{" "}
99107

100-
{example.data.remix?.map((item, i) => {
108+
{remixData?.map((item, i) => {
101109
const parts = [];
102110

103111
// Each remix entry requires at least one attribution
@@ -146,7 +154,7 @@ const { showBanner, englishUrl } = checkTranslationBanner(
146154
{remixHistoryHasCodeLinks ? (
147155
<>
148156
{t("attribution", "You can find the code history of these examples here")}{": "}
149-
{example.data.remix
157+
{remixData
150158
.map(item => item?.code)
151159
.flat()
152160
.filter(codeItem => codeItem && codeItem.URL)

src/pages/_utils.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -457,3 +457,37 @@ const getUrl = (
457457
return "";
458458
}
459459
};
460+
461+
/**
462+
* Retrieves fallback remix (attribution/code history) data from the English example
463+
* if the current localized example is missing it.
464+
*
465+
* @param currentId The id of the current example
466+
* @param currentLocale The current locale string
467+
* @param currentRemixData The remix data from the current locale (if any)
468+
* @returns An array of remix data
469+
*/
470+
export const getFallbackRemixData = async (
471+
currentId: string,
472+
currentLocale: string,
473+
currentRemixData: any[] | undefined,
474+
) => {
475+
// Return early if data already exists or if we are already on the English page
476+
if (currentRemixData && currentRemixData.length > 0) {
477+
return currentRemixData;
478+
}
479+
if (currentLocale === "en") {
480+
return currentRemixData;
481+
}
482+
// Main logic
483+
// replace the core path with the English path to find the corresponding English example
484+
// e.g., "zh-Hans/02_Animation_And_Variables/00_Drawing_Lines/description.mdx"
485+
// -> "en/02_Animation_And_Variables/00_Drawing_Lines/description.mdx"
486+
const englishId = currentId.replace(`${currentLocale}/`, "en/");
487+
const allExamples = await getCollection("examples");
488+
const englishExample = allExamples.find((e) => e.id === englishId);
489+
if (englishExample?.data.remix && englishExample.data.remix.length > 0) {
490+
return englishExample.data.remix;
491+
}
492+
return currentRemixData;
493+
}

test/pages/_utils.test.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
removeContentFileExt,
55
removeLeadingSlash,
66
removeLocaleAndExtension,
7+
getFallbackRemixData,
78
} from "@pages/_utils";
89

910
suite("exampleContentSlugToLegacyWebsiteSlug", () => {
@@ -50,3 +51,15 @@ suite("removeLocaleAndExtensionFromId", () => {
5051
);
5152
});
5253
});
54+
55+
suite("getFallbackRemixData", () => {
56+
test("returns remix data for English example when current locale example has no remix data", () => {
57+
expect(
58+
getFallbackRemixData(
59+
"zh-Hans/02_Animation_And_Variables/00_Drawing_Lines/description.mdx",
60+
"zh-Hans",
61+
undefined
62+
)
63+
).toBeDefined();
64+
});
65+
});

0 commit comments

Comments
 (0)