forked from github/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproduct-examples.js
56 lines (49 loc) · 1.82 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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import getApplicableVersions from '../../lib/get-applicable-versions.js'
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
)
const productCodeExamples = getProductExampleData(
currentProduct,
'code-examples',
currentLanguage
)
// We currently only support versioning in code examples.
// TODO support versioning across all example types.
req.context.productCodeExamples =
productCodeExamples &&
productCodeExamples.filter((example) => {
// If an example block does NOT contain the versions prop, assume it's available in all versions
return (
!example.versions ||
getApplicableVersions(example.versions).includes(req.context.currentVersion)
)
})
return next()
}