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) for image import name collisions in mdx files #35028

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions e2e-tests/mdx/cypress/integration/pages.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,14 @@ describe(`Pages`, () => {
cy.visit(`/list`).waitForRouteChange()
cy.getTestElement(`embed-slug`).contains(`about/embedded`)
})

it (`generates a page with an image import for a jpg`, () => {
cy.visit(`/mdx_import1`).waitForRouteChange()
cy.getTestElement(`purple-png-image`).invoke('height').should('eq', 512)
})

it (`generates a page with an image import for a png`, () => {
cy.visit(`/mdx_import2`).waitForRouteChange()
cy.getTestElement(`green-png-image`).invoke('height').should('eq', 1024)
})
})
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions e2e-tests/mdx/src/pages/mdx_import1/index.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import prettyImage from "./gatsby-icon.png"

<img data-testid="purple-png-image" src={prettyImage} />
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions e2e-tests/mdx/src/pages/mdx_import2/index.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import prettyImage from "./gatsby-icon.png"

<img data-testid="green-png-image" src={prettyImage} />
58 changes: 56 additions & 2 deletions packages/gatsby-plugin-mdx/gatsby/on-create-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,66 @@ async function onCreateNode(api, pluginOptions) {
return
}

/** @type string */
const content = await api.loadNodeContent(api.node)

/** Captures the import name when used on a file with an extension up to 4 chars long. e.g
* `import pic from file.jpeg` or `import * as pic from file.jpeg` */
const importFinderRegexp = `import {?(?:\\*\\s+as\\s+)?(.*)(?:}?\\s*from\\s+['"].*\\.(?:avif|bmp|jpe?g|gif|png|svg|webp)['"])`
/** An array of file imports, e.g. `import pic from file.jpeg`
* @type RegExpMatchArray */
const fileImports = []
/** @type string[] */
const namedImports = []
Array.from(
content.matchAll(RegExp(importFinderRegexp, `gm`)),
(search, idx) => {
fileImports[idx] = search[0]
namedImports[idx] = search[1]
}
)
const importId = api.node.id.replace(/-/g, ``)

const renamedImports = fileImports?.map((fileImport, index) =>
fileImport.replace(
namedImports[index],
`${namedImports[index].trim()}_${importId} `
)
)

/** @type string */
let fixedContent = content
if (fileImports?.length !== renamedImports?.length) {
// Throw error
api.reporter.error(
`Error: the number of namespaced imports and found imports do not match in gatsby-plugin-mdx's onCreateNode`
)
} else {
try {
if (fileImports.length > 0) {
fileImports.forEach((item, index) => {
const namedImport = namedImports[index].trim()
fixedContent = fixedContent
// 1st replacement: replace given import, with uid postfixed imports
.replace(item, renamedImports[index])
// 2nd replacement: check for src={importName} and globally replace it regardless of surrounding space
.replace(
RegExp(`src={ *?${namedImport} *?}`, `gm`),
`src={${namedImport}_${importId}}`
)
})
}
} catch (err) {
api.reporter.error(
`Error when creating namespaced imports in gatsby-plugin-mdx`
)
}
}

if (options.lessBabel) {
await onCreateNodeLessBabel(content, api, options)
await onCreateNodeLessBabel(fixedContent, api, options)
} else {
await onCreateNodeExtraBabel(content, api, options)
await onCreateNodeExtraBabel(fixedContent, api, options)
}
}

Expand Down