Skip to content

Commit

Permalink
Account for partial string matches in fetchLinkSuggestions (WordPress…
Browse files Browse the repository at this point in the history
…#62570)

Co-authored-by: noisysocks <[email protected]>
Co-authored-by: andrewserong <[email protected]>
  • Loading branch information
3 people authored Jun 17, 2024
1 parent aa6b6f0 commit 4413591
Showing 1 changed file with 5 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -264,14 +264,16 @@ export default async function fetchLinkSuggestions(
* @param search
*/
export function sortResults( results: SearchResult[], search: string ) {
const searchTokens = new Set( tokenize( search ) );
const searchTokens = tokenize( search );

const scores = {};
for ( const result of results ) {
if ( result.title ) {
const titleTokens = tokenize( result.title );
const matchingTokens = titleTokens.filter( ( token ) =>
searchTokens.has( token )
const matchingTokens = titleTokens.filter( ( titleToken ) =>
searchTokens.some( ( searchToken ) =>
titleToken.includes( searchToken )
)
);
scores[ result.id ] = matchingTokens.length / titleTokens.length;
} else {
Expand Down

0 comments on commit 4413591

Please sign in to comment.