Remove reliance on platform to construct URLs (#738)
Refactor code that depends on the destination wiki's platform in order to support generic wiki platforms (e.g. Wikidot). Redirection to the Main Page now uses the destination_content_path to send users directly to the destination wiki's main page, rather than using a different URL depending on the destination wiki's software. The only remaining code that depends on the destination wiki's software is the construction of the search path. This code now has a default case that doesn't add any software-specific path to a constructed search URL. This allows any wiki, regardless of the software it runs on, to be added by putting its full search path in the "destination_search_path" property of the redirect definition. As a result of these changes, it is now possible to add redirects to Wikidot wikis, by just defining the full search path in their redirect entry's "destination_search_path". Co-authored-by: SnorlaxMonster <snorlaxmonster@users.noreply.github.com>pull/755/head
parent
f4c5ca10d2
commit
2b14da434b
|
@ -175,52 +175,52 @@ function commonFunctionGetDestinationArticle(matchingSite, article) {
|
||||||
return matchingSite['destination_content_prefix'] + article + matchingSite['destination_content_suffix'];
|
return matchingSite['destination_content_prefix'] + article + matchingSite['destination_content_suffix'];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function encodeArticleTitle(articleTitle) {
|
||||||
|
// We decode + encode to ensure we don't double-encode,
|
||||||
|
// in the event a string is already encoded.
|
||||||
|
// We wrap in a try-catch as decoding can sometimes fail if destination article
|
||||||
|
// does have special characters (e.g. %) in the title.
|
||||||
|
try {
|
||||||
|
return encodeURIComponent(decodeURIComponent(articleTitle));
|
||||||
|
} catch {
|
||||||
|
return encodeURIComponent(articleTitle);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
function commonFunctionGetNewURL(originURL, matchingSite) {
|
function commonFunctionGetNewURL(originURL, matchingSite) {
|
||||||
// Get article name from the end of the URL;
|
// Get article name from the end of the URL;
|
||||||
// We can't just take the last part of the path due to subpages;
|
// We can't just take the last part of the path due to subpages;
|
||||||
// Instead, we take everything after the wiki's base URL + content path
|
// Instead, we take everything after the wiki's base URL + content path
|
||||||
let originArticle = commonFunctionGetOriginArticle(originURL, matchingSite);
|
let originArticle = commonFunctionGetOriginArticle(originURL, matchingSite);
|
||||||
let destinationArticle = commonFunctionGetDestinationArticle(matchingSite, originArticle);
|
let destinationArticle = commonFunctionGetDestinationArticle(matchingSite, originArticle);
|
||||||
|
|
||||||
// Set up URL to redirect user to based on wiki platform
|
// Set up URL to redirect user to based on wiki platform
|
||||||
let newURL = '';
|
let newURL = '';
|
||||||
if (originArticle) {
|
|
||||||
// Check if main page
|
// If the article is the main page (or missing), redirect to the indie wiki's main page
|
||||||
if (decodeURIComponent(originArticle) === matchingSite['origin_main_page']) {
|
if ((!originArticle) || (decodeURIComponent(originArticle) === matchingSite['origin_main_page'])) {
|
||||||
switch (matchingSite['destination_platform']) {
|
const mainPageArticle = encodeArticleTitle(matchingSite['destination_main_page']);
|
||||||
case 'dokuwiki':
|
newURL = 'https://' + matchingSite["destination_base_url"] + matchingSite["destination_content_path"] + mainPageArticle + matchingSite['destination_content_suffix'];
|
||||||
destinationArticle = '';
|
return newURL;
|
||||||
break;
|
|
||||||
default:
|
|
||||||
destinationArticle = matchingSite['destination_main_page'] + matchingSite['destination_content_suffix'];
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Replace underscores with spaces as that performs better in search
|
// Replace underscores with spaces as that performs better in search
|
||||||
destinationArticle = destinationArticle.replaceAll('_', ' ');
|
const encodedDestinationArticle = encodeArticleTitle(destinationArticle.replaceAll('_', ' '));
|
||||||
// Encode article
|
|
||||||
// We decode + encode to ensure we don't double-encode,
|
|
||||||
// in the event a string is already encoded.
|
|
||||||
// We wrap in a try-catch as decoding can sometimes fail if destination article
|
|
||||||
// does have special characters (e.g. %) in the title.
|
|
||||||
try {
|
|
||||||
destinationArticle = encodeURIComponent(decodeURIComponent(destinationArticle));
|
|
||||||
} catch {
|
|
||||||
destinationArticle = encodeURIComponent(destinationArticle);
|
|
||||||
}
|
|
||||||
|
|
||||||
let searchParams = '';
|
let searchParams = '';
|
||||||
switch (matchingSite['destination_platform']) {
|
switch (matchingSite['destination_platform']) {
|
||||||
case 'mediawiki':
|
case 'mediawiki':
|
||||||
searchParams = '?title=Special:Search&search=' + destinationArticle;
|
searchParams = `?title=Special:Search&search=${encodedDestinationArticle}`;
|
||||||
break;
|
break;
|
||||||
case 'dokuwiki':
|
case 'dokuwiki':
|
||||||
searchParams = '?do=search&q=' + destinationArticle;
|
searchParams = `?do=search&q=${encodedDestinationArticle}`;
|
||||||
|
break;
|
||||||
|
// Otherwise, assume the full search path is defined on "destination_search_path"
|
||||||
|
default:
|
||||||
|
searchParams = encodedDestinationArticle;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
newURL = 'https://' + matchingSite["destination_base_url"] + matchingSite["destination_search_path"] + searchParams;
|
newURL = 'https://' + matchingSite["destination_base_url"] + matchingSite["destination_search_path"] + searchParams;
|
||||||
} else {
|
|
||||||
newURL = 'https://' + matchingSite["destination_base_url"];
|
|
||||||
}
|
|
||||||
|
|
||||||
return newURL;
|
return newURL;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue