Skip to content

Commit efedf80

Browse files
committed
refactoring
1 parent b880f2b commit efedf80

3 files changed

Lines changed: 46 additions & 54 deletions

File tree

app/utils/llms-utils.ts

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { createDomain } from "~/utils/http"
22
import { loadContentCollections } from "~/utils/load-content-collections"
3-
import type { PageRec, SectionRec } from "../../content-collections"
3+
import type { PageRecord, SectionRecord } from "../../content-collections"
44
import { pageUrl } from "./version-links"
55
import { versions } from "./versions"
66
import type { Version } from "./versions-utils"
@@ -15,20 +15,20 @@ async function loadVersionData(version: Version) {
1515
}
1616

1717
async function loadAllVersions() {
18-
const acc: Record<string, { pages: PageRec[]; sections: SectionRec[] }> = {}
18+
const acc: Record<string, { pages: PageRecord[]; sections: SectionRecord[] }> = {}
1919
for (const v of versions) {
2020
const loaded = await loadVersionData(v)
2121
if (loaded) acc[v] = loaded
2222
}
2323
return acc
2424
}
2525

26-
function buildSectionTitles(sections: SectionRec[]) {
26+
function buildSectionTitles(sections: SectionRecord[]) {
2727
return new Map(sections.map((s) => [s.slug.split("/").pop() || "", s.title]))
2828
}
2929

30-
function groupPagesByFolder(pages: PageRec[]) {
31-
const groups = new Map<string, PageRec[]>()
30+
function groupPagesByFolder(pages: PageRecord[]) {
31+
const groups = new Map<string, PageRecord[]>()
3232

3333
for (const p of pages) {
3434
const id = p.section ?? p._meta?.path?.split("/")[0]
@@ -45,25 +45,25 @@ function groupPagesByFolder(pages: PageRec[]) {
4545
return groups
4646
}
4747

48-
function renderVersionBlock(domain: string, version: string, pages: PageRec[], sections: SectionRec[]) {
48+
function renderVersionBlock(domain: string, version: string, pages: PageRecord[], sections: SectionRecord[]) {
4949
if (!pages.length) return `## ${version}\n\n_No pages found._`
5050

5151
const sectionTitles = buildSectionTitles(sections)
5252
const groups = groupPagesByFolder(pages)
5353

54-
const blocks = Array.from(groups.entries())
55-
.map(([id, list]) => {
56-
const label = sectionTitles.get(id) ?? id
57-
const lines = list
58-
.map((p) => {
59-
const url = pageUrl(domain, version, p.slug)
60-
const note = p.summary || p.description || ""
61-
return `- [${p.title}](${url})${note ? `: ${note}` : ""}`
62-
})
63-
.join("\n")
64-
return `### ${label}\n\n${lines}`
65-
})
66-
.join("\n\n")
54+
const renderPageLink = (p: PageRecord) => {
55+
const url = pageUrl(domain, version, p.slug)
56+
const note = p.summary || p.description || ""
57+
return `- [${p.title}](${url})${note ? `: ${note}` : ""}`
58+
}
59+
60+
const renderSection = ([id, list]: [string, PageRecord[]]) => {
61+
const label = sectionTitles.get(id) ?? id
62+
const lines = list.map(renderPageLink).join("\n")
63+
return `### ${label}\n\n${lines}`
64+
}
65+
66+
const blocks = Array.from(groups.entries()).map(renderSection).join("\n\n")
6767

6868
return `\n## ${version}\n\n${blocks}`
6969
}

app/utils/version-links.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export function resolveLayoutVersion(paramsVersion: string | undefined, request:
3030
return ensureVersion(isKnownVersion(first) ? first : undefined)
3131
}
3232

33-
export function homepageUrl(base: string, version: string) {
33+
function homepageUrl(base: string, version: string) {
3434
return `${base}/${version}/home`
3535
}
3636

content-collections.ts

Lines changed: 26 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,31 @@ const sectionSchema = z.object({
77
title: z.string(),
88
})
99

10+
const pageSchema = z.object({
11+
title: z.string(),
12+
summary: z.string(),
13+
description: z.string(),
14+
})
15+
16+
const metaSchema = z.object({ path: z.string() }).partial().optional()
17+
const outputBaseSchema = z.object({
18+
slug: z.string(),
19+
_meta: metaSchema,
20+
})
21+
22+
const sectionOutputSchema = sectionSchema.extend(outputBaseSchema.shape)
23+
const pageOutputSchema = pageSchema.extend({
24+
...outputBaseSchema.shape,
25+
section: z.string().optional(),
26+
rawMdx: z.string(),
27+
content: z.unknown(),
28+
})
29+
30+
export type SectionRecord = z.infer<typeof sectionOutputSchema>
31+
export type PageRecord = z.infer<typeof pageOutputSchema>
32+
export const SectionsArray = z.array(sectionOutputSchema)
33+
export const PagesArray = z.array(pageOutputSchema)
34+
1035
/**
1136
* Removes leading number prefixes like "01-", "02-" from each path segment.
1237
*/
@@ -30,20 +55,10 @@ const section = defineCollection({
3055
transform: (document) => {
3156
const relativePath = document._meta.path.split("/").filter(Boolean).join("/")
3257
const slug = cleanSlug(relativePath)
33-
34-
return {
35-
...document,
36-
slug,
37-
}
58+
return { ...document, slug }
3859
},
3960
})
4061

41-
const pageSchema = z.object({
42-
title: z.string(),
43-
summary: z.string(),
44-
description: z.string(),
45-
})
46-
4762
/*
4863
* This collection defines an individual documentation page within the package documentation.
4964
*
@@ -57,11 +72,9 @@ const page = defineCollection({
5772
transform: async (document, context) => {
5873
const relativePath = document._meta.path.split("/").filter(Boolean).join("/")
5974
const slug = cleanSlug(relativePath)
60-
6175
const content = await compileMDX(context, document, {
6276
rehypePlugins: [rehypeSlug],
6377
})
64-
6578
// rawMdx is the content without the frontmatter, used to read headings from the mdx file and create a content tree for the table of content component
6679
const rawMdx = document.content.replace(/^---\s*[\r\n](.*?|\r|\n)---/, "").trim()
6780

@@ -78,24 +91,3 @@ const page = defineCollection({
7891
export default defineConfig({
7992
collections: [section, page],
8093
})
81-
82-
export const sectionOutputSchema = sectionSchema.extend({
83-
slug: z.string(),
84-
version: z.string(),
85-
_meta: z.object({ path: z.string() }).partial().optional(),
86-
})
87-
88-
export const pageOutputSchema = pageSchema.extend({
89-
slug: z.string(),
90-
section: z.string().optional(),
91-
version: z.string(),
92-
rawMdx: z.string(),
93-
content: z.unknown(),
94-
_meta: z.object({ path: z.string() }).partial().optional(),
95-
})
96-
97-
export type SectionRec = z.infer<typeof sectionOutputSchema>
98-
export type PageRec = z.infer<typeof pageOutputSchema>
99-
100-
export const SectionsArray = z.array(sectionOutputSchema)
101-
export const PagesArray = z.array(pageOutputSchema)

0 commit comments

Comments
 (0)