| 220 | } |
| 221 | |
| 222 | export async function versionPages(obj, version, langCode) { |
| 223 | // Add a versioned href as a convenience for use in layouts. |
| 224 | obj.href = obj.page.permalinks.find( |
| 225 | (pl) => |
| 226 | pl.pageVersion === version || |
| 227 | (pl.pageVersion === 'homepage' && version === nonEnterpriseDefaultVersion) |
| 228 | ).href |
| 229 | |
| 230 | if (!obj.childPages) return obj |
| 231 | const versionedChildPages = await Promise.all( |
| 232 | obj.childPages |
| 233 | // Drop child pages that do not apply to the current version |
| 234 | .filter((childPage) => childPage.page.applicableVersions.includes(version)) |
| 235 | // Version the child pages recursively. |
| 236 | .map((childPage) => versionPages(Object.assign({}, childPage), version, langCode)) |
| 237 | ) |
| 238 | |
| 239 | obj.childPages = [...versionedChildPages] |
| 240 | |
| 241 | return obj |
| 242 | } |
| 243 | |
| 244 | // Derive a flat array of Page objects in all languages. |
| 245 | export async function loadPageList(unversionedTree, languagesOnly = null) { |