MCPcopy
hub / github.com/github/docs / renderContentWithFallback

Function renderContentWithFallback

lib/render-with-fallback.js:18–42  ·  view source on GitHub ↗
(page, property, context, options)

Source from the content-addressed store, hash-verified

16// highter level than `lib/`) how to use the URL to figure out the
17// equivalent English page instance.
18export async function renderContentWithFallback(page, property, context, options) {
19 if (!(page instanceof Page)) {
20 throw new Error(`The first argument has to be Page instance (not ${typeof page})`)
21 }
22 if (typeof property !== 'string') {
23 throw new Error(`The second argument has to be a string (not ${typeof property})`)
24 }
25 const template = page[property]
26 try {
27 return await renderContent(template, context, options)
28 } catch (error) {
29 // Only bother trying to fallback if it was an error we *can* fall back
30 // on English for.
31 if (isLiquidError(error) && context.getEnglishPage) {
32 const enPage = context.getEnglishPage(context)
33 const englishTemplate = enPage[property]
34 // If you don't change the context, it'll confuse the liquid plugins
35 // like `data.js` that uses `environment.scope.currentLanguage`
36 const enContext = Object.assign({}, context, { currentLanguage: 'en' })
37 // Try again!
38 return await renderContent(englishTemplate, enContext, options)
39 }
40 throw error
41 }
42}
43
44// Returns the result of executing the first function, but if it fails
45// return the result of executing the second function.

Callers 2

_renderMethod · 0.90
renderPropMethod · 0.90

Calls 2

renderContentFunction · 0.85
isLiquidErrorFunction · 0.85

Tested by

no test coverage detected