1
0
mirror of synced 2025-12-23 03:44:00 -05:00
Files
docs/middleware/featured-links.js
Vanessa Yuen 3df90fc9b8 Hello git history spelunker!
Are you looking for something? Here is all of the GitHub Docs history in one single commit. Enjoy! 🎉
2020-09-27 14:10:11 +02:00

51 lines
2.0 KiB
JavaScript

const findPage = require('../lib/find-page')
const getApplicableVersions = require('../lib/get-applicable-versions')
const { getVersionedPathWithLanguage } = require('../lib/path-utils')
// this middleware adds properties to the context object
module.exports = async (req, res, next) => {
if (!req.context.page) return next()
if (!(req.context.page.relativePath.endsWith('index.md') || req.context.page.layout === 'product-landing')) return next()
req.context.gettingStartedLinks = await getLinkData(req.context.page.rawGettingStartedLinks, req.context)
req.context.popularLinks = await getLinkData(req.context.page.rawPopularLinks, req.context)
req.context.guideLinks = await getLinkData(req.context.page.rawGuideLinks, req.context)
return next()
}
// rawLinks is an array of paths: [ '/foo' ]
// we need to convert it to an array of localized objects: [ { href: '/en/foo', title: 'Foo', intro: 'Description here' } ]
async function getLinkData (rawLinks, context) {
if (!rawLinks) return
const links = []
for (const link of rawLinks) {
const href = link.href
? getVersionedPathWithLanguage(link.href, context.currentVersion, context.currentLanguage)
: getVersionedPathWithLanguage(link, context.currentVersion, context.currentLanguage)
const linkedPage = findPage(href, context.pages, context.redirects, context.currentLanguage)
if (!linkedPage) continue
const applicableVersions = process.env.FEATURE_NEW_VERSIONS
? getApplicableVersions(linkedPage.versions, linkedPage.fullPath)
: getApplicableVersions(linkedPage.productVersions, linkedPage.fullPath)
// skip link if this is not the homepage and the link's versions do not include the current version
if (context.currentVersion !== 'homepage' && !applicableVersions.includes(context.currentVersion)) continue
const opts = { textOnly: true, encodeEntities: true }
links.push({
href,
title: await linkedPage.renderTitle(context, opts),
intro: await linkedPage.renderProp('intro', context, opts)
})
}
return links
}