2023-08-02 22:56:08 +00:00
|
|
|
/*
|
|
|
|
* Adapted from Vector
|
|
|
|
* All credits go to the developers behind Vector
|
|
|
|
* @see https://github.com/wikimedia/mediawiki-skins-Vector/blob/master/resources/skins.vector.search/urlGenerator.js
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @typedef {Record<string,string>} UrlParams
|
|
|
|
* @param {string} title
|
|
|
|
* @param {string} fulltext
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @callback generateUrl
|
2024-07-05 20:21:10 +00:00
|
|
|
* @param {SearchResult|string} page
|
2023-08-02 22:56:08 +00:00
|
|
|
* @param {UrlParams} [params]
|
|
|
|
* @return {string}
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @typedef {Object} UrlGenerator
|
|
|
|
* @property {generateUrl} generateUrl
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2024-07-05 19:32:08 +00:00
|
|
|
* Generates URLs for suggestions.
|
2023-08-02 22:56:08 +00:00
|
|
|
*
|
|
|
|
* @param {MwMap} config
|
|
|
|
* @return {UrlGenerator}
|
|
|
|
*/
|
|
|
|
function urlGenerator( config ) {
|
|
|
|
return {
|
|
|
|
/**
|
2024-07-05 20:21:10 +00:00
|
|
|
* @param {SearchResult|string} page
|
2023-08-02 22:56:08 +00:00
|
|
|
* @param {UrlParams} params
|
|
|
|
* @return {string}
|
|
|
|
*/
|
|
|
|
generateUrl(
|
2024-07-05 20:21:10 +00:00
|
|
|
page,
|
|
|
|
params = {}
|
2023-08-02 22:56:08 +00:00
|
|
|
) {
|
2024-07-05 20:21:10 +00:00
|
|
|
const getPageTitle = () => {
|
|
|
|
let title;
|
|
|
|
if ( !page ) {
|
|
|
|
title = 'Special:Search';
|
|
|
|
} else if ( typeof page !== 'string' ) {
|
|
|
|
title = page.title;
|
|
|
|
} else {
|
|
|
|
title = page;
|
|
|
|
}
|
|
|
|
return encodeURIComponent( title );
|
|
|
|
};
|
2023-08-02 22:56:08 +00:00
|
|
|
|
2024-07-05 20:21:10 +00:00
|
|
|
// Use short URL if avaliable, instead of doing a bunch of 302 like mediawiki.searchSuggest does
|
|
|
|
const articlePath = config.wgArticlePath.replace( '$1', getPageTitle( page ) );
|
2024-07-05 19:32:08 +00:00
|
|
|
|
2024-07-05 20:21:10 +00:00
|
|
|
if ( Object.keys( params ).length === 0 ) {
|
|
|
|
return articlePath;
|
|
|
|
} else {
|
|
|
|
const searchParams = new URLSearchParams( params );
|
|
|
|
return `${ articlePath }?${ searchParams.toString() }`;
|
|
|
|
}
|
2023-08-02 22:56:08 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @module urlGenerator */
|
|
|
|
module.exports = urlGenerator;
|