-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathnext.config.mjs
184 lines (159 loc) · 5.32 KB
/
next.config.mjs
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
import fs from 'fs';
import path from 'path';
import { visit } from 'unist-util-visit';
import { u } from 'unist-builder';
import remarkGfm from 'remark-gfm';
import createMDX from '@next/mdx';
import { remarkCodeHike, recmaCodeHike } from 'codehike/mdx';
import rehypeSlug from 'rehype-slug';
// Import the JSON file
import docsData from './configs/docs.json' assert { type: 'json' };
// import dataArray from './configs/docs';
const { dataArray } = docsData;
const chConfig = {
components: { code: 'PreCode' },
};
function rehypeComponent() {
return (tree) => {
visit(tree, (node) => {
if (node.type === 'mdxJsxFlowElement' && node.name === 'TabCodePreview') {
// console.log('Found CodePreview element:', node);
const nameAttribute = node.attributes.find(
(attr) => attr.name === 'name'
);
const name = nameAttribute ? nameAttribute.value : null;
// console.log('CodePreview name:', name);
if (!name) {
console.log('No name found for TabCodePreview');
return;
}
try {
const currentComponentData = dataArray.reduce((acc, section) => {
const component = section.componentArray.find(
(comp) => comp.componentName === name
);
if (component) {
return component;
}
return acc;
}, null);
if (!currentComponentData) {
console.error(`Component not found: ${name}`);
return;
}
// console.log('Found component data:', currentComponentData);
const filesContent =
currentComponentData.filesArray?.map((file) => {
const filePath = path.join(process.cwd(), file.filesrc);
const source = fs.readFileSync(filePath, 'utf8');
return {
name: file.name,
content: source,
path: file.filesrc,
componentName: currentComponentData.componentName,
};
}) || [];
// console.log(filesContent);
// console.log('Files content:', filesContent);
// console.log('checking inner node:', node.children);
node.children = filesContent.map((file) =>
u('element', {
tagName: 'PreCode',
properties: {
codeblock: JSON.stringify(file.content), // Stringify the content
filename: file.name,
componentname: file.componentName,
},
children: node.children,
})
);
// console.log('Updated node children:', node.children);
} catch (error) {
console.error(`Error processing component ${name}:`, error);
}
}
if (
node.type === 'mdxJsxFlowElement' &&
node.name === 'DrawerCodePreview'
) {
// console.log('Found CodePreview element:', node);
const nameAttribute = node.attributes.find(
(attr) => attr.name === 'name'
);
const name = nameAttribute ? nameAttribute.value : null;
// console.log('CodePreview name:', node, node.children);
if (!name) {
console.log('No name found for CodePreview');
return;
}
try {
const currentComponentData = dataArray.reduce((acc, section) => {
const component = section.componentArray.find(
(comp) => comp.componentName === name
);
if (component) {
return component;
}
return acc;
}, null);
if (!currentComponentData) {
console.error(`Component not found: ${name}`);
return;
}
// console.log('Found component data:', currentComponentData);
const filePath = path.join(
process.cwd(),
`./registry/${currentComponentData.filesrc}`
);
const source = fs.readFileSync(filePath, 'utf8');
// console.log('Files content:', filesContent);
node.children = [
u('element', {
tagName: 'PreCode',
properties: {
codeblock: JSON.stringify(source), // Stringify the content
comName: currentComponentData.componentName,
filesrc: currentComponentData.filesrc,
},
children: node.children,
}),
];
// console.log('Updated node children:', node.children);
} catch (error) {
console.error(`Error processing component ${name}:`, error);
}
}
});
};
}
const withMDX = createMDX({
extension: /\.mdx?$/,
options: {
remarkPlugins: [remarkGfm, [remarkCodeHike, chConfig]],
recmaPlugins: [[recmaCodeHike, chConfig]],
rehypePlugins: [rehypeSlug, rehypeComponent],
jsx: true,
},
});
/** @type {import('next').NextConfig} */
const nextConfig = {
pageExtensions: ['js', 'jsx', 'ts', 'tsx', 'md', 'mdx'],
images: {
remotePatterns: [
{
hostname: 'avatars.githubusercontent.com',
},
{
hostname: 'res.cloudinary.com',
},
{
hostname: 'images.unsplash.com',
},
{
hostname: 'img.freepik.com',
},
],
},
// Add other Next.js config options here
};
export default withMDX(nextConfig);