Skip to content

Commit 94d18fa

Browse files
authored
fix(gatsby-plugin-mdx): Unwrap images (#36856)
* add remark-unwrap-images to mdx * place remark plugin at correct place
1 parent 20ef7c1 commit 94d18fa

File tree

3 files changed

+20
-0
lines changed

3 files changed

+20
-0
lines changed

packages/gatsby-plugin-mdx/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
"mdast-util-to-markdown": "^1.3.0",
4343
"mdast-util-toc": "^6.1.0",
4444
"rehype-infer-description-meta": "^1.0.1",
45+
"remark-unwrap-images": "^3.0.1",
4546
"unified": "^10.1.2",
4647
"unist-util-visit": "^4.1.0",
4748
"vfile": "^5.3.2"

packages/gatsby-plugin-mdx/src/plugin-options.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import type { ProcessorOptions } from "@mdx-js/mdx"
22
import type { GatsbyCache, NodePluginArgs, PluginOptions, Store } from "gatsby"
33
import deepmerge from "deepmerge"
44
import type { IPluginInfo } from "gatsby-plugin-utils/types"
5+
import { cachedImport } from "./cache-helpers"
56
import { getSourcePluginsAsRemarkPlugins } from "./get-source-plugins-as-remark-plugins"
67
import rehypeMdxMetadataExtractor from "./rehype-metadata-extractor"
78
import { 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([

yarn.lock

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21556,6 +21556,16 @@ remark-toc@^5.0.0:
2155621556
mdast-util-toc "^2.0.0"
2155721557
remark-slug "^5.0.0"
2155821558

21559+
remark-unwrap-images@^3.0.1:
21560+
version "3.0.1"
21561+
resolved "https://registry.yarnpkg.com/remark-unwrap-images/-/remark-unwrap-images-3.0.1.tgz#22a547baa8fa1e2c66c7d087ebd9cf829a74a255"
21562+
integrity sha512-5VUY0n+J9lPTPfkct5S3/SbutryBjp8J/4mbgtlkDrOk3h8jde0hyqdYUJOoJKherZezS08tjd6i4+nnQ+wl5w==
21563+
dependencies:
21564+
"@types/mdast" "^3.0.0"
21565+
hast-util-whitespace "^2.0.0"
21566+
unified "^10.0.0"
21567+
unist-util-visit "^4.0.0"
21568+
2155921569
remark@^13.0.0:
2156021570
version "13.0.0"
2156121571
resolved "https://registry.yarnpkg.com/remark/-/remark-13.0.0.tgz#d15d9bf71a402f40287ebe36067b66d54868e425"

0 commit comments

Comments
 (0)