1
0
mirror of synced 2026-01-04 09:06:46 -05:00
Files
docs/lib/find-page.js
James M. Greene fb30a0766f Unravel pages (the array + map object) (#16708)
* Revise the 'pages' module to export two methods: 'loadPages' and 'loadPageMap'

Update all existing references to use 'loadPages' for now

* Remove explicit Promise resolutions on loadPage* methods

* Condense reduction method into its now-singular usage spot

* Opt for for-of instead of forEach

* Make require of pages in warm-server more explicit

* Be more explicit about find-page using a pageMap

* Be more explicit about find-page-in-version using a pageMap

* Be more explicit about site-tree using a pageMap

* Extract the map creation from loadPageMap

* Be more explicit about using a pageMap

* Update redirects precompile to take two arguments: pageList, pageMap

* Rename internal loadPages method to loadPageList

* Clarify pageMap is what is stored in context.pages

* Use loadPageMap in tests and stuff
2020-12-03 22:31:10 +00:00

66 lines
2.2 KiB
JavaScript

const slash = require('slash')
const patterns = require('./patterns')
const allVersions = Object.keys(require('./all-versions'))
const { getVersionedPathWithLanguage } = require('./path-utils')
module.exports = function findPage (href, pageMap, redirects = {}, languageCode = 'en', sourceLanguage = null) {
// Convert Windows backslashes to forward slashes
// remove trailing slash
href = slash(href).replace(patterns.trailingSlash, '$1')
// check all potential versions
const versionedPathsToCheck = [...new Set(allVersions.map(version => {
return getVersionedPathWithLanguage(href, version, languageCode)
}))]
// get the first found path of the page (account for redirects)
let pathToPage = versionedPathsToCheck.find(path => {
path = redirects[path] || path
return pageMap[removeFragment(path)]
})
// need to account for redirects again
pathToPage = redirects[pathToPage] || pathToPage
// find the page
const page = pageMap[removeFragment(pathToPage)]
if (page) return page
if (process.env.NODE_ENV !== 'test' && languageCode === 'en') {
const error = sourceLanguage
? `href not found in ${sourceLanguage} pages (no English fallback found)`
: 'href not found'
// if English page can't be found, throw an error
// because these errors should be surfaced and fixed right away
if (sourceLanguage === 'en') {
throw new Error(`${error}: ${href}`)
} else {
console.error(`${error}: ${href}`)
}
}
// if English page can't be found in tests, don't throw an error
// or the tests will stall
if (process.env.NODE_ENV === 'test' && languageCode === 'en') {
if (sourceLanguage === 'en') console.log(`href not found: ${href}`)
return null
}
// if localized page can't be found, fall back to English
// because localized content is not yet synced
if (languageCode !== 'en') {
// pass the source language so we can surface it in error messages
return findPage(href, pageMap, redirects, 'en', languageCode)
}
}
// some redirects include fragments
// need to remove the fragment to find the page
function removeFragment (path) {
if (!path) return
return path.replace(/#.*$/, '')
}