forked from github/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproduct-examples.js
37 lines (32 loc) · 1.17 KB
/
product-examples.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import { getDataByLanguage } from '../../lib/get-data.js'
function getProductExampleData(product, key, language) {
// Because getDataByLanguage() depends on reading data files from
// disk, asking for something that doesn't exist would throw a
// `ENOENT` error from `fs.readFile` but we want that to default
// to `undefined` because certain product's don't have all product
// examples.
try {
return getDataByLanguage(`product-examples.${product}.${key}`, language)
} catch (error) {
if (error.code === 'ENOENT') return
throw error
}
}
export default async function productExamples(req, res, next) {
if (!req.context.page) return next()
if (req.context.currentLayoutName !== 'product-landing') return next()
const { currentProduct, currentLanguage } = req.context
if (currentProduct.includes('.'))
throw new Error(`currentProduct can not contain a . (${currentProduct})`)
req.context.productCommunityExamples = getProductExampleData(
currentProduct,
'community-examples',
currentLanguage,
)
req.context.productUserExamples = getProductExampleData(
currentProduct,
'user-examples',
currentLanguage,
)
return next()
}