1
0
mirror of synced 2025-12-30 12:02:01 -05:00
Files
docs/middleware/featured-links.js
Sarah Schneider d9b1fa9d40 use new module
2020-11-12 13:31:57 -05:00

47 lines
1.6 KiB
JavaScript

const findPageInVersion = require('../lib/find-page-in-version')
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()
if (!req.context.page.featuredLinks) return next()
req.context.featuredLinks = {}
for (const key in req.context.page.featuredLinks) {
req.context.featuredLinks[key] = await getLinkData(req.context.page.featuredLinks[key], 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 = findPageInVersion(href, context.pages, context.redirects, context.currentLanguage, context.currentVersion)
if (!linkedPage) continue
const opts = { textOnly: true, encodeEntities: true }
links.push({
href,
title: await linkedPage.renderTitle(context, opts),
intro: await linkedPage.renderProp('intro', context, opts),
page: linkedPage
})
}
return links
}