| 99 | ) |
| 100 | |
| 101 | export 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 [] |