Skip to content

Commit 0fcde60

Browse files
committed
fix: pass path to MDX file for remark plugins
1 parent b0a2d32 commit 0fcde60

File tree

3 files changed

+15
-18
lines changed

3 files changed

+15
-18
lines changed

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

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import type { ProcessorOptions } from "@mdx-js/mdx"
2-
3-
import { Node } from "gatsby"
2+
import type { IFileNode, IMdxNode } from "./types"
43

54
// Compiles MDX into JS
65
// This could be replaced by @mdx-js/mdx if MDX compile would
76
// accept custom data passed to the unified pipeline via processor.data()
87
export default async function compileMDX(
98
source: string,
10-
mdxNode: Node,
9+
mdxNode: IMdxNode,
10+
fileNode: IFileNode,
1111
options: ProcessorOptions
1212
): Promise<string> {
1313
const { createProcessor } = await import(`@mdx-js/mdx`)
@@ -18,11 +18,9 @@ export default async function compileMDX(
1818
// If we could pass this via MDX loader config, this whole custom loader might be obsolete.
1919
processor.data(`mdxNode`, mdxNode)
2020

21-
console.dir(`@todo add path`, { mdxNode })
22-
2321
const result = await processor.process(
2422
// Inject path to original file for remark plugins. See: https://github.com/gatsbyjs/gatsby/issues/26914
25-
new VFile({ value: source, path: `/at/todo/foo.mdx` })
23+
new VFile({ value: source, path: fileNode.absolutePath })
2624
)
2725

2826
return result.toString()

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

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,9 @@ const gatsbyMDXLoader: LoaderDefinition = async function (source) {
2323
return source
2424
}
2525

26-
const { mdxNode } = res
27-
28-
return compileMDX(
29-
source,
30-
mdxNode || {
31-
id: ``,
32-
children: [],
33-
parent: ``,
34-
internal: { contentDigest: ``, owner: ``, type: `` },
35-
},
36-
options
37-
)
26+
const { mdxNode, fileNode } = res
27+
28+
return compileMDX(source, mdxNode, fileNode, options)
3829
}
3930

4031
export default gatsbyMDXLoader

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,14 @@ export const preprocessSource: GatsbyNode["preprocessSource"] = async (
133133
parent: ``,
134134
internal: { contentDigest: ``, owner: ``, type: `` },
135135
},
136+
{
137+
id: ``,
138+
children: [],
139+
parent: ``,
140+
internal: { contentDigest: ``, owner: ``, type: `` },
141+
absolutePath: filename,
142+
sourceInstanceName: `mocked`,
143+
},
136144
mdxOptions
137145
)
138146

0 commit comments

Comments
 (0)