|
1 | | -import { defineCollection, defineConfig } from "@content-collections/core"; |
2 | | -import { compileMDX } from "@content-collections/mdx"; |
3 | | -import path from "node:path"; |
4 | | -import fs from "node:fs/promises"; |
| 1 | +import { z } from "zod"; |
5 | 2 |
|
6 | | -// MDX Plugins: |
7 | 3 | import { |
8 | | - remarkGfm, |
9 | | - remarkHeading, |
10 | | - remarkStructure, |
11 | | -} from "fumadocs-core/mdx-plugins"; |
12 | | -import GithubSlugger from "github-slugger"; |
| 4 | + defineCollection, |
| 5 | + defineConfig, |
| 6 | + type Context, |
| 7 | + type Document, |
| 8 | +} from "@content-collections/core"; |
| 9 | + |
| 10 | +// Plugins: |
| 11 | +import remarkGfm from "remark-gfm"; |
| 12 | +import rehypeSlug from "rehype-slug"; |
| 13 | +import rehypeShiki from "@shikijs/rehype/core"; |
| 14 | + |
| 15 | +import { compileMDX } from "@content-collections/mdx"; |
13 | 16 | import rehypeAutolinkHeadings from "rehype-autolink-headings"; |
14 | | -import { visit } from "unist-util-visit"; |
15 | | -import rehypeShiki, { type RehypeShikiOptions } from "@shikijs/rehype"; |
16 | | -import { rehypeComponent } from "./src/mdx/rehypeComponent"; |
17 | 17 |
|
18 | | -// Domain: |
19 | | -const domain = "codeblocks.pheralb.dev"; |
| 18 | +import { highlight } from "./src/utils/shiki"; |
| 19 | +import { rehypeShikiOptions } from "./src/mdx/plugins/rehypeShiki"; |
| 20 | +import { getTableOfContents } from "./src/mdx/plugins/generateToC"; |
| 21 | +import { rehypeComponent } from "./src/mdx/plugins/rehypeComponent"; |
| 22 | +import { rehypeReactDoc } from "./src/mdx/plugins/rehypeReactDoc"; |
| 23 | +import { HEADING_LINK_ANCHOR } from "./src/components/ui/headings"; |
| 24 | + |
| 25 | +// Schema: |
| 26 | +const docSchema = z.object({ |
| 27 | + title: z.string(), |
| 28 | + description: z.string(), |
| 29 | + category: z.string(), |
| 30 | + content: z.string(), |
| 31 | +}); |
| 32 | + |
| 33 | +type DocSchema = z.infer<typeof docSchema>; |
| 34 | +type DocsDocument = Document & DocSchema; |
20 | 35 |
|
21 | | -// Shiki Options: |
22 | | -const shikiOptions: RehypeShikiOptions = { |
23 | | - themes: { |
24 | | - light: "github-light", |
25 | | - dark: "github-dark", |
26 | | - }, |
27 | | - transformers: [ |
28 | | - { |
29 | | - name: "AddPreProperties", |
30 | | - pre(node) { |
31 | | - node.properties["data-language"] = this.options.lang || "plaintext"; |
32 | | - node.properties["data-code"] = this.source; |
33 | | - }, |
34 | | - }, |
35 | | - { |
36 | | - name: "WordWrap", |
37 | | - pre(node) { |
38 | | - node.properties["style"] = "white-space: pre-wrap;"; |
39 | | - }, |
40 | | - }, |
41 | | - ], |
| 36 | +// Transform: |
| 37 | +const docTransform = async ( |
| 38 | + folder: string, |
| 39 | + document: DocsDocument, |
| 40 | + context: Context, |
| 41 | +) => { |
| 42 | + const highlighter = await highlight(); |
| 43 | + const tableOfContents = getTableOfContents(document.content); |
| 44 | + const mdx = await compileMDX(context, document, { |
| 45 | + remarkPlugins: [remarkGfm], |
| 46 | + rehypePlugins: [ |
| 47 | + rehypeComponent, |
| 48 | + rehypeSlug, |
| 49 | + [ |
| 50 | + rehypeAutolinkHeadings, |
| 51 | + { |
| 52 | + behavior: "wrap", |
| 53 | + properties: { |
| 54 | + className: [HEADING_LINK_ANCHOR], |
| 55 | + }, |
| 56 | + }, |
| 57 | + ], |
| 58 | + rehypeReactDoc, |
| 59 | + [rehypeShiki, highlighter, rehypeShikiOptions], |
| 60 | + ], |
| 61 | + }); |
| 62 | + return { |
| 63 | + ...document, |
| 64 | + folder, |
| 65 | + tableOfContents, |
| 66 | + mdx, |
| 67 | + }; |
42 | 68 | }; |
43 | 69 |
|
44 | | -// Docs Collection: |
45 | | -const docs = defineCollection({ |
46 | | - name: "docs", |
| 70 | +// Collections: |
| 71 | +const generalDocs = defineCollection({ |
| 72 | + name: "general", |
47 | 73 | directory: "src/docs", |
48 | 74 | include: "**/*.mdx", |
49 | | - schema: (z) => ({ |
50 | | - title: z.string(), |
51 | | - description: z.string(), |
52 | | - category: z.string(), |
53 | | - }), |
54 | | - transform: async (document, context) => { |
55 | | - const filePath = path.join( |
56 | | - context.collection.directory, |
57 | | - document._meta.filePath, |
58 | | - ); |
59 | | - const { mtimeMs, birthtimeMs } = await fs.stat(filePath); |
60 | | - const mdx = await compileMDX(context, document, { |
61 | | - remarkPlugins: [remarkGfm, remarkHeading, remarkStructure], |
62 | | - rehypePlugins: [ |
63 | | - // Rehype Component: |
64 | | - rehypeComponent, |
65 | | - // Shiki Syntax Highlighting: |
66 | | - [rehypeShiki, shikiOptions], |
67 | | - // Open External Links in New Tab: |
68 | | - () => (tree) => { |
69 | | - visit(tree, "element", (e) => { |
70 | | - if ( |
71 | | - e.tagName === "a" && |
72 | | - e.properties?.href && |
73 | | - e.properties.href.toString().startsWith("http") && |
74 | | - !e.properties.href.toString().includes(domain) |
75 | | - ) { |
76 | | - e.properties!["target"] = "_blank"; |
77 | | - } |
78 | | - }); |
79 | | - }, |
80 | | - [rehypeAutolinkHeadings], |
81 | | - ], |
82 | | - }); |
83 | | - const slugger = new GithubSlugger(); |
84 | | - const regXHeader = /(?:^|\n)(?<flag>##+)\s+(?<content>.+)/g; |
85 | | - const tableOfContents = Array.from( |
86 | | - document.content.matchAll(regXHeader), |
87 | | - ).map(({ groups }) => { |
88 | | - const flag = groups?.flag; |
89 | | - const content = groups?.content; |
90 | | - return { |
91 | | - level: flag?.length, |
92 | | - text: content, |
93 | | - slug: content ? slugger.slug(content) : undefined, |
94 | | - }; |
95 | | - }); |
96 | | - return { |
97 | | - ...document, |
98 | | - mdx, |
99 | | - slug: document._meta.path, |
100 | | - url: `/${document._meta.path}`, |
101 | | - toc: tableOfContents, |
102 | | - createdAt: new Date(birthtimeMs), |
103 | | - updatedAt: new Date(mtimeMs), |
104 | | - }; |
105 | | - }, |
| 75 | + schema: docSchema, |
| 76 | + transform: (document, context) => docTransform("general", document, context), |
| 77 | +}); |
| 78 | + |
| 79 | +const gstartedDocs = defineCollection({ |
| 80 | + name: "gstarted", |
| 81 | + directory: "src/docs/getting-started", |
| 82 | + include: "**/*.mdx", |
| 83 | + schema: docSchema, |
| 84 | + transform: (document, context) => |
| 85 | + docTransform("getting-started", document, context), |
| 86 | +}); |
| 87 | + |
| 88 | +const componentsDocs = defineCollection({ |
| 89 | + name: "components", |
| 90 | + directory: "src/docs/components", |
| 91 | + include: "**/*.mdx", |
| 92 | + schema: docSchema, |
| 93 | + transform: (document, context) => |
| 94 | + docTransform("components", document, context), |
| 95 | +}); |
| 96 | + |
| 97 | +const shikiDocs = defineCollection({ |
| 98 | + name: "shiki", |
| 99 | + directory: "src/docs/shiki", |
| 100 | + include: "**/*.mdx", |
| 101 | + schema: docSchema, |
| 102 | + transform: (document, context) => docTransform("shiki", document, context), |
106 | 103 | }); |
107 | 104 |
|
108 | 105 | export default defineConfig({ |
109 | | - collections: [docs], |
| 106 | + collections: [generalDocs, gstartedDocs, componentsDocs, shikiDocs], |
110 | 107 | }); |
0 commit comments