-
Notifications
You must be signed in to change notification settings - Fork 146
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
Imports in MDX files #143
Comments
There are a variety of technical hurdles that stand in the way of this. If you are interested in tackling them, we'd very much welcome a contribution! |
Just want to update here that we are planning on implementing this as soon as we're able to prioritize it. Still quite a difficult task, but it should be possible. |
Any updates on this issue ? |
Yes please, 🙇♂️ for me this feature is a deal breaker and I would looove to use this tool to create a blog that relies heavily on Also, I see that export const myVar = "foo";
I will use {myVar} here. |
Sorry, no updates here. It's very difficult to pull this off properly loading from a remote source, and would require a fairly large time investment - afaik there is not a use case for this specifically at HashiCorp right now so there's not a lot of motivation to put tons of work into it. If you are using this for something small that it not intended to go to high scale, you can still get away with loading from webpack the way that If you're making a personal blog, you could also look into nextra! |
We believe in you! |
@jescalan I feel like I might be able to come up with something using node fs when encountering an import statement... but before I jump in to trying something out, I want to understand if there is a reason you would recommend against that or if that would preclude those efforts from getting merged based on HashiCorp's use case. I am guessing I would need to poke around in the plugin that removes import and export statements from the MDX as we will need the import statements for using getting the imported components. If either of the above ideas is incorrect or not in alignment with this project, let me know. Otherwise, I happen to have some time off before starting a new role and I might find some time to see if I can get something working. I am also not that good with Typescript so this would be something fun to level up on a bit. |
Hey @brianespinosa! The main difficulty with supporting import statements with this library is that it operates after the application is bundled. This is by design, as at HashiCorp we treat the MDX content as data which is fetched at runtime, instead of a different module format. When the application gets bundled If we want to share code between the application bundle and the imports in our MDX, we need a way to map the module being requested from an MDX import to that module in our app bundle. An fs-based approach could work in a node environment, but we'd be out of luck when executing the code on the client. We could support passing some sort of import map into our methods, using a combination of https://webpack.js.org/api/module-methods/#requireresolveweak and a plugin to rewrite imports to Another approach might be to use dynamic imports and attempt to resolve from external modules, e.g.: import foo from 'unpkg.com/[email protected]'
// vvvv
(await import('unpkg.com/[email protected]')).foo With this though, we change the MDX rendering from sync to async, so we'd need Hopefully this helps give some context, if you're interested in giving this a shot let me know! |
@BRKalow thank you for taking the time to put this thoughtful write up together about the limitations with my proposal and some pointers of where this might still work. I'll check back in when I sit back down at a computer in a couple weeks and update you on if I have some bandwidth to tinker with this. |
A very makeshift workaround for now could be:
e.g. Here is an example of rendering JSON files in graph components. I was trying to import the JSON files directly in the MDX. import ChartSnapshot from "~/components/ChartSnapshot";
import decemberSnapshotNz from "~/snapshots/december-2022-nz.json";
import decemberSnapshotAu from "~/snapshots/december-2022-au.json";
const components = {
ChartSnapshotDecemberNz2022: () => (
<ChartSnapshot
snapshotData={decemberSnapshotNz}
label="Number of Open IT Jobs over Dec 2022, NZ"
description="50.2% decrease in New Zealand IT jobs over December 2022."
/>
)
};
...
<MDXRemote {...source} components={components} /> And then in the MDX file ## Numbers
#### Foo Bar
<ChartSnapshotDecemberNz2022 /> |
Hey y'all, I know this issue has been open for quite some time and supporting import statements is a common question/feature when folks start using As mentioned above, here at HashiCorp we treat our MDX content as data that gets loaded and rendered at runtime, potentially independent of our application's compilation steps. This is fundamentally at odds with supporting imports to local application code. Given that, I'm going to close this issue as it's not something we have a need to support at this time. If all of your content is local to your repository, it might be worth looking at As we continue to evolve our content platform, this might be something we revisit. With React Server Components support now available, this opens interesting opportunities to use async server components to support imports of external modules. Anyone is of course welcome to contribute support for this if they're up for it! 🙏 |
I know that the README's caveats mention this isn't currently possible and #48 is closed but:
But why not just let us tell next-mdx-remote where to load them from?
The text was updated successfully, but these errors were encountered: