From 5d39594c1d9b3562b318b5d299905c0078d74557 Mon Sep 17 00:00:00 2001 From: Kai Hao Date: Wed, 23 Sep 2020 20:41:07 +0800 Subject: [PATCH] fix(gatsby-plugin-mdx): Fix not passing file path to remark plugins (#26914) * Fix gatsby-plugin-mdx not passing file path to remark plugins * Add comments for the test --- e2e-tests/mdx/gatsby-config.js | 28 +++++++++++++++------ packages/gatsby-plugin-mdx/utils/gen-mdx.js | 2 +- 2 files changed, 22 insertions(+), 8 deletions(-) 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.