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 diff --git a/scripts/reorder-search-index.mjs b/scripts/reorder-search-index.mjs index 33fef2c1819e..291c9628863c 100644 --- a/scripts/reorder-search-index.mjs +++ b/scripts/reorder-search-index.mjs @@ -4,23 +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).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 getIndex = (slug) => ref.indexOf(slug); + // 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)); - const result = []; - for (const [fromIndex, toIndex] of input - .map(getSlug) - .map(getIndex) - .entries()) { - result[toIndex] = input[fromIndex]; - } + const result = indexed.map(([, entry]) => entry); writeFileSync(outputPath ?? inputPath, JSON.stringify(result), "utf-8"); }