diff --git a/e2e-tests/mdx/gatsby-config.js b/e2e-tests/mdx/gatsby-config.js index 694c6c5baa01e..1a58208423cb0 100644 --- a/e2e-tests/mdx/gatsby-config.js +++ b/e2e-tests/mdx/gatsby-config.js @@ -17,13 +17,27 @@ module.exports = { path: `${__dirname}/src/posts`, }, }, - {resolve: `gatsby-plugin-mdx`, - options: { - extensions: [`.mdx`, `.md`], - defaultLayouts: { - default: require.resolve("./src/components/layout.js"), + { + resolve: `gatsby-plugin-mdx`, + options: { + extensions: [`.mdx`, `.md`], + defaultLayouts: { + default: require.resolve("./src/components/layout.js"), + }, + remarkPlugins: [remarkRequireFilePathPlugin], }, - } - }, + }, ], } + +/** + * This is a test to ensure that `gatsby-plugin-mdx` correctly pass the `file` argument to the underlying remark plugins. + * See #26914 for more info. + */ +function remarkRequireFilePathPlugin() { + return function transformer(tree, file) { + if (!file.dirname) { + throw new Error("No directory name for this markdown file!") + } + } +} diff --git a/packages/gatsby-plugin-mdx/utils/gen-mdx.js b/packages/gatsby-plugin-mdx/utils/gen-mdx.js index 276e14587ac61..b8e3691a23f02 100644 --- a/packages/gatsby-plugin-mdx/utils/gen-mdx.js +++ b/packages/gatsby-plugin-mdx/utils/gen-mdx.js @@ -241,7 +241,7 @@ async function findImports({ } let mdast = await compiler.parse(fileOpts) - mdast = await compiler.run(mdast) + mdast = await compiler.run(mdast, fileOpts) // Assuming valid code, identifiers must be unique (they are consts) so // we don't need to dedupe the symbols here.