Skip to content

Commit

Permalink
fix: pass path to MDX file for remark plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
axe312ger committed May 31, 2022
1 parent b0a2d32 commit 0fcde60
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 18 deletions.
10 changes: 4 additions & 6 deletions packages/gatsby-plugin-mdx/src/compile-mdx.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import type { ProcessorOptions } from "@mdx-js/mdx"

import { Node } from "gatsby"
import type { IFileNode, IMdxNode } from "./types"

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

console.dir(`@todo add path`, { mdxNode })

const result = await processor.process(
// Inject path to original file for remark plugins. See: https://github.com/gatsbyjs/gatsby/issues/26914
new VFile({ value: source, path: `/at/todo/foo.mdx` })
new VFile({ value: source, path: fileNode.absolutePath })
)

return result.toString()
Expand Down
15 changes: 3 additions & 12 deletions packages/gatsby-plugin-mdx/src/gatsby-mdx-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,9 @@ const gatsbyMDXLoader: LoaderDefinition = async function (source) {
return source
}

const { mdxNode } = res

return compileMDX(
source,
mdxNode || {
id: ``,
children: [],
parent: ``,
internal: { contentDigest: ``, owner: ``, type: `` },
},
options
)
const { mdxNode, fileNode } = res

return compileMDX(source, mdxNode, fileNode, options)
}

export default gatsbyMDXLoader
8 changes: 8 additions & 0 deletions packages/gatsby-plugin-mdx/src/gatsby-node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,14 @@ export const preprocessSource: GatsbyNode["preprocessSource"] = async (
parent: ``,
internal: { contentDigest: ``, owner: ``, type: `` },
},
{
id: ``,
children: [],
parent: ``,
internal: { contentDigest: ``, owner: ``, type: `` },
absolutePath: filename,
sourceInstanceName: `mocked`,
},
mdxOptions
)

Expand Down

0 comments on commit 0fcde60

Please sign in to comment.