From 8460962f48d7820b09bcaebd3d554f35abeac59e Mon Sep 17 00:00:00 2001 From: Claas Augner Date: Mon, 9 Dec 2024 10:43:34 +0100 Subject: [PATCH 1/4] fix(scripts/reorder-search-index): add all reference items Avoids `null` values. --- scripts/reorder-search-index.mjs | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/scripts/reorder-search-index.mjs b/scripts/reorder-search-index.mjs index 33fef2c1819e..4bfffb4811e5 100644 --- a/scripts/reorder-search-index.mjs +++ b/scripts/reorder-search-index.mjs @@ -7,19 +7,29 @@ async function main() { const getSlug = ({ url }) => url.replace(/^\/[^/]+\/docs\//, ""); // Read reference (e.g. "client/build/en-us/search-index.json"). - const ref = readJson(refPath).map(getSlug); + const ref = readJson(refPath); + const refSlugs = ref.map(getSlug); // Read index (e.g. "client/build/de/search-index.json"). const input = readJson(inputPath); - - const getIndex = (slug) => ref.indexOf(slug); + const inputSlugs = input.map(getSlug); const result = []; - for (const [fromIndex, toIndex] of input - .map(getSlug) - .map(getIndex) - .entries()) { - result[toIndex] = input[fromIndex]; + + // Add all reference items that are in the reference. + for (const [refIndex, slug] of refSlugs.entries()) { + const inputIndex = inputSlugs.indexOf(slug); + // Use reference item where index does not have this item. + const item = inputIndex !== -1 ? input[inputIndex] : ref[refIndex]; + result.push(item); + } + + // Add items that are NOT in the reference (e.g. moved/removed). + for (const [inputIndex, slug] of inputSlugs.entries()) { + if (!refSlugs.includes(slug)) { + const item = input[inputIndex]; + result.push(item); + } } writeFileSync(outputPath ?? inputPath, JSON.stringify(result), "utf-8"); From 7acae53454f8ff14da709e1671ab6b0220911b9b Mon Sep 17 00:00:00 2001 From: Claas Augner Date: Mon, 9 Dec 2024 10:46:27 +0100 Subject: [PATCH 2/4] fix(workflows/stage-build): remove duplicate script call --- .github/workflows/stage-build.yml | 3 --- 1 file changed, 3 deletions(-) diff --git a/.github/workflows/stage-build.yml b/.github/workflows/stage-build.yml index ce8d912a774e..84cd1bb92435 100644 --- a/.github/workflows/stage-build.yml +++ b/.github/workflows/stage-build.yml @@ -320,9 +320,6 @@ jobs: yarn rari build --all --issues client/build/issues.json --templ-stats - # Sort DE search index by en-US popularity. - node scripts/reorder-search-index.mjs client/build/en-us/search-index.json client/build/de/search-index.json - # SSR all pages yarn render:html From aec80a68c87dda435715ca7e86acd001b398946e Mon Sep 17 00:00:00 2001 From: Claas Augner <495429+caugner@users.noreply.github.com> Date: Mon, 9 Dec 2024 12:22:55 +0100 Subject: [PATCH 3/4] chore(scripts/reorder-search-index): refine comments --- scripts/reorder-search-index.mjs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/scripts/reorder-search-index.mjs b/scripts/reorder-search-index.mjs index 4bfffb4811e5..593ab7a83c72 100644 --- a/scripts/reorder-search-index.mjs +++ b/scripts/reorder-search-index.mjs @@ -16,15 +16,15 @@ async function main() { const result = []; - // Add all reference items that are in the reference. + // Add entry for each reference item. for (const [refIndex, slug] of refSlugs.entries()) { const inputIndex = inputSlugs.indexOf(slug); - // Use reference item where index does not have this item. + // Use reference item if it's currently missing in index. const item = inputIndex !== -1 ? input[inputIndex] : ref[refIndex]; result.push(item); } - // Add items that are NOT in the reference (e.g. moved/removed). + // Add entry for any item that is NOT in the reference (e.g. moved/removed). for (const [inputIndex, slug] of inputSlugs.entries()) { if (!refSlugs.includes(slug)) { const item = input[inputIndex]; From 27f5819974005c3fa2e3e27b02693e006beb493a Mon Sep 17 00:00:00 2001 From: Florian Dieminger Date: Tue, 17 Dec 2024 12:08:49 +0100 Subject: [PATCH 4/4] enhance performance and remove en-us fallback --- scripts/reorder-search-index.mjs | 35 +++++++++++++------------------- 1 file changed, 14 insertions(+), 21 deletions(-) diff --git a/scripts/reorder-search-index.mjs b/scripts/reorder-search-index.mjs index 593ab7a83c72..291c9628863c 100644 --- a/scripts/reorder-search-index.mjs +++ b/scripts/reorder-search-index.mjs @@ -4,33 +4,26 @@ async function main() { const [refPath, inputPath, outputPath = null] = process.argv.slice(2); const readJson = (path) => JSON.parse(readFileSync(path, "utf-8")); - const getSlug = ({ url }) => url.replace(/^\/[^/]+\/docs\//, ""); + const slugify = (url) => url.replace(/^\/[^/]+\/docs\//, ""); - // Read reference (e.g. "client/build/en-us/search-index.json"). - const ref = readJson(refPath); - const refSlugs = ref.map(getSlug); + // Read reference (e.g. "client/build/en-us/search-index.json") + // into map: slug -> index-in-ref + const ref = Object.fromEntries( + readJson(refPath).map(({ url }, i) => [slugify(url), i]) + ); // Read index (e.g. "client/build/de/search-index.json"). const input = readJson(inputPath); - const inputSlugs = input.map(getSlug); - const result = []; + // Array of tuples (index-in-ref, input-entry). + const indexed = input.map(({ title, url }) => [ + ref[slugify(url)] ?? Infinity, + { title, url }, + ]); + // Sort by index-in-ref. + indexed.sort(([a], [b]) => (a < b ? -1 : a > b ? 1 : 0)); - // Add entry for each reference item. - for (const [refIndex, slug] of refSlugs.entries()) { - const inputIndex = inputSlugs.indexOf(slug); - // Use reference item if it's currently missing in index. - const item = inputIndex !== -1 ? input[inputIndex] : ref[refIndex]; - result.push(item); - } - - // Add entry for any item that is NOT in the reference (e.g. moved/removed). - for (const [inputIndex, slug] of inputSlugs.entries()) { - if (!refSlugs.includes(slug)) { - const item = input[inputIndex]; - result.push(item); - } - } + const result = indexed.map(([, entry]) => entry); writeFileSync(outputPath ?? inputPath, JSON.stringify(result), "utf-8"); }