-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontent-collections.ts
More file actions
101 lines (90 loc) · 2.6 KB
/
content-collections.ts
File metadata and controls
101 lines (90 loc) · 2.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import { posix } from "node:path"
import { defineCollection, defineConfig } from "@content-collections/core"
import { compileMDX } from "@content-collections/mdx"
import rehypeSlug from "rehype-slug"
import { z } from "zod"
const sectionSchema = z.object({
title: z.string(),
})
const pageSchema = z.object({
title: z.string(),
summary: z.string(),
description: z.string(),
})
const metaSchema = z.object({
filePath: z.string(),
fileName: z.string(),
directory: z.string(),
path: z.string(),
extension: z.string(),
})
const outputBaseSchema = z.object({
slug: z.string(),
_meta: metaSchema,
})
const sectionOutputSchema = sectionSchema.extend(outputBaseSchema.shape)
const pageOutputSchema = pageSchema.extend({
...outputBaseSchema.shape,
section: z.string().optional(),
rawMdx: z.string(),
content: z.string(),
})
export type Section = z.infer<typeof sectionOutputSchema>
export type Page = z.infer<typeof pageOutputSchema>
/**
* Removes leading number prefixes like "01-", "02-" from each path segment.
*/
const cleanSlug = (p: string) =>
toPosix(p)
.split("/")
.filter(Boolean)
.map((seg) => seg.replace(/^\d{2,}-/, ""))
.join("/")
const toPosix = (s: string) => {
return posix.normalize(s.replace(/\\/g, "/"))
}
const stripExt = (s: string) => s.replace(/\.(md|mdx)$/i, "")
const stripTrailingIndex = (s: string) => s.replace(/\/index$/i, "")
/*
* This collection defines a documentation section shown in the sidebar of the package documentation.
*
* Each section is represented by a directory in the `content` folder and must contain an `index.md` file
* with metadata (title).
*/
const section = defineCollection({
name: "section",
directory: "content",
include: "**/index.md",
schema: sectionSchema,
transform: (document) => {
const slug = stripTrailingIndex(cleanSlug(document._meta.path))
return { ...document, slug }
},
})
/*
* This collection defines an individual documentation page within the package documentation.
*
* Pages are `.mdx` files located inside section folders or their subdirectories.
*/
const page = defineCollection({
name: "page",
directory: "content",
include: "**/**/*.mdx",
schema: pageSchema,
transform: async (document, context) => {
const cleanedSlug = cleanSlug(document._meta.path)
const slug = stripExt(cleanedSlug)
const content = await compileMDX(context, document, { rehypePlugins: [rehypeSlug] })
const rawMdx = document.content.replace(/^---\s*[\r\n](.*?|\r|\n)---/, "").trim()
return {
...document,
content,
slug,
section: slug.split("/")[0] ?? "",
rawMdx,
}
},
})
export default defineConfig({
collections: [section, page],
})