Skip to content

Commit 680d8f8

Browse files
authored
Docs: Improved search with category matching. (#32164)
1 parent 50f79bb commit 680d8f8

File tree

2 files changed

+16
-6
lines changed

2 files changed

+16
-6
lines changed

docs/index.html

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2029,7 +2029,9 @@ <h2>Global</h2>
20292029
}
20302030

20312031
const regExp = new RegExp( escapeRegExp( v ), 'gi' );
2032-
const highlightRegExp = new RegExp( v.replace( /[.*+?^${}()|[\]\\]/g, '\\$&' ), 'gi' );
2032+
// Create highlight regex that matches any of the search words
2033+
const words = v.split( ' ' ).map( word => word.replace( /[.*+?^${}()|[\]\\]/g, '\\$&' ) ).join( '|' );
2034+
const highlightRegExp = new RegExp( words, 'gi' );
20332035

20342036
// Search through all categories
20352037
const results = [];
@@ -2038,7 +2040,9 @@ <h2>Global</h2>
20382040
const items = searchData[ category ];
20392041
for ( const item of items ) {
20402042

2041-
if ( item.title.match( regExp ) ) {
2043+
// Match against combined category and title for multi-word searches
2044+
const searchText = category + ' ' + item.title;
2045+
if ( searchText.match( regExp ) ) {
20422046

20432047
results.push( { ...item, category } );
20442048

@@ -2128,7 +2132,8 @@ <h2>Global</h2>
21282132

21292133
if ( ! byCategory[ category ] ) continue;
21302134

2131-
html += `<h2>${category}</h2>`;
2135+
const highlightedCategory = highlightMatch( category, highlightRegExp );
2136+
html += `<h2>${highlightedCategory}</h2>`;
21322137

21332138
for ( const className in byCategory[ category ] ) {
21342139

utils/docs/template/static/index.html

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,9 @@ <h1><a href="https://threejs.org">three.js</a></h1>
315315
}
316316

317317
const regExp = new RegExp( escapeRegExp( v ), 'gi' );
318-
const highlightRegExp = new RegExp( v.replace( /[.*+?^${}()|[\]\\]/g, '\\$&' ), 'gi' );
318+
// Create highlight regex that matches any of the search words
319+
const words = v.split( ' ' ).map( word => word.replace( /[.*+?^${}()|[\]\\]/g, '\\$&' ) ).join( '|' );
320+
const highlightRegExp = new RegExp( words, 'gi' );
319321

320322
// Search through all categories
321323
const results = [];
@@ -324,7 +326,9 @@ <h1><a href="https://threejs.org">three.js</a></h1>
324326
const items = searchData[ category ];
325327
for ( const item of items ) {
326328

327-
if ( item.title.match( regExp ) ) {
329+
// Match against combined category and title for multi-word searches
330+
const searchText = category + ' ' + item.title;
331+
if ( searchText.match( regExp ) ) {
328332

329333
results.push( { ...item, category } );
330334

@@ -414,7 +418,8 @@ <h1><a href="https://threejs.org">three.js</a></h1>
414418

415419
if ( ! byCategory[ category ] ) continue;
416420

417-
html += `<h2>${category}</h2>`;
421+
const highlightedCategory = highlightMatch( category, highlightRegExp );
422+
html += `<h2>${highlightedCategory}</h2>`;
418423

419424
for ( const className in byCategory[ category ] ) {
420425

0 commit comments

Comments
 (0)