Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(gatsby-plugin-mdx): Unwrap images #36856

Merged
merged 2 commits into from Oct 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/gatsby-plugin-mdx/package.json
Expand Up @@ -42,6 +42,7 @@
"mdast-util-to-markdown": "^1.3.0",
"mdast-util-toc": "^6.1.0",
"rehype-infer-description-meta": "^1.0.1",
"remark-unwrap-images": "^3.0.1",
"unified": "^10.1.2",
"unist-util-visit": "^4.1.0",
"vfile": "^5.3.2"
Expand Down
9 changes: 9 additions & 0 deletions packages/gatsby-plugin-mdx/src/plugin-options.ts
Expand Up @@ -2,6 +2,7 @@ import type { ProcessorOptions } from "@mdx-js/mdx"
import type { GatsbyCache, NodePluginArgs, PluginOptions, Store } from "gatsby"
import deepmerge from "deepmerge"
import type { IPluginInfo } from "gatsby-plugin-utils/types"
import { cachedImport } from "./cache-helpers"
import { getSourcePluginsAsRemarkPlugins } from "./get-source-plugins-as-remark-plugins"
import rehypeMdxMetadataExtractor from "./rehype-metadata-extractor"
import { remarkMdxHtmlPlugin } from "./remark-mdx-html-plugin"
Expand Down Expand Up @@ -51,6 +52,10 @@ export const enhanceMdxOptions: EnhanceMdxOptions = async (
) => {
const options = defaultOptions(pluginOptions)

const { default: remarkUnwrapImages } = await cachedImport<
typeof import("remark-unwrap-images")
>(`remark-unwrap-images`)

// Set jsxRuntime & jsxImportSource based on Gatsby project config
const { config } = helpers.store.getState()
const { jsxRuntime, jsxImportSource } = config
Expand All @@ -61,6 +66,10 @@ export const enhanceMdxOptions: EnhanceMdxOptions = async (
options.mdxOptions.remarkPlugins = []
}

// The unwrapping has to happen before any other remark plugins are run (especially gatsby-remark-images)
// Otherwise remark-unwrap-images would operate on the already transformed images
options.mdxOptions.remarkPlugins.push(remarkUnwrapImages)

// Inject Gatsby path prefix if needed
if (helpers.pathPrefix) {
options.mdxOptions.remarkPlugins.push([
Expand Down
10 changes: 10 additions & 0 deletions yarn.lock
Expand Up @@ -21565,6 +21565,16 @@ remark-toc@^5.0.0:
mdast-util-toc "^2.0.0"
remark-slug "^5.0.0"

remark-unwrap-images@^3.0.1:
version "3.0.1"
resolved "https://registry.yarnpkg.com/remark-unwrap-images/-/remark-unwrap-images-3.0.1.tgz#22a547baa8fa1e2c66c7d087ebd9cf829a74a255"
integrity sha512-5VUY0n+J9lPTPfkct5S3/SbutryBjp8J/4mbgtlkDrOk3h8jde0hyqdYUJOoJKherZezS08tjd6i4+nnQ+wl5w==
dependencies:
"@types/mdast" "^3.0.0"
hast-util-whitespace "^2.0.0"
unified "^10.0.0"
unist-util-visit "^4.0.0"

remark@^13.0.0:
version "13.0.0"
resolved "https://registry.yarnpkg.com/remark/-/remark-13.0.0.tgz#d15d9bf71a402f40287ebe36067b66d54868e425"
Expand Down