MCPcopy
hub / github.com/github/docs / createTree

Function createTree

lib/create-tree.js:6–59  ·  view source on GitHub ↗
(originalPath, rootPath)

Source from the content-addressed store, hash-verified

4import Page from './page.js'
5
6export default async function createTree(originalPath, rootPath) {
7 const basePath = rootPath || originalPath
8
9 // On recursive runs, this is processing page.children items in `/<link>` format.
10 // If the path exists as is, assume this is a directory with a child index.md.
11 // Otherwise, assume it's a child .md file and add `.md` to the path.
12 let filepath
13 try {
14 await fs.access(originalPath)
15 filepath = `${originalPath}/index.md`
16 } catch {
17 filepath = `${originalPath}.md`
18 }
19
20 const relativePath = filepath.replace(`${basePath}/`, '')
21
22 // Initialize the Page! This is where the file reads happen.
23 const page = await Page.init({
24 basePath,
25 relativePath,
26 languageCode: 'en',
27 })
28
29 if (!page) {
30 // Do not throw an error if Early Access is not available.
31 if (relativePath.startsWith('early-access')) {
32 console.warn(
33 `${relativePath} could not be turned into a Page, but is ignore because it's early-access`
34 )
35 return
36 }
37
38 throw Error(`Cannot initialize page for ${filepath}`)
39 }
40
41 // Create the root tree object on the first run, and create children recursively.
42 const item = {
43 page,
44 }
45
46 // Process frontmatter children recursively.
47 if (item.page.children) {
48 assertUniqueChildren(item.page)
49 item.childPages = (
50 await Promise.all(
51 item.page.children.map(
52 async (child) => await createTree(path.posix.join(originalPath, child), basePath)
53 )
54 )
55 ).filter(Boolean)
56 }
57
58 return item
59}
60
61function assertUniqueChildren(page) {
62 if (page.children.length !== new Set(page.children).size) {

Callers 1

loadUnversionedTreeFunction · 0.85

Calls 3

ErrorFunction · 0.85
assertUniqueChildrenFunction · 0.85
initMethod · 0.80

Tested by

no test coverage detected