|
| 1 | +import { |
| 2 | + defineCollection, |
| 3 | + defineConfig, |
| 4 | + Context, |
| 5 | + Document, |
| 6 | +} from "@content-collections/core"; |
| 7 | +import rehypeAutolinkHeadings from "rehype-autolink-headings"; |
| 8 | +import rehypePrettyCode from "rehype-pretty-code"; |
| 9 | +import rehypeSlug from "rehype-slug"; |
| 10 | +import remarkGfm from "remark-gfm"; |
| 11 | +import { visit } from "unist-util-visit"; |
| 12 | +import { compileMDX } from "@content-collections/mdx"; |
| 13 | + |
| 14 | +type Transformed = { |
| 15 | + _id: string |
| 16 | + body: string; |
| 17 | + slug: string; |
| 18 | + images: string[]; |
| 19 | + slugAsParams: string; |
| 20 | +} |
| 21 | + |
| 22 | +const transform = async <T extends Document & { content: string }>( |
| 23 | + doc: T, |
| 24 | + context: Context |
| 25 | +): Promise<T & Transformed> => { |
| 26 | + const body = await compileMDX(context, doc, { |
| 27 | + remarkPlugins: [remarkGfm], |
| 28 | + rehypePlugins: [ |
| 29 | + rehypeSlug, |
| 30 | + () => (tree) => { |
| 31 | + visit(tree, (node) => { |
| 32 | + if (node?.type === "element" && node?.tagName === "pre") { |
| 33 | + const [codeEl] = node.children; |
| 34 | + if (codeEl.tagName !== "code") return; |
| 35 | + node.rawString = codeEl.children?.[0].value; |
| 36 | + } |
| 37 | + }); |
| 38 | + }, |
| 39 | + [ |
| 40 | + rehypePrettyCode, |
| 41 | + { |
| 42 | + theme: "github-dark", |
| 43 | + keepBackground: false, |
| 44 | + onVisitLine(node) { |
| 45 | + // Prevent lines from collapsing in `display: grid` mode, and allow empty lines to be copy/pasted |
| 46 | + if (node.children.length === 0) { |
| 47 | + node.children = [{ type: "text", value: " " }]; |
| 48 | + } |
| 49 | + }, |
| 50 | + }, |
| 51 | + ], |
| 52 | + () => (tree) => { |
| 53 | + visit(tree, (node) => { |
| 54 | + if (node?.type === "element" && node?.tagName === "figure") { |
| 55 | + if (!("data-rehype-pretty-code-figure" in node.properties)) { |
| 56 | + return; |
| 57 | + } |
| 58 | + const preElement = node.children.at(-1); |
| 59 | + if (preElement.tagName !== "pre") { |
| 60 | + return; |
| 61 | + } |
| 62 | + preElement.properties["rawString"] = node.rawString; |
| 63 | + } |
| 64 | + }); |
| 65 | + }, |
| 66 | + [ |
| 67 | + rehypeAutolinkHeadings, |
| 68 | + { |
| 69 | + properties: { |
| 70 | + className: ["subheading-anchor"], |
| 71 | + ariaLabel: "Link to section", |
| 72 | + }, |
| 73 | + }, |
| 74 | + ], |
| 75 | + ], |
| 76 | + }); |
| 77 | + |
| 78 | + const imageMatches = |
| 79 | + doc.content.match(/(?<=<Image[^>]\bsrc=")[^"]+(?="[^>]\/>)/g) || []; |
| 80 | + |
| 81 | + const rootPath = context.collection.directory.split("/").slice(1).join("/") |
| 82 | + |
| 83 | + const transformedDoc: T & Transformed = { |
| 84 | + ...doc, |
| 85 | + body, |
| 86 | + images: imageMatches, |
| 87 | + _id: doc._meta.filePath, |
| 88 | + slugAsParams: doc._meta.path, |
| 89 | + slug: `/${rootPath}/${doc._meta.path}` |
| 90 | + }; |
| 91 | + |
| 92 | + return transformedDoc; |
| 93 | +}; |
| 94 | + |
| 95 | +const Page = defineCollection({ |
| 96 | + name: "Page", |
| 97 | + directory: "content/pages", |
| 98 | + include: "**/*.mdx", |
| 99 | + schema: (z) => ({ |
| 100 | + title: z.string(), |
| 101 | + description: z.string().optional(), |
| 102 | + }), |
| 103 | + transform, |
| 104 | +}); |
| 105 | + |
| 106 | +const Doc = defineCollection({ |
| 107 | + name: "Doc", |
| 108 | + directory: "content/docs", |
| 109 | + include: "**/*.mdx", |
| 110 | + schema: (z) => ({ |
| 111 | + title: z.string(), |
| 112 | + description: z.string().optional(), |
| 113 | + published: z.boolean().default(true), |
| 114 | + }), |
| 115 | + transform, |
| 116 | +}); |
| 117 | + |
| 118 | +const Guide = defineCollection({ |
| 119 | + name: "Guide", |
| 120 | + directory: "content/guides", |
| 121 | + include: "**/*.mdx", |
| 122 | + schema: (z) => ({ |
| 123 | + title: z.string(), |
| 124 | + description: z.string().optional(), |
| 125 | + date: z.string(), |
| 126 | + published: z.boolean().default(true), |
| 127 | + featured: z.boolean().default(false), |
| 128 | + }), |
| 129 | + transform, |
| 130 | +}); |
| 131 | + |
| 132 | +const Post = defineCollection({ |
| 133 | + name: "Post", |
| 134 | + directory: "content/blog", |
| 135 | + include: "**/*.mdx", |
| 136 | + schema: (z) => ({ |
| 137 | + title: z.string(), |
| 138 | + description: z.string().optional(), |
| 139 | + date: z.string(), |
| 140 | + published: z.boolean().default(true), |
| 141 | + image: z.string(), |
| 142 | + authors: z.array(z.string()), |
| 143 | + categories: z.array(z.enum(["news", "education"])).default(["news"]), |
| 144 | + related: z.array(z.string()).optional(), |
| 145 | + }), |
| 146 | + transform, |
| 147 | +}); |
| 148 | + |
| 149 | +export default defineConfig({ |
| 150 | + collections: [Page, Doc, Guide, Post], |
| 151 | +}); |
0 commit comments