-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontent-collections.ts
More file actions
52 lines (46 loc) · 1.68 KB
/
Copy pathcontent-collections.ts
File metadata and controls
52 lines (46 loc) · 1.68 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
import { defineCollection, defineConfig } from '@content-collections/core';
import { compileMDX } from '@content-collections/mdx';
import { slugify } from '@/lib/utils';
import { z } from 'zod';
import { rehypeParseCodeBlocks } from '@/shiki-rehype.mjs';
import rehypeAutolinkHeadings from 'rehype-autolink-headings';
// for more information on configuration, visit:
// https://www.content-collections.dev/docs/configuration
const defaultPaths = [
{ name: 'algorithms', directory: 'src/paths/algorithms' },
{ name: 'dataStructures', directory: 'src/paths/data-structures' },
];
const collections = defaultPaths.map((path) => {
return defineCollection({
name: path.name,
directory: path.directory,
include: ['**/*.mdx'],
schema: z.object({
title: z.string(),
summary: z.string(),
author: z.string(),
date: z.coerce.date(),
segments: z.optional(z.array(z.string())),
}),
transform: async (document, context) => {
const mdx = await compileMDX(context, document, {
rehypePlugins: [
rehypeParseCodeBlocks,
[rehypeAutolinkHeadings, { properties: { className: ['anchor'] } }],
],
});
const regXHeader = /^(?:[\n\r]|)(?<flag>#{1,6})\s+(?<content>.+)/gm;
const headings = Array.from(document.content.matchAll(regXHeader)).map(({ groups }) => {
const flag = groups?.flag;
const content = groups?.content;
return {
level: flag?.length,
text: content,
slug: slugify(content ?? '#'),
};
});
return { ...document, headings, mdx, segments: document._meta.path.split('/') };
},
});
});
export default defineConfig({ collections });