MCPcopy Create free account
hub / github.com/contentlayerdev/contentlayer / bundleMDX

Function bundleMDX

packages/@contentlayer/core/src/markdown/mdx.ts:12–84  ·  view source on GitHub ↗
({
  mdxString,
  options,
  contentDirPath,
  rawDocumentData,
}: {
  mdxString: string
  options?: MDXOptions
  contentDirPath: string
  rawDocumentData: RawDocumentData
})

Source from the content-addressed store, hash-verified

10import { addRawDocumentToVFile } from './unified.js'
11
12export const bundleMDX = ({
13 mdxString,
14 options,
15 contentDirPath,
16 rawDocumentData,
17}: {
18 mdxString: string
19 options?: MDXOptions
20 contentDirPath: string
21 rawDocumentData: RawDocumentData
22}): T.Effect<OT.HasTracer, UnexpectedMDXError, string> =>
23 pipe(
24 T.gen(function* ($) {
25 // TODO should be fixed in `mdx-bundler`
26 if (mdxString.length === 0) {
27 return ''
28 }
29 const {
30 rehypePlugins,
31 remarkPlugins,
32 resolveCwd,
33 cwd: cwd_,
34 mdxOptions: mapMdxOptions,
35 esbuildOptions: mapEsbuildOptions,
36 ...restOptions
37 } = options ?? {}
38
39 const getCwdFromContentDirPath = () =>
40 // TODO don't use `process.cwd()` but instead `HasCwd`
41 path.isAbsolute(contentDirPath) ? contentDirPath : path.join(process.cwd(), contentDirPath)
42
43 const getRelativeCwd = () => path.join(getCwdFromContentDirPath(), path.dirname(rawDocumentData.flattenedPath))
44
45 const getCwd = () => (resolveCwd === 'contentDirPath' ? getCwdFromContentDirPath() : getRelativeCwd())
46
47 // TODO when fixed https://github.com/kentcdodds/mdx-bundler/pull/206
48 if (process.env.NODE_ENV === undefined) {
49 process.env.NODE_ENV = 'development'
50 }
51
52 const mdxOptions: BundleMDXOptions<any> = {
53 mdxOptions: (opts) => {
54 opts.rehypePlugins = [...(opts.rehypePlugins ?? []), ...(rehypePlugins ?? [])]
55 opts.remarkPlugins = [
56 addRawDocumentToVFile(rawDocumentData),
57 ...(opts.remarkPlugins ?? []),
58 ...(remarkPlugins ?? []),
59 ]
60 return mapMdxOptions ? mapMdxOptions(opts) : opts
61 },
62 // User-provided cwd trumps resolution
63 cwd: cwd_ ?? getCwd(),
64 esbuildOptions: (opts, frontmatter) => {
65 // NOTE this is needed to avoid `esbuild` from logging a warning regarding the `tsconfig.json` target option not being used
66 opts.target = 'es2020'
67 return mapEsbuildOptions ? mapEsbuildOptions(opts, frontmatter) : opts
68 },
69 // NOTE `restOptions` should be spread at the end to allow for user overrides

Callers 1

mdxToHtmlFunction · 0.85

Calls 2

addRawDocumentToVFileFunction · 0.85
getCwdFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…