-
Notifications
You must be signed in to change notification settings - Fork 10.3k
mdx-v2: feat: add support for remark, rehype and gatsby-remark plugins #35751
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
c1a011d
feat: add support for remark, rehype and gatsby-remark plugins
axe312ger 3c0c5ab
clean up
axe312ger e8c2ec9
refactor: extract our two custom remark into their own files
axe312ger 75bd697
fix: pass path to MDX file for remark plugins
axe312ger c85bb8c
refactor: remove type assertions
axe312ger e924f4d
feat: extend plugin option validation
axe312ger db25e2e
remove old comment
axe312ger File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| import type { ProcessorOptions } from "@mdx-js/mdx" | ||
| import type { IFileNode, IMdxNode } from "./types" | ||
|
|
||
| // Compiles MDX into JS | ||
| // This could be replaced by @mdx-js/mdx if MDX compile would | ||
| // accept custom data passed to the unified pipeline via processor.data() | ||
| export default async function compileMDX( | ||
| source: string, | ||
| mdxNode: IMdxNode, | ||
| fileNode: IFileNode, | ||
| options: ProcessorOptions | ||
| ): Promise<string> { | ||
| const { createProcessor } = await import(`@mdx-js/mdx`) | ||
| const { VFile } = await import(`vfile`) | ||
|
|
||
| const processor = createProcessor(options) | ||
|
|
||
| // If we could pass this via MDX loader config, this whole custom loader might be obsolete. | ||
| processor.data(`mdxNode`, mdxNode) | ||
|
|
||
| const result = await processor.process( | ||
| // Inject path to original file for remark plugins. See: https://github.com/gatsbyjs/gatsby/issues/26914 | ||
| new VFile({ value: source, path: fileNode.absolutePath }) | ||
| ) | ||
|
|
||
| return result.toString() | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| /* eslint-disable @babel/no-invalid-this */ | ||
| import type { LoaderDefinition } from "webpack" | ||
| import type { ProcessorOptions } from "@mdx-js/mdx" | ||
| import type { NodeMap } from "./types" | ||
|
|
||
| import { getOptions } from "loader-utils" | ||
|
|
||
| import compileMDX from "./compile-mdx" | ||
|
|
||
| export interface IGatsbyMDXLoaderOptions { | ||
| options: ProcessorOptions | ||
| nodeMap: NodeMap | ||
| } | ||
|
|
||
| // Custom MDX Loader that injects the GraphQL MDX node into plugin data | ||
| // This whole loaded could be replaced by @mdx-js/loader if MDX would | ||
| // accept custom data passed to the unified pipeline via processor.data() | ||
| const gatsbyMDXLoader: LoaderDefinition = async function (source) { | ||
| const { options, nodeMap }: IGatsbyMDXLoaderOptions = getOptions(this) | ||
| const res = nodeMap.get(this.resourcePath) | ||
|
|
||
| if (!res) { | ||
| return source | ||
| } | ||
|
|
||
| const { mdxNode, fileNode } = res | ||
|
|
||
| return compileMDX(source, mdxNode, fileNode, options) | ||
| } | ||
|
|
||
| export default gatsbyMDXLoader |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we really still need that?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When I look at https://unifiedjs.com/learn/guide/create-a-plugin/ the
fileas a second argument is still there. So there will be remark plugins that rely on that.Isn't the official loader also doing this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
At least not in the way our test is checking it:
https://github.com/gatsbyjs/gatsby/blob/feat/mdx-v2-plugin-support/e2e-tests/mdx/gatsby-config.js#L39-L49
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@axe312ger then we'll need to update our test later