Skip to content

Commit

Permalink
fix(scripts/reorder-search-index): no null values (mdn#12270)
Browse files Browse the repository at this point in the history
* fix(workflows/stage-build): remove duplicate script call
* enhance performance and remove en-us fallback

---------

Co-authored-by: Florian Dieminger <me@fiji-flo.de>
  • Loading branch information
2 people authored and pepelsbey committed Dec 21, 2024
1 parent f2e1ae3 commit fbb2088
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 14 deletions.
3 changes: 0 additions & 3 deletions .github/workflows/stage-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
25 changes: 14 additions & 11 deletions scripts/reorder-search-index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
Expand Down

0 comments on commit fbb2088

Please sign in to comment.