From 67730fdfa8fa1f123a5999a2ddf9834775f7d98b Mon Sep 17 00:00:00 2001 From: Kai Hao Date: Tue, 21 Apr 2020 22:05:03 +0800 Subject: [PATCH] Add support for file argument in remark plugins for gatsby-plugin-mdx (#21489) --- packages/gatsby-plugin-mdx/gatsby/on-create-page.js | 5 ++++- packages/gatsby-plugin-mdx/gatsby/preprocess-source.js | 5 ++++- packages/gatsby-plugin-mdx/utils/gen-mdx.js | 1 + 3 files changed, 9 insertions(+), 2 deletions(-) diff --git a/packages/gatsby-plugin-mdx/gatsby/on-create-page.js b/packages/gatsby-plugin-mdx/gatsby/on-create-page.js index 260dbdd031b19..be94d4361f7ca 100644 --- a/packages/gatsby-plugin-mdx/gatsby/on-create-page.js +++ b/packages/gatsby-plugin-mdx/gatsby/on-create-page.js @@ -14,7 +14,10 @@ module.exports = async ({ page, actions }, pluginOptions) => { // we're trying to insert and if we don't check we can end up in infinite loops if (extensions.includes(ext) && !page.context.frontmatter) { const content = await fs.readFile(page.component, `utf8`) - const code = await mdx(content, options) + const code = await mdx(content, { + filepath: page.component, + ...options, + }) // grab the exported frontmatter const { frontmatter } = extractExports(code) diff --git a/packages/gatsby-plugin-mdx/gatsby/preprocess-source.js b/packages/gatsby-plugin-mdx/gatsby/preprocess-source.js index 22c7ff419682c..d3e4d38ea2afb 100644 --- a/packages/gatsby-plugin-mdx/gatsby/preprocess-source.js +++ b/packages/gatsby-plugin-mdx/gatsby/preprocess-source.js @@ -10,7 +10,10 @@ module.exports = async function preprocessSource( const ext = path.extname(filename) if (extensions.includes(ext)) { - const code = await mdx(contents, options) + const code = await mdx(contents, { + filepath: filename, + ...options, + }) return code } return null diff --git a/packages/gatsby-plugin-mdx/utils/gen-mdx.js b/packages/gatsby-plugin-mdx/utils/gen-mdx.js index fc6fbd4b3ef3d..3c6ede688ddd7 100644 --- a/packages/gatsby-plugin-mdx/utils/gen-mdx.js +++ b/packages/gatsby-plugin-mdx/utils/gen-mdx.js @@ -122,6 +122,7 @@ export const _frontmatter = ${JSON.stringify(data)}` debug(`running mdx`) let code = await mdx(content, { + filepath: node.fileAbsolutePath, ...options, remarkPlugins: options.remarkPlugins.concat( gatsbyRemarkPluginsAsremarkPlugins