-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmdx.ts
More file actions
57 lines (51 loc) · 1.32 KB
/
Copy pathmdx.ts
File metadata and controls
57 lines (51 loc) · 1.32 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
53
54
55
56
57
import path from 'path';
import { bundleMDX } from 'mdx-bundler';
import { remarkMdxImages } from 'remark-mdx-images';
import rehypePrism from '@mapbox/rehype-prism';
import { ResourceWithContent } from 'Types/content';
const prepareMDX = async (
source: string,
options: {
resourcePath: string;
imagesDirectory: string;
},
): Promise<
ResourceWithContent<{
[key: string]: any;
}>
> => {
const { resourcePath, imagesDirectory } = options;
const { code, frontmatter } = await bundleMDX({
source,
cwd: resourcePath,
mdxOptions: (options) => ({
...options,
remarkPlugins: [...(options.remarkPlugins ?? []), remarkMdxImages],
rehypePlugins: [...(options.rehypePlugins ?? []), rehypePrism],
}),
esbuildOptions: (options) => ({
...options,
outdir: path.join(process.cwd(), 'public', imagesDirectory),
platform: 'node',
target: 'esnext',
loader: {
...options.loader,
'.png': 'file',
'.jpg': 'file',
'.gif': 'file',
},
publicPath: imagesDirectory,
write: true,
}),
globals: {
VideoInterlude: 'videoInterlude',
CodePenIframe: 'codePenIframe',
DistantButton: 'distantButton',
},
});
return {
content: code,
metaData: frontmatter,
};
};
export default prepareMDX;