mediawiki-skins-Vector/resources/skins.vector.search/urlGenerator.js
Lucas Werkmeister 86c9693693 search: Reduce skins.vector.search module size
Change I765d3bbf89 pushes the module over the configured maximum allowed
size (3 KiB, see bundlesize.config.json); shave off some bytes elsewhere
to bring it below the limit again. IMHO all of these changes should be
acceptable:

* arrow functions are already used elsewhere in this module;
* using the mw.config.get() fallback argument is normal (it slightly
  changes behavior, but I don’t think explicitly setting the search
  client or URL generator to a falsy value and expecting to get the
  default behavior should be considered supported);
* not quoting [name="search"] matches [name=title] immediately above;
* using forEach() with a function reference is still readable (initApp()
  is now called with extra arguments, but that doesn’t matter).

Change-Id: I45dda26cb59279d91804b0c2bbf12174fa78ee12
2022-09-30 19:05:03 +02:00

59 lines
1.4 KiB
JavaScript

/* global RestResult, SearchResult */
/**
* @typedef {Object} UrlParams
* @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.
return config.get( 'wgVectorSearchUrlGenerator', {
/**
* @type {generateUrl}
*/
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).
// @ts-ignore
params.fulltext = '1';
}
return articlePath + '?' + $.param( $.extend( {}, params, { search: suggestion } ) );
}
} );
}
/** @module urlGenerator */
module.exports = urlGenerator;