From bf46ea1ebb30d51817f72ee8d6828cc6417595a5 Mon Sep 17 00:00:00 2001 From: Glen Huang Date: Wed, 8 Oct 2025 12:33:13 +0800 Subject: [PATCH] site: refactor doc utils --- site/src/routes/docs/utils.ts | 148 +++++++++++++++++----------------- 1 file changed, 75 insertions(+), 73 deletions(-) diff --git a/site/src/routes/docs/utils.ts b/site/src/routes/docs/utils.ts index b3938ced8..a94092c26 100644 --- a/site/src/routes/docs/utils.ts +++ b/site/src/routes/docs/utils.ts @@ -75,88 +75,90 @@ export async function normalizeNavLink( }; } - if (!("items" in navLink)) { - if ("slug" in navLink) { - const article = articles[navLink.slug]; - if (!article) { - throw new Error(`Doc not found: ${navLink.slug}`); - } - return { - label: navLink.label ?? (await article()).frontmatter.title, - link: `/docs/${navLink.slug}`, - badge: normalizeBadge(navLink.badge), - external: false, - }; - } - if ("autogenerate" in navLink) { - const dir = navLink.autogenerate.directory; - const articleEntries = Object.entries(articles).filter(([key]) => - key.startsWith(dir + "/"), - ); - - const frontmatters = await Promise.all( - articleEntries.map(async ([key, article]) => ({ - key, - frontmatter: (await article()).frontmatter, - })), - ); - - let titleMissing = false; - // Check frontmatter for title - for (const item of frontmatters) { - if (!item.frontmatter.title) { - console.error( - `Missing title in frontmatter for autogenerated doc: ${item.key}`, - ); - titleMissing = true; - } - } - if (titleMissing) throw new Error("Aborting due to errors."); - - const items: NormalizedNavLink[] = await Promise.all( - frontmatters - .sort((a, b) => { - const orderA = a.frontmatter.order; - const orderB = b.frontmatter.order; - if (orderA != null && orderB != null) { - return orderA - orderB; - } - if (orderA != null) { - return -1; - } - if (orderB != null) { - return 1; - } - const titleA = a.frontmatter.title ?? a.key; - const titleB = a.frontmatter.title ?? a.key; - return titleA.localeCompare(titleB.title); - }) - .map((item) => - normalizeNavLink({ - label: item.frontmatter.title, - link: `/docs/${item.key}`, - }), - ), - ); - return { - label: navLink.label ?? dir.split("/").slice(-1)[0], - items, - collapsed: !!navLink.collapsed, - badge: normalizeBadge(navLink.badge), - }; - } + if ("items" in navLink) { return { ...navLink, + collapsed: !!navLink.collapsed, + badge: normalizeBadge(navLink.badge), + items: await Promise.all(navLink.items.map(normalizeNavLink)), + }; + } + + if ("slug" in navLink) { + const article = articles[navLink.slug]; + if (!article) { + throw new Error(`Doc not found: ${navLink.slug}`); + } + return { + label: navLink.label ?? (await article()).frontmatter.title, + link: `/docs/${navLink.slug}`, + badge: normalizeBadge(navLink.badge), + external: false, + }; + } + + if ("autogenerate" in navLink) { + const dir = navLink.autogenerate.directory; + const articleEntries = Object.entries(articles).filter(([key]) => + key.startsWith(dir + "/"), + ); + + const frontmatters = await Promise.all( + articleEntries.map(async ([key, article]) => ({ + key, + frontmatter: (await article()).frontmatter, + })), + ); + + let titleMissing = false; + // Check frontmatter for title + for (const item of frontmatters) { + if (!item.frontmatter.title) { + console.error( + `Missing title in frontmatter for autogenerated doc: ${item.key}`, + ); + titleMissing = true; + } + } + if (titleMissing) throw new Error("Aborting due to errors."); + + const items: NormalizedNavLink[] = await Promise.all( + frontmatters + .sort((a, b) => { + const orderA = a.frontmatter.order; + const orderB = b.frontmatter.order; + if (orderA != null && orderB != null) { + return orderA - orderB; + } + if (orderA != null) { + return -1; + } + if (orderB != null) { + return 1; + } + const titleA = a.frontmatter.title ?? a.key; + const titleB = a.frontmatter.title ?? a.key; + return titleA.localeCompare(titleB.title); + }) + .map((item) => + normalizeNavLink({ + label: item.frontmatter.title, + link: `/docs/${item.key}`, + }), + ), + ); + return { + label: navLink.label ?? dir.split("/").slice(-1)[0], + items, + collapsed: !!navLink.collapsed, badge: normalizeBadge(navLink.badge), - external: /^https?:\/\//.test(navLink.link), }; } return { ...navLink, - collapsed: !!navLink.collapsed, badge: normalizeBadge(navLink.badge), - items: await Promise.all(navLink.items.map(normalizeNavLink)), + external: /^(https?:)?\/\//.test(navLink.link), }; }