Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
70 changes: 70 additions & 0 deletions src/documentation/search/search.worker.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,76 @@ describe("buildReferenceIndex", () => {
});
});

describe("buildApiIndex", () => {
const reference: Toolkit = {
id: "reference",
description: "Reference stuff",
name: "Reference",
contents: [],
language: "en",
};
const api: ApiDocsResponse = {
"microbit.display": {
id: "microbit.display",
name: "display",
fullName: "microbit.display",
kind: "module",
docString: "Control the LED display.",
children: [
{
id: "microbit.display.delay",
name: "delay",
fullName: "microbit.display.delay",
kind: "variable",
docString: "A timing value.",
},
{
id: "microbit.display.show",
name: "show",
fullName: "microbit.display.show",
kind: "function",
params: [
{ name: "image", category: "simple" },
{ name: "loop", category: "simple", defaultValue: "False" },
],
docString:
"Shows an image.\n\nExample: `display.show(Image.HEART)`\n\n:param image: The image to show.\n:param delay: The time between frames.\nThe animation can happen in the background.",
},
],
},
};

it("finds API entries by parameter documentation", async () => {
const search = await buildIndex(reference, api, undefined, undefined);

const results = search.search("background").api;

expect(results).toHaveLength(1);
expect(results[0].id).toEqual("microbit.display.show");
expect(results[0].extract.content).toContainEqual({
extract: "background.",
type: "match",
});
});

it("finds API entries by parameter name", async () => {
const search = await buildIndex(reference, api, undefined, undefined);

expect(search.search("loop").api.map((result) => result.id)).toContain(
"microbit.display.show"
);
});

it("ranks an exact API name above a parameter documentation match", async () => {
const search = await buildIndex(reference, api, undefined, undefined);

expect(search.search("delay").api.map((result) => result.id)).toEqual([
"microbit.display.delay",
"microbit.display.show",
]);
});
});

describe("SearchWorker", () => {
it("blocks queries on initialization", async () => {
const postMessage = vi.fn();
Expand Down
60 changes: 47 additions & 13 deletions src/documentation/search/search.worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ const getExtracts = (
content: SearchableContent
): Extracts => {
const allContentPositions: Position[] = [];
const allDetailPositions: Position[] = [];
const allTitlePositions: Position[] = [];

for (const match of Object.values(matchMetadata)) {
Expand All @@ -136,11 +137,21 @@ const getExtracts = (
allContentPositions.push(p);
});
}
if (match.detail) {
match.detail.position.forEach((p) => {
allDetailPositions.push(p);
});
}
}

const detailOnlyMatch =
allContentPositions.length === 0 && allDetailPositions.length > 0;

return {
title: fullStringExtracts(allTitlePositions, content.title),
content: contextExtracts(allContentPositions, content.content),
content: detailOnlyMatch
? contextExtracts(allDetailPositions, content.detail ?? "")
: contextExtracts(allContentPositions, content.content),
};
};

Expand All @@ -163,6 +174,12 @@ export interface SearchableContent {
containerTitle: string;
title: string;
content: string;
/**
* Lower-priority content that should be searchable but not outrank names or
* summaries. For API entries this contains parameter names and the detailed
* part of the docstring.
*/
detail?: string;
}

const defaultString = (string: string | undefined): string => {
Expand Down Expand Up @@ -203,27 +220,43 @@ const apiSearchableContent = (
toolkit: ApiDocsResponse
): SearchableContent[] => {
const content: SearchableContent[] = [];
const searchableEntry = (
entry: ApiDocsEntry,
title: string,
containerTitle: string
): SearchableContent => {
const { summary, remainder } = splitDocString(
defaultString(entry.docString)
);
const parameterNames = entry.params
?.filter((parameter) => parameter.name && parameter.name !== "self")
.map((parameter) => parameter.name)
.join(" ");
return {
id: entry.id,
title,
containerTitle,
content: summary,
detail: [parameterNames, remainder].filter(Boolean).join("\n\n"),
};
};
const addNestedDocs = (
moduleName: string,
entries: ApiDocsEntry[] | undefined
): void => {
entries?.forEach((c) => {
content.push({
id: c.id,
title: c.fullName.substring(moduleName.length + 1),
containerTitle: moduleName,
content: splitDocString(defaultString(c.docString)).summary,
});
content.push(
searchableEntry(
c,
c.fullName.substring(moduleName.length + 1),
moduleName
)
);
addNestedDocs(moduleName, c.children);
});
};
for (const module of Object.values(toolkit)) {
content.push({
id: module.id,
title: module.fullName,
containerTitle: module.fullName,
content: splitDocString(defaultString(module.docString)).summary,
});
content.push(searchableEntry(module, module.fullName, module.fullName));
addNestedDocs(module.fullName, module.children);
}
return content;
Expand Down Expand Up @@ -256,6 +289,7 @@ export const buildSearchIndex = (
});
this.field("title", { boost: 10 });
this.field("content");
this.field("detail", { boost: 0.25 });
this.use(languagePlugin);
plugins.forEach((p) => this.use(p));

Expand Down