1
0
mirror of synced 2025-12-19 18:10:59 -05:00
Files
docs/lib/find-page.js
Peter Bengtsson 07c8fc3c2a Decouple redirects from language (#24597)
* 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
2022-02-14 20:19:10 +00:00

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)]
}