Skip to content

Commit 36f6b4c

Browse files
committed
refactor: expose compileMDX API
1 parent 0a90bf9 commit 36f6b4c

File tree

4 files changed

+63
-59
lines changed

4 files changed

+63
-59
lines changed

packages/gatsby-plugin-mdx/src/compile-mdx.ts

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
1+
import type { NodePluginArgs } from "gatsby"
12
import type { ProcessorOptions } from "@mdx-js/mdx"
23
import type { IFileNode, IMdxMetadata, IMdxNode } from "./types"
34

5+
import deepmerge from "deepmerge"
6+
7+
import { enhanceMdxOptions, IMdxPluginOptions } from "./plugin-options"
8+
49
// Compiles MDX into JS
510
// This could be replaced by @mdx-js/mdx if MDX compile would
611
// accept custom data passed to the unified pipeline via processor.data()
7-
export default async function compileMDX(
12+
export async function compileMDX(
813
mdxNode: IMdxNode,
914
fileNode: IFileNode,
1015
options: ProcessorOptions
@@ -49,3 +54,55 @@ export default async function compileMDX(
4954
throw err
5055
}
5156
}
57+
58+
/**
59+
* This helper function allows you to inject additional plugins and configuration into the MDX
60+
* compilation pipeline. Very useful to create your own resolvers that return custom metadata.
61+
* Internally used to generate the tables of contents and the excerpts.
62+
*/
63+
export const compileMDXWithCustomOptions = async ({
64+
pluginOptions,
65+
customOptions,
66+
getNode,
67+
getNodesByType,
68+
pathPrefix,
69+
reporter,
70+
cache,
71+
mdxNode,
72+
}: {
73+
pluginOptions: IMdxPluginOptions
74+
customOptions: Partial<IMdxPluginOptions>
75+
getNode: NodePluginArgs["getNode"]
76+
getNodesByType: NodePluginArgs["getNodesByType"]
77+
pathPrefix: string
78+
reporter: NodePluginArgs["reporter"]
79+
cache: NodePluginArgs["cache"]
80+
mdxNode: IMdxNode
81+
}): Promise<{
82+
processedMDX: string
83+
metadata: IMdxMetadata
84+
} | null> => {
85+
const customPluginOptions = deepmerge(
86+
Object.assign({}, pluginOptions),
87+
customOptions
88+
)
89+
90+
// Prepare MDX compile
91+
const mdxOptions = await enhanceMdxOptions(customPluginOptions, {
92+
getNode,
93+
getNodesByType,
94+
pathPrefix,
95+
reporter,
96+
cache,
97+
})
98+
if (!mdxNode.parent) {
99+
return null
100+
}
101+
const fileNode = getNode(mdxNode.parent)
102+
if (!fileNode) {
103+
return null
104+
}
105+
106+
// Compile MDX and extract metadata
107+
return compileMDX(mdxNode, fileNode, mdxOptions)
108+
}

packages/gatsby-plugin-mdx/src/gatsby-mdx-loader.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import type { NodeMap } from "./types"
55

66
import { getOptions } from "loader-utils"
77

8-
import compileMDX from "./compile-mdx"
8+
import { compileMDX } from "./compile-mdx"
99

1010
export interface IGatsbyMDXLoaderOptions {
1111
options: ProcessorOptions

packages/gatsby-plugin-mdx/src/gatsby-node.ts

Lines changed: 3 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,20 @@
1-
import type { GatsbyNode, NodeInput, NodePluginArgs } from "gatsby"
1+
import type { GatsbyNode, NodeInput } from "gatsby"
22
import type { FileSystemNode } from "gatsby-source-filesystem"
33
import type { Options } from "rehype-infer-description-meta"
4-
import type { IFileNode, IMdxMetadata, IMdxNode, NodeMap } from "./types"
4+
import type { IFileNode, NodeMap } from "./types"
55

66
import path from "path"
77
import { sentenceCase } from "change-case"
88
import fs from "fs-extra"
99
import grayMatter from "gray-matter"
10-
import deepmerge from "deepmerge"
1110

1211
import {
1312
defaultOptions,
1413
enhanceMdxOptions,
1514
IMdxPluginOptions,
1615
} from "./plugin-options"
1716
import { IGatsbyLayoutLoaderOptions } from "./gatsby-layout-loader"
18-
import compileMDX from "./compile-mdx"
17+
import { compileMDX, compileMDXWithCustomOptions } from "./compile-mdx"
1918
import { IGatsbyMDXLoaderOptions } from "./gatsby-mdx-loader"
2019
import remarkInferTocMeta from "./remark-infer-toc-meta"
2120

@@ -145,58 +144,6 @@ export const preprocessSource: GatsbyNode["preprocessSource"] = async (
145144
return processedMDX.toString()
146145
}
147146

148-
/**
149-
* This helper function allows you to inject additional plugins and configuration into the MDX
150-
* compilation pipeline. Very useful to create your own resolvers that return custom metadata.
151-
* Internally used to generate the tables of contents and the excerpts.
152-
*/
153-
export const compileMDXWithCustomOptions = async ({
154-
pluginOptions,
155-
customOptions,
156-
getNode,
157-
getNodesByType,
158-
pathPrefix,
159-
reporter,
160-
cache,
161-
mdxNode,
162-
}: {
163-
pluginOptions: IMdxPluginOptions
164-
customOptions: Partial<IMdxPluginOptions>
165-
getNode: NodePluginArgs["getNode"]
166-
getNodesByType: NodePluginArgs["getNodesByType"]
167-
pathPrefix: string
168-
reporter: NodePluginArgs["reporter"]
169-
cache: NodePluginArgs["cache"]
170-
mdxNode: IMdxNode
171-
}): Promise<{
172-
processedMDX: string
173-
metadata: IMdxMetadata
174-
} | null> => {
175-
const customPluginOptions = deepmerge(
176-
Object.assign({}, pluginOptions),
177-
customOptions
178-
)
179-
180-
// Prepare MDX compile
181-
const mdxOptions = await enhanceMdxOptions(customPluginOptions, {
182-
getNode,
183-
getNodesByType,
184-
pathPrefix,
185-
reporter,
186-
cache,
187-
})
188-
if (!mdxNode.parent) {
189-
return null
190-
}
191-
const fileNode = getNode(mdxNode.parent)
192-
if (!fileNode) {
193-
return null
194-
}
195-
196-
// Compile MDX and extract metadata
197-
return compileMDX(mdxNode, fileNode, mdxOptions)
198-
}
199-
200147
export const createSchemaCustomization: GatsbyNode["createSchemaCustomization"] =
201148
async (
202149
{ getNode, getNodesByType, pathPrefix, reporter, cache, actions, schema },
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
// noop
1+
export * from "./compile-mdx"

0 commit comments

Comments
 (0)