Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(scripts/reorder-search-index): no null values #12270

Merged
merged 5 commits into from
Dec 17, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
26 changes: 18 additions & 8 deletions scripts/reorder-search-index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
caugner marked this conversation as resolved.
Show resolved Hide resolved
for (const [refIndex, slug] of refSlugs.entries()) {
const inputIndex = inputSlugs.indexOf(slug);
// Use reference item where index does not have this item.
caugner marked this conversation as resolved.
Show resolved Hide resolved
const item = inputIndex !== -1 ? input[inputIndex] : ref[refIndex];
result.push(item);
}

// Add items that are NOT in the reference (e.g. moved/removed).
caugner marked this conversation as resolved.
Show resolved Hide resolved
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");
Expand Down
Loading