Skip to content

Commit

Permalink
fix(scripts/reorder-search-index): add all reference items
Browse files Browse the repository at this point in the history
Avoids `null` values.
  • Loading branch information
caugner committed Dec 9, 2024
1 parent 5f63c83 commit 8460962
Showing 1 changed file with 18 additions and 8 deletions.
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.
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");
Expand Down

0 comments on commit 8460962

Please sign in to comment.