Skip to content

Commit

Permalink
fix some pass-by-reference issues
Browse files Browse the repository at this point in the history
  • Loading branch information
sarahs committed Apr 7, 2021
1 parent ad28189 commit 0eebfc4
Showing 1 changed file with 23 additions and 19 deletions.
42 changes: 23 additions & 19 deletions lib/page-data.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,35 +37,39 @@ async function loadSiteTree (unversionedTree) {
const rawTree = Object.assign({}, (unversionedTree || await loadUnversionedTree()))
const siteTree = {}

// For every language...
await Promise.all(Object.keys(languages).map(async (langCode) => {
const treePerVersion = {}

// in every version...
await Promise.all(versions.map(async (version) => {
versionPages(rawTree[langCode])
// "version" the pages.
treePerVersion[version] = versionPages(Object.assign({}, rawTree[langCode]), version)
}))

// This step can't be asynchronous because the order of child pages matters.
function versionPages (item) {
// Add a versioned href as a convenience for use in layouts.
item.href = item.page.permalinks
.find(pl => pl.pageVersion === version || (pl.pageVersion === 'homepage' && version === nonEnterpriseDefaultVersion))
.href
siteTree[langCode] = treePerVersion
}))

if (!item.childPages) return item
return siteTree
}

// Drop child pages that do not apply to the current version.
item.childPages = item.childPages.filter(childPage => childPage.page.applicableVersions.includes(version))
// This step can't be asynchronous because the order of child pages matters.
function versionPages (obj, version) {
// Add a versioned href as a convenience for use in layouts.
obj.href = obj.page.permalinks
.find(pl => pl.pageVersion === version || (pl.pageVersion === 'homepage' && version === nonEnterpriseDefaultVersion))
.href

// Run on the child pages recursively.
item.childPages.forEach(childPage => versionPages(childPage))
}
if (!obj.childPages) return obj

treePerVersion[version] = rawTree[langCode]
}))
const versionedChildPages = obj.childPages
// Drop child pages that do not apply to the current version.
.filter(childPage => childPage.page.applicableVersions.includes(version))
// Version the child pages recursively.
.map(childPage => versionPages(Object.assign({}, childPage), version))

siteTree[langCode] = treePerVersion
}))
obj.childPages = [...versionedChildPages]

return siteTree
return obj
}

// Derive a flat array of Page objects in all languages.
Expand Down

0 comments on commit 0eebfc4

Please sign in to comment.