From f2d2abe054feca91b89c00c33e1d726bbda85dcb Mon Sep 17 00:00:00 2001 From: Michael Yoder Date: Fri, 15 Sep 2023 14:45:44 -0400 Subject: [PATCH] Handling for multi word searches Split searches on space and wraps each part in a wildcard. This replaces the current current behavior of wrapping the entire search query in a wildcard. Current behavior: * "create" becomes "*create*" * "create item" becomes "*create item*" New behavior: * "create" becomes "*create*" * "create item" becomes "*create* *item*" --- .../assets/typedoc/components/Search.ts | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/src/lib/output/themes/default/assets/typedoc/components/Search.ts b/src/lib/output/themes/default/assets/typedoc/components/Search.ts index 7ea4910fe..9e7f282fb 100644 --- a/src/lib/output/themes/default/assets/typedoc/components/Search.ts +++ b/src/lib/output/themes/default/assets/typedoc/components/Search.ts @@ -157,9 +157,22 @@ function updateResults( const searchText = query.value.trim(); // Perform a wildcard search - // Set empty `res` to prevent getting random results with wildcard search - // when the `searchText` is empty. - let res = searchText ? state.index.search(`*${searchText}*`) : []; + let res: Index.Result[]; + if (searchText) { + // Create a wildcard out of space-separated words in the query, + // ignoring any extra spaces + const searchWithWildcards = searchText + .split(" ") + .map((x) => { + return x.length ? `*${x}*` : ""; + }) + .join(" "); + res = state.index.search(searchWithWildcards); + } else { + // Set empty `res` to prevent getting random results with wildcard search + // when the `searchText` is empty. + res = []; + } for (let i = 0; i < res.length; i++) { const item = res[i];