Skip to content

Commit

Permalink
Compat the new style of search-index.js format. Fixes #106
Browse files Browse the repository at this point in the history
librustdoc has switched the search-index.js from a "array of struct" to a "struct of array" format.We need to compat both the new and old formats.
  • Loading branch information
Folyd committed Mar 20, 2021
1 parent 4f0be20 commit d56d7f3
Showing 1 changed file with 102 additions and 40 deletions.
142 changes: 102 additions & 40 deletions extension/search/docs/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,49 +83,111 @@ class DocSearch {
type: null,
});

// an array of [(Number) item type,
// (String) name,
// (String) full path or empty string for previous path,
// (String) description,
// (Number | null) the parent path index to `paths`]
// (Object | null) the type of the function (if any)
// Compat old style (items, paths) and new style (i, p)
var items = rawSearchIndex[crate].items || rawSearchIndex[crate].i;
// an array of [(Number) item type,
// (String) name]
var paths = rawSearchIndex[crate].paths || rawSearchIndex[crate].p;

// convert `paths` into an object form
for (var i = 0; i < paths.length; ++i) {
if (Array.isArray(paths[i])) {
paths[i] = {ty: paths[i][0], name: paths[i][1]};
// https://github.com/rust-lang/rust/pull/83003
// librustdoc has switched the search-index.js from a "array of struct" to a "struct of array" format.
// We need to compat both the new and old formats.
if (["t", "n", "q", "d", "i", "f", "p"].every( key => key in rawSearchIndex[crate])) {
// an array of (Number) item types
var itemTypes = rawSearchIndex[crate].t;
// an array of (String) item names
var itemNames = rawSearchIndex[crate].n;
// an array of (String) full paths (or empty string for previous path)
var itemPaths = rawSearchIndex[crate].q;
// an array of (String) descriptions
var itemDescs = rawSearchIndex[crate].d;
// an array of (Number) the parent path index + 1 to `paths`, or 0 if none
var itemParentIdxs = rawSearchIndex[crate].i;
// an array of (Object | null) the type of the function, if any
var itemFunctionSearchTypes = rawSearchIndex[crate].f;
// an array of [(Number) item type,
// (String) name]
var paths = rawSearchIndex[crate].p;

// convert `paths` into an object form
for (var i = 0; i < paths.length; ++i) {
if (Array.isArray(paths[i])) {
paths[i] = {ty: paths[i][0], name: paths[i][1]};
}
}
}

// convert `items` into an object form, and construct word indices.
//
// before any analysis is performed lets gather the search terms to
// search against apart from the rest of the data. This is a quick
// operation that is cached for the life of the page state so that
// all other search operations have access to this cached data for
// faster analysis operations
var len = items.length;
var lastPath = "";
for (var i = 0; i < len; ++i) {
var rawRow = items[i];
var row = {
crate: crate, ty: rawRow[0], name: rawRow[1],
path: rawRow[2] || lastPath, desc: rawRow[3],
parent: paths[rawRow[4]], type: rawRow[5]
};
searchIndex.push(row);
if (typeof row.name === "string") {
var word = row.name.toLowerCase();
searchWords.push(word);
} else {
searchWords.push("");
// convert `item*` into an object form, and construct word indices.
//
// before any analysis is performed lets gather the search terms to
// search against apart from the rest of the data. This is a quick
// operation that is cached for the life of the page state so that
// all other search operations have access to this cached data for
// faster analysis operations
var len = itemTypes.length;
var lastPath = "";
for (i = 0; i < len; ++i) {
var row = {
crate: crate,
ty: itemTypes[i],
name: itemNames[i],
path: itemPaths[i] ? itemPaths[i] : lastPath,
desc: itemDescs[i],
parent: itemParentIdxs[i] > 0 ? paths[itemParentIdxs[i] - 1] : undefined,
type: itemFunctionSearchTypes[i],
};
searchIndex.push(row);
if (typeof row.name === "string") {
var word = row.name.toLowerCase();
searchWords.push(word);
} else {
searchWords.push("");
}
lastPath = row.path;
}
} else {
// an array of [(Number) item type,
// (String) name,
// (String) full path or empty string for previous path,
// (String) description,
// (Number | null) the parent path index to `paths`]
// (Object | null) the type of the function (if any)
// Compat old style (items, paths) and new style (i, p)
var items = rawSearchIndex[crate].items || rawSearchIndex[crate].i;

// an array of [(Number) item type,
// (String) name]
var paths = rawSearchIndex[crate].paths || rawSearchIndex[crate].p;

// convert `paths` into an object form
for (var i = 0; i < paths.length; ++i) {
if (Array.isArray(paths[i])) {
paths[i] = {ty: paths[i][0], name: paths[i][1]};
}
}

// convert `items` into an object form, and construct word indices.
//
// before any analysis is performed lets gather the search terms to
// search against apart from the rest of the data. This is a quick
// operation that is cached for the life of the page state so that
// all other search operations have access to this cached data for
// faster analysis operations
var len = items.length;
var lastPath = "";
for (i = 0; i < len; ++i) {
var rawRow = items[i];
var row = {
crate: crate,
ty: rawRow[0],
name: rawRow[1],
path: rawRow[2] || lastPath,
desc: rawRow[3],
parent: paths[rawRow[4]],
type: rawRow[5]
};
searchIndex.push(row);
if (typeof row.name === "string") {
var word = row.name.toLowerCase();
searchWords.push(word);
} else {
searchWords.push("");
}
lastPath = row.path;
}
lastPath = row.path;
}
}
this.searchWords = searchWords;
Expand Down

0 comments on commit d56d7f3

Please sign in to comment.