Skip to content

Commit ae5f581

Browse files
authored
fix: gatsby-plugin-mdx's findImports not running plugins (#26191)
1 parent 70cd249 commit ae5f581

File tree

3 files changed

+75
-1
lines changed

3 files changed

+75
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
function createImportAST(name, filePath) {
2+
return {
3+
type: `import`,
4+
value: `import ${name} from '${filePath}'`,
5+
}
6+
}
7+
8+
module.exports = ({ markdownAST }) => {
9+
markdownAST.children = [
10+
createImportAST(`* as testInjection`, `@private/test-inject`),
11+
...markdownAST.children,
12+
]
13+
return markdownAST
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
const path = require(`path`)
2+
const { findImports } = require(`../gen-mdx`)
3+
4+
const markdownContent = `---
5+
title: Introduction
6+
---
7+
8+
# Header 1
9+
## Header 2
10+
### Header 3
11+
12+
\`\`\`js
13+
console.log('hello world')
14+
\`\`\`
15+
`
16+
17+
describe(`find imports`, () => {
18+
it(`allows injecting imports via plugins`, async () => {
19+
const results = await findImports({
20+
node: {
21+
id: `bbffffbb-bfff-bfff-bfff-dededededede`,
22+
children: [],
23+
parent: `bbffffbb-bfff-bfff-bfff-dededededebb`,
24+
internal: {
25+
content: markdownContent,
26+
type: `Mdx`,
27+
contentDigest: `6433c536b5eb922ad01f8d420bcabf6d`,
28+
counter: 38,
29+
owner: `gatsby-plugin-mdx`,
30+
},
31+
frontmatter: { title: `Introduction` },
32+
excerpt: undefined,
33+
exports: {},
34+
rawBody: markdownContent,
35+
fileAbsolutePath: `/path/to/getting-started.mdx`,
36+
},
37+
options: {
38+
remarkPlugins: [],
39+
gatsbyRemarkPlugins: [
40+
{ resolve: path.join(__dirname, `fixtures`, `test-plugin`) },
41+
],
42+
},
43+
getNode: () => null,
44+
getNodes: () => [],
45+
getNodesByType: () => [],
46+
repoter: () => {},
47+
cache: () => {},
48+
pathPrefix: ``,
49+
})
50+
51+
expect(results).toEqual({
52+
scopeImports: [
53+
`import * as testInjection from '@private/test-inject'`,
54+
`import * as React from 'react'`,
55+
],
56+
scopeIdentifiers: [`testInjection`, `React`],
57+
})
58+
})
59+
})

packages/gatsby-plugin-mdx/utils/gen-mdx.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,8 @@ async function findImportsExports({
242242
fileOpts.path = absolutePath
243243
}
244244

245-
const mdast = await compiler.parse(fileOpts)
245+
let mdast = await compiler.parse(fileOpts)
246+
mdast = await compiler.run(mdast)
246247

247248
// Assuming valid code, identifiers must be unique (they are consts) so
248249
// we don't need to dedupe the symbols here.

0 commit comments

Comments
 (0)