Skip to content

Commit

Permalink
Remove disambiguation pages from search results (#107)
Browse files Browse the repository at this point in the history
Co-authored-by: Sbisson <[email protected]>
  • Loading branch information
stephanebisson and Sbisson authored Feb 14, 2024
1 parent 9436530 commit 9f4d220
Showing 1 changed file with 35 additions and 45 deletions.
80 changes: 35 additions & 45 deletions src/link/api.js
Original file line number Diff line number Diff line change
@@ -1,35 +1,49 @@
let abortFunctions = [];

export const prefixSearch = ( lang, term, callback ) => {
const url = `https://${ lang }.wikipedia.org/w/rest.php/v1/search/title?q=${ term.trim() }&limit=5`;
return request( url, ( data ) => {
if ( ! data.pages ) {
const handler = ( callback ) => {
return ( data ) => {
if ( ! data.query || ! data.query.pages ) {
callback( [] );
} else {
callback(
Object.values( data.pages ).map( ( page ) => {
return {
title: page.title,
description: page.description,
thumbnail: page.thumbnail?.url,
};
} )
Object.values( data.query.pages )
.filter( ( page ) => {
return ! page.pageprops ||
! page.pageprops.hasOwnProperty( 'disambiguation' );
} )
.map( ( page ) => {
return {
title: page.title,
description: page.description,
thumbnail: page.thumbnail?.source,
};
} )
);
}
} );
};
};

export const prefixSearch = ( lang, term, callback ) => {
const params = {
action: 'query',
prop: 'pageimages|pageprops|description',
piprop: 'thumbnail',
pithumbsize: 64,
pilimit: 5,
generator: 'prefixsearch',
gpssearch: term,
gpsnamespace: 0,
gpslimit: 5,
};

const url = buildMwApiUrl( lang, params );
return request( url, handler( callback ) );
};

export const fulltextSearch = ( lang, term, callback ) => {
const params = {
action: 'query',
list: 'search',
srprop: 'snippet',
srsearch: term,
srnamespace: 0,
srlimit: 5,
srenablerewrites: true,
srinfo: 'rewrittenquery',
prop: 'pageimages',
prop: 'pageimages|pageprops|description',
piprop: 'thumbnail',
pithumbsize: 64,
pilimit: 5,
Expand All @@ -40,25 +54,7 @@ export const fulltextSearch = ( lang, term, callback ) => {
};

const url = buildMwApiUrl( lang, params );
return request( url, ( data ) => {
if ( ! data.query?.search ) {
callback( [] );
} else {
const { search, pages } = data.query;
callback(
Object.values( search ).map( ( item ) => {
const page = pages?.find(
( { pageid } ) => pageid === item.pageid
);
return {
title: item.title,
description: stripHtml( item.snippet ),
thumbnail: page?.thumbnail?.source,
};
} )
);
}
} );
return request( url, handler( callback ) );
};

export const abortAllRequest = () => {
Expand Down Expand Up @@ -101,9 +97,3 @@ const request = ( url, callback ) => {

abortFunctions.push( xhr );
};

const stripHtml = ( html ) => {
const tmp = document.createElement( 'DIV' );
tmp.innerHTML = html;
return tmp.textContent || tmp.innerText || '';
};

0 comments on commit 9f4d220

Please sign in to comment.