MCPcopy
hub / github.com/github/docs / rereadByPath

Function rereadByPath

middleware/find-page.js:60–82  ·  view source on GitHub ↗
(uri, contentRoot, currentVersion)

Source from the content-addressed store, hash-verified

58}
59
60async function rereadByPath(uri, contentRoot, currentVersion) {
61 const languageCode = uri.match(languagePrefixRegex)[1]
62 const withoutLanguage = uri.replace(languagePrefixRegex, '/')
63 const withoutVersion = withoutLanguage.replace(`/${currentVersion}`, '')
64 // TODO: Support loading translations the same way.
65 // NOTE: No one is going to test translations like this in development
66 // but perhaps one day we can always and only do these kinds of lookups
67 // at runtime.
68 const possible = path.join(contentRoot, withoutVersion)
69 const filePath = existsSync(possible) ? path.join(possible, 'index.md') : possible + '.md'
70 const relativePath = path.relative(contentRoot, filePath)
71 const basePath = contentRoot
72
73 // Remember, the Page.init() can return a Promise that resolves to falsy
74 // if it can't read the file in from disk. E.g. a request for /en/non/existent.
75 // In other words, it's fine if it can't be read from disk. It'll get
76 // handled and turned into a nice 404 message.
77 return await Page.init({
78 basePath,
79 relativePath,
80 languageCode,
81 })
82}

Callers 1

findPageFunction · 0.85

Calls 1

initMethod · 0.80

Tested by

no test coverage detected