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
|
|
|
*
|
|
|
|
* @return {UrlGenerator}
|
|
|
|
*/
|
2024-07-17 18:16:55 +00:00
|
|
|
function urlGenerator() {
|
2023-08-02 22:56:08 +00:00
|
|
|
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-17 18:16:55 +00:00
|
|
|
let title;
|
|
|
|
if ( !page ) {
|
|
|
|
title = 'Special:Search';
|
|
|
|
} else if ( typeof page !== 'string' ) {
|
2024-08-17 23:21:09 +00:00
|
|
|
const fragment = page.fragment;
|
2024-07-17 18:16:55 +00:00
|
|
|
title = page.title;
|
2024-08-17 23:21:09 +00:00
|
|
|
if ( fragment ) {
|
|
|
|
title += `#${ fragment }`;
|
|
|
|
}
|
2024-07-05 20:21:10 +00:00
|
|
|
} else {
|
2024-07-17 18:16:55 +00:00
|
|
|
title = page;
|
2024-07-05 20:21:10 +00:00
|
|
|
}
|
2024-07-17 18:16:55 +00:00
|
|
|
return mw.util.getUrl( title, params );
|
2023-08-02 22:56:08 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @module urlGenerator */
|
|
|
|
module.exports = urlGenerator;
|