Created
May 24, 2023 12:34
-
-
Save peterbe/cb89fced455252030bd14a8bed3d3bc1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import fs from 'fs' | |
import { loadPageMap, loadPages, loadSiteTree, loadUnversionedTree } from './lib/page-data.js' | |
import contextualize from './middleware/context.js' | |
import shortVersions from './middleware/contextualizers/short-versions.js' | |
import features from './middleware/contextualizers/features.js' | |
async function getInfo(page, permalink) { | |
const pathname = permalink.href | |
const renderingReq = { | |
path: pathname, | |
language: page.languageCode, | |
pagePath: pathname, | |
cookies: {}, | |
} | |
const res = {} | |
const next = () => {} | |
await contextualize(renderingReq, res, next) | |
await shortVersions(renderingReq, res, next) | |
renderingReq.context.page = page | |
await features(renderingReq, res, next) | |
const context = renderingReq.context | |
const title = await page.renderProp('title', context, { textOnly: true }) | |
const intro = await page.renderProp('intro', context, { textOnly: true }) | |
let productPage = null | |
for (const permalink of page.permalinks) { | |
const rootHref = permalink.href | |
.split('/') | |
.slice(0, permalink.pageVersion === 'free-pro-team@latest' ? 3 : 4) | |
.join('/') | |
const rootPage = context.pages[rootHref] | |
if (rootPage) { | |
productPage = rootPage | |
break | |
} | |
} | |
let product = '' | |
if (productPage) { | |
product = await productPage.renderProp('shortTitle', context, { | |
textOnly: true, | |
}) | |
if (!product) { | |
product = await productPage.renderProp('title', context, { | |
textOnly: true, | |
}) | |
} | |
} | |
return { | |
product, | |
title, | |
intro, | |
} | |
} | |
async function main() { | |
// const unversionedTree = await loadUnversionedTree(['en']) | |
const unversionedTree = await loadUnversionedTree(['en']) | |
const pageList = await loadPages(unversionedTree, ['en']) | |
// const pageList = await loadPages(unversionedTree) | |
console.log(pageList.length) | |
// console.log(pageList[0]) | |
// console.log(pageList[1]) | |
const cache = {} | |
console.time('compute it all') | |
// for (const page of pageList) { | |
// for (const permalink of page.permalinks) { | |
// cache[permalink.href] = await getInfo(page, permalink) | |
// } | |
// } | |
// for (const page of pageList) { | |
// await Promise.all( | |
// page.permalinks.map(async (permalink) => { | |
// cache[permalink.href] = await getInfo(page, permalink) | |
// }) | |
// ) | |
// } | |
// await Promise.all( | |
// pageList.map(async (page) => { | |
// await Promise.all( | |
// page.permalinks.map(async (permalink) => { | |
// cache[permalink.href] = await getInfo(page, permalink) | |
// }) | |
// ) | |
// }) | |
// ) | |
// Only the first permalink | |
await Promise.all( | |
pageList.map(async (page) => { | |
const permalink = page.permalinks[0] | |
cache[permalink.href] = await getInfo(page, permalink) | |
}) | |
) | |
console.timeEnd('compute it all') | |
console.log(Object.keys(cache).length, 'keys') | |
fs.writeFileSync('pageinfo.json', JSON.stringify(cache, null, 2), 'utf-8') | |
} | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment