Debian Codegraph @

skins/Vector/resources/skins.vector.search/urlGenerator.js

raw symbols SCIP
1 /**
2 * @typedef {Record<string,string>} UrlParams
3 * @param {string} title
4 * @param {string} fulltext
5 */
6
7 /**
8 * @callback generateUrl
9 * @param {RestResult|SearchResult|string} searchResult
10 * @param {UrlParams} [params]
11 * @param {string} [articlePath]
12 * @return {string}
13 */
14
15 /**
16 * @typedef {Object} UrlGenerator
17 * @property {generateUrl} generateUrl
18 */
19
20 /**
21 * Generates URLs for suggestions like those in MediaWiki's mediawiki.searchSuggest implementation.
22 *
23 * @param {MwMap} config
24 * @return {UrlGenerator}
25 */
26 function urlGenerator( config ) {
27 // TODO: This is a placeholder for enabling customization of the URL generator.
28 // wgVectorSearchUrlGenerator has not been defined as a config variable yet.
29 return config.get( 'wgVectorSearchUrlGenerator', {
30 /**
31 * @param {RestResult|SearchResult|string} suggestion
32 * @param {UrlParams} params
33 * @param {string} articlePath
34 * @return {string}
35 */
36 generateUrl(
37 suggestion,
38 params = {
39 title: 'Special:Search'
40 },
41 articlePath = config.get( 'wgScript' )
42 ) {
43 if ( typeof suggestion !== 'string' ) {
44 suggestion = suggestion.title;
45 } else {
46 // Add `fulltext` query param to search within pages and for navigation
47 // to the search results page (prevents being redirected to a certain
48 // article).
49 params = Object.assign( {}, params, {
50 fulltext: '1'
51 } );
52 }
53
54 const searchParams = new URLSearchParams(
55 Object.assign( {}, params, { search: suggestion } )
56 );
57 return `${ articlePath }?${ searchParams.toString() }`;
58 }
59 } );
60 }
61
62 /** @module urlGenerator */
63 module.exports = urlGenerator;