MCPcopy Index your code
hub / github.com/github/docs / translateTree

Function translateTree

lib/page-data.js:69–181  ·  view source on GitHub ↗
(dir, langObj, enTree)

Source from the content-addressed store, hash-verified

67}
68
69async function translateTree(dir, langObj, enTree) {
70 const item = {}
71 const enPage = enTree.page
72 const { ...enData } = enPage
73
74 const basePath = dir
75 const relativePath = enPage.relativePath
76 const fullPath = path.join(basePath, relativePath)
77
78 let data
79 let content
80 try {
81 const read = await readFileContents(fullPath)
82 // If it worked, great!
83 content = read.content
84 data = read.data
85
86 if (!data) {
87 // If the file's frontmatter Yaml is entirely broken,
88 // the result of `readFileContents()` is that you just
89 // get a `errors` key. E.g.
90 //
91 // errors: [
92 // {
93 // reason: 'invalid frontmatter entry',
94 // message: 'YML parsing error!',
95 // filepath: 'translations/ja-JP/content/get-started/index.md'
96 // }
97 // ]
98 //
99 // If this the case throw error so we can lump this error with
100 // how we deal with the file not even being present on disk.
101 throw new FrontmatterParsingError(read.errors)
102 }
103
104 for (const { property } of read.errors) {
105 // If any of the errors happened on keys that are considered
106 // translatable, we can't accept that and have to fall back to
107 // English.
108 // For example, if a Japanese page's frontmatter lacks `title`,
109 // (which triggers a 'is required' error) you can't include it
110 // because you'd have a Page with `{title: undefined}`.
111 // The beauty in this is that if the translated content file
112 // has something wrong with, say, the `versions` frontmatter key
113 // we don't even care because we won't be using it anyway.
114 if (translatableFrontmatterKeys.includes(property)) {
115 const message = `frontmatter error on '${property}' (in ${fullPath}) so falling back to English`
116 if (DEBUG_TRANSLATION_FALLBACKS) {
117 // The object format is so the health report knows which path the issue is on
118 console.warn({ message, path: relativePath })
119 }
120 if (THROW_TRANSLATION_ERRORS) {
121 throw new Error(message)
122 }
123 data[property] = enData[property]
124 }
125 }
126 } catch (error) {

Callers 1

loadUnversionedTreeFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected