(href, pages, redirects)
| 2 | import getRedirect from './get-redirect.js' |
| 3 | |
| 4 | export default function findPage(href, pages, redirects) { |
| 5 | // remove any fragments |
| 6 | href = new URL(href, 'http://example.com').pathname |
| 7 | |
| 8 | const redirectsContext = { redirects, pages } |
| 9 | |
| 10 | // find the page |
| 11 | const page = pages[href] || pages[getRedirect(href, redirectsContext)] |
| 12 | if (page) return page |
| 13 | |
| 14 | // get the current language |
| 15 | const currentLang = getLanguageCode.test(href) ? href.match(getLanguageCode)[1] : 'en' |
| 16 | |
| 17 | // try to fall back to English if the translated page can't be found |
| 18 | const englishHref = href.replace(`/${currentLang}/`, '/en/') |
| 19 | return pages[englishHref] || pages[getRedirect(englishHref, redirectsContext)] |
| 20 | } |
no test coverage detected