@@ -2,6 +2,7 @@ import type { ProcessorOptions } from "@mdx-js/mdx"
22import type { GatsbyCache , NodePluginArgs , PluginOptions , Store } from "gatsby"
33import deepmerge from "deepmerge"
44import type { IPluginInfo } from "gatsby-plugin-utils/types"
5+ import { cachedImport } from "./cache-helpers"
56import { getSourcePluginsAsRemarkPlugins } from "./get-source-plugins-as-remark-plugins"
67import rehypeMdxMetadataExtractor from "./rehype-metadata-extractor"
78import { remarkMdxHtmlPlugin } from "./remark-mdx-html-plugin"
@@ -51,6 +52,10 @@ export const enhanceMdxOptions: EnhanceMdxOptions = async (
5152) => {
5253 const options = defaultOptions ( pluginOptions )
5354
55+ const { default : remarkUnwrapImages } = await cachedImport <
56+ typeof import ( "remark-unwrap-images" )
57+ > ( `remark-unwrap-images` )
58+
5459 // Set jsxRuntime & jsxImportSource based on Gatsby project config
5560 const { config } = helpers . store . getState ( )
5661 const { jsxRuntime, jsxImportSource } = config
@@ -61,6 +66,10 @@ export const enhanceMdxOptions: EnhanceMdxOptions = async (
6166 options . mdxOptions . remarkPlugins = [ ]
6267 }
6368
69+ // The unwrapping has to happen before any other remark plugins are run (especially gatsby-remark-images)
70+ // Otherwise remark-unwrap-images would operate on the already transformed images
71+ options . mdxOptions . remarkPlugins . push ( remarkUnwrapImages )
72+
6473 // Inject Gatsby path prefix if needed
6574 if ( helpers . pathPrefix ) {
6675 options . mdxOptions . remarkPlugins . push ( [
0 commit comments