site: refactor doc utils

This commit is contained in:
Glen Huang
2025-10-08 12:33:13 +08:00
committed by Johannes Kirschbauer
parent 4ba722dd36
commit bf46ea1ebb

View File

@@ -75,88 +75,90 @@ export async function normalizeNavLink(
}; };
} }
if (!("items" in navLink)) { 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),
};
}
return { return {
...navLink, ...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), badge: normalizeBadge(navLink.badge),
external: /^https?:\/\//.test(navLink.link),
}; };
} }
return { return {
...navLink, ...navLink,
collapsed: !!navLink.collapsed,
badge: normalizeBadge(navLink.badge), badge: normalizeBadge(navLink.badge),
items: await Promise.all(navLink.items.map(normalizeNavLink)), external: /^(https?:)?\/\//.test(navLink.link),
}; };
} }