2022-02-01 20:52:16 +00:00
|
|
|
/**
|
2023-04-12 22:37:36 +00:00
|
|
|
* @typedef {Record<string,string>} UrlParams
|
2022-02-01 20:52:16 +00:00
|
|
|
* @param {string} title
|
|
|
|
* @param {string} fulltext
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @callback generateUrl
|
|
|
|
* @param {RestResult|SearchResult|string} searchResult
|
|
|
|
* @param {UrlParams} [params]
|
|
|
|
* @param {string} [articlePath]
|
|
|
|
* @return {string}
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @typedef {Object} UrlGenerator
|
|
|
|
* @property {generateUrl} generateUrl
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generates URLs for suggestions like those in MediaWiki's mediawiki.searchSuggest implementation.
|
|
|
|
*
|
|
|
|
* @param {MwMap} config
|
|
|
|
* @return {UrlGenerator}
|
|
|
|
*/
|
|
|
|
function urlGenerator( config ) {
|
|
|
|
// TODO: This is a placeholder for enabling customization of the URL generator.
|
|
|
|
// wgVectorSearchUrlGenerator has not been defined as a config variable yet.
|
2022-09-30 16:25:35 +00:00
|
|
|
return config.get( 'wgVectorSearchUrlGenerator', {
|
2022-02-01 20:52:16 +00:00
|
|
|
/**
|
2023-03-28 18:45:35 +00:00
|
|
|
* @param {RestResult|SearchResult|string} suggestion
|
|
|
|
* @param {UrlParams} params
|
|
|
|
* @param {string} articlePath
|
|
|
|
* @return {string}
|
2022-02-01 20:52:16 +00:00
|
|
|
*/
|
|
|
|
generateUrl(
|
|
|
|
suggestion,
|
|
|
|
params = {
|
|
|
|
title: 'Special:Search'
|
|
|
|
},
|
|
|
|
articlePath = config.get( 'wgScript' )
|
|
|
|
) {
|
|
|
|
if ( typeof suggestion !== 'string' ) {
|
|
|
|
suggestion = suggestion.title;
|
|
|
|
} else {
|
|
|
|
// Add `fulltext` query param to search within pages and for navigation
|
|
|
|
// to the search results page (prevents being redirected to a certain
|
|
|
|
// article).
|
2023-03-28 18:45:35 +00:00
|
|
|
params = Object.assign( {}, params, {
|
|
|
|
fulltext: '1'
|
|
|
|
} );
|
2022-02-01 20:52:16 +00:00
|
|
|
}
|
|
|
|
|
2023-04-12 22:37:36 +00:00
|
|
|
const searchParams = new URLSearchParams(
|
|
|
|
Object.assign( {}, params, { search: suggestion } )
|
|
|
|
);
|
2024-01-11 19:08:25 +00:00
|
|
|
return `${ articlePath }?${ searchParams.toString() }`;
|
2022-02-01 20:52:16 +00:00
|
|
|
}
|
2022-09-30 16:25:35 +00:00
|
|
|
} );
|
2022-02-01 20:52:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/** @module urlGenerator */
|
|
|
|
module.exports = urlGenerator;
|