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");