-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcontent-collections.ts
119 lines (114 loc) · 3.46 KB
/
content-collections.ts
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import { defineCollection, defineConfig } from "@content-collections/core";
import { compileMDX } from "@content-collections/mdx";
// MDX Plugins:
import {
remarkGfm,
remarkHeading,
remarkStructure,
} from "fumadocs-core/mdx-plugins";
import GithubSlugger from "github-slugger";
import rehypePrettyCode, { type Options } from "rehype-pretty-code";
import rehypeAutolinkHeadings from "rehype-autolink-headings";
import { visit } from "unist-util-visit";
import { HEADING_LINK_ANCHOR } from "./src/ui/prose-headings";
// Rehype Shiki Options:
const rehypePrettyOptions: Options = {
theme: {
dark: "github-dark",
light: "github-light",
},
keepBackground: false,
bypassInlineCode: true,
};
// Domain:
const domain = "toast.pheralb.dev";
// Docs Collection:
const docs = defineCollection({
name: "docs",
directory: "src/docs",
include: "**/*.mdx",
schema: (z) => ({
title: z.string(),
description: z.string(),
category: z.string(),
}),
transform: async (document, context) => {
const mdx = await compileMDX(context, document, {
remarkPlugins: [remarkGfm, remarkHeading, remarkStructure],
rehypePlugins: [
// (1a) Add rawstring to pre element, to be used in the copy button:
() => (tree) => {
visit(tree, (node) => {
if (node?.type === "element" && node?.tagName === "pre") {
const [codeEl] = node.children;
if (codeEl.tagName !== "code") return;
node.rawstring = codeEl.children?.[0].value;
}
});
},
// (1b) Render Code Blocks with Rehype Pretty Code:
[rehypePrettyCode, rehypePrettyOptions],
// (2) Add rawstring to pre element, to be used in the copy button:
() => (tree) => {
visit(tree, (node) => {
if (node?.type === "element" && node?.tagName === "figure") {
if (!("data-rehype-pretty-code-figure" in node.properties)) {
return;
}
const preElement = node.children.at(-1);
if (preElement.tagName !== "pre") {
return;
}
preElement.properties["rawstring"] = node.rawstring;
}
});
},
// Open External Links in New Tab:
() => (tree) => {
visit(tree, "element", (e) => {
if (
e.tagName === "a" &&
e.properties?.href &&
e.properties.href.toString().startsWith("http") &&
!e.properties.href.toString().includes(domain)
) {
e.properties!["target"] = "_blank";
}
});
},
[
rehypeAutolinkHeadings,
{
behavior: "wrap",
properties: {
className: [HEADING_LINK_ANCHOR],
},
},
],
],
});
const slugger = new GithubSlugger();
const regXHeader = /\n(?<flag>#+)\s+(?<content>.+)/g;
const tableOfContents = Array.from(
document.content.matchAll(regXHeader),
).map(({ groups }) => {
const flag = groups?.flag;
const content = groups?.content;
return {
level: flag?.length,
text: content,
slug: content ? slugger.slug(content) : undefined,
};
});
return {
...document,
mdx,
slug: document._meta.path,
url: `/${document._meta.path}`,
tableOfContents,
};
},
});
export default defineConfig({
collections: [docs],
});