diff --git a/website/api/controllers/handbook/view-basic-handbook.js b/website/api/controllers/handbook/view-basic-handbook.js index 6f7ac876e8..fa9992acc2 100644 --- a/website/api/controllers/handbook/view-basic-handbook.js +++ b/website/api/controllers/handbook/view-basic-handbook.js @@ -41,14 +41,25 @@ module.exports = { let thisPage = _.find(sails.config.builtStaticContent.markdownPages, { url: _.trimRight(SECTION_URL_PREFIX + '/' + _.trim(pageUrlSuffix, '/'), '/') }); - if (!thisPage) { - throw 'notFound'; - } - if (false) { - // TODO: add "redirect" exit and handle mismatched capitalization / extra slashes by redirecting to the correct URL. e.g. "http://localhost:2024/docs//usiNG-fleet///" Partial example of this: https://github.com/sailshq/sailsjs.com/blob/b53c6e6a90c9afdf89e5cae00b9c9dd3f391b0e7/api/controllers/documentation/view-documentation.js#L161-L166 - let revisedUrl = 'todo'; - throw {redirect: revisedUrl}; + // Setting a flag if the pageUrlSuffix doesn't match any existing page, or if the page it matches doesn't exactly match the pageUrlSuffix provided + // Note: because this also handles fleetdm.com/handbook and a pageUrlSuffix might not have provided, we set this flag to false if the url is just '/handbook' + let needsRedirectMaybe = (!thisPage || (thisPage.url !== '/handbook/'+pageUrlSuffix && thisPage.url !== '/handbook')); + if (needsRedirectMaybe) { + // Creating a lower case, repeating-slashless pageUrlSuffix + let multipleSlashesRegex = /\/{2,}/g; + let modifiedPageUrlSuffix = pageUrlSuffix.toLowerCase().replace(multipleSlashesRegex, '/'); + // Finding the appropriate page content using the modified pageUrlSuffix. + let revisedPage = _.find(sails.config.builtStaticContent.markdownPages, { + url: _.trimRight(SECTION_URL_PREFIX + '/' + _.trim(modifiedPageUrlSuffix, '/'), '/') + }); + if(revisedPage) { + // If we matched a page with the modified pageUrlSuffix, then redirect to that. + throw {redirect: revisedPage.url}; + } else { + // If no page was found, throw a 404 error. + throw 'notFound'; + } } // Setting the meta title for this page. let pageTitleForMeta;