MCPcopy Create free account
hub / github.com/arktypeio/arktype / buildPageTree

Function buildPageTree

ark/docs/lib/content/pageTree.ts:101–255  ·  view source on GitHub ↗
(contentDir: string)

Source from the content-addressed store, hash-verified

99 )
100
101export const buildPageTree = (contentDir: string): PageTree => {
102 const errors: string[] = []
103 const warnings: string[] = []
104 const pagesByPath = new Map<string, Page>()
105 const listed: Page[] = []
106
107 const loadPage = (path: string, section: string): Page => {
108 const existing = pagesByPath.get(path)
109 if (existing) return existing
110 const parsed = parseMdx(readFileSync(path, "utf8"))
111 if (parsed.problem)
112 errors.push(`${contentPath(contentDir, path)}: ${parsed.problem}`)
113 const url = pageUrl(contentDir, path)
114 const page: Page = {
115 url,
116 slug: url.split("/").slice(2),
117 path,
118 section,
119 frontmatter: parsed.frontmatter,
120 listed: false
121 }
122 pagesByPath.set(path, page)
123 return page
124 }
125
126 const listPage = (page: Page) => {
127 if (page.listed) {
128 warnings.push(`${page.url} is listed more than once`)
129 return
130 }
131 page.listed = true
132 listed.push(page)
133 }
134
135 const resolveEntries = (
136 dir: string,
137 entries: string[],
138 section: string
139 ): NavItem[] =>
140 entries.flatMap((entry): NavItem[] => {
141 const separator = separatorPattern.exec(entry)
142 if (separator) return [{ kind: "separator", title: separator[1].trim() }]
143
144 const link = linkPattern.exec(entry)
145 if (link) return [{ kind: "link", title: link[1], url: link[2] }]
146
147 const path = join(dir, entry)
148 if (existsSync(`${path}.mdx`)) {
149 const page = loadPage(`${path}.mdx`, section)
150 listPage(page)
151 return [{ kind: "page", page }]
152 }
153 if (isDir(path)) return [resolveFolder(path, section)]
154
155 warnings.push(
156 `${contentPath(contentDir, join(dir, "meta.json"))} lists "${entry}", which doesn't exist`
157 )
158 return []

Callers 2

getPageTreeFunction · 0.90
fixtureTreeFunction · 0.90

Calls 13

readMetaFunction · 0.85
isDirFunction · 0.85
joinFunction · 0.85
resolveEntriesFunction · 0.85
defaultEntriesFunction · 0.85
mdxFilesInFunction · 0.85
loadPageFunction · 0.85
contentPathFunction · 0.85
filterMethod · 0.80
sortMethod · 0.80
getMethod · 0.65
pushMethod · 0.45

Tested by 1

fixtureTreeFunction · 0.72