* experimenting with redirects * cleanup developer.json * wip * clean console.log * progress * some progress * progress * much progress * debugging tests * hacky progress * ditch latest -> number redirects * minor * hacky progress * lots of progress * some small fixes * fix rendering tests * small fixes * progress * undo debugging * better * routing tests OK * more cleaning * unit tests * undoing lineending edit * undoing temporary debugging * don't ever set this.redirects on Page * cope with archived version redirects * adding code comments on the major if statements * address all feedback * update README about redirects * delete invalid test * fix feedback
21 lines
735 B
JavaScript
21 lines
735 B
JavaScript
import { getLanguageCode } from './patterns.js'
|
|
import getRedirect from './get-redirect.js'
|
|
|
|
export default function findPage(href, pageMap, redirects) {
|
|
// remove any fragments
|
|
href = href.replace(/#.*$/, '')
|
|
|
|
const redirectsContext = { redirects, pages: pageMap }
|
|
|
|
// find the page
|
|
const page = pageMap[href] || pageMap[getRedirect(href, redirectsContext)]
|
|
if (page) return page
|
|
|
|
// get the current language
|
|
const currentLang = getLanguageCode.test(href) ? href.match(getLanguageCode)[1] : 'en'
|
|
|
|
// try to fall back to English if the translated page can't be found
|
|
const englishHref = href.replace(`/${currentLang}/`, '/en/')
|
|
return pageMap[englishHref] || pageMap[getRedirect(englishHref, redirectsContext)]
|
|
}
|