-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Alternative to createMdxAstCompiler without mdxAstToMdxHast #1512
Comments
Here's what I'm doing as a workaround: brillout/vite-plugin-mdx@818bdab |
This seems like an XY problem. It also doesn't really make sense particularly around:
Lines 14 to 16 in 5169bf1
as you seem to be aware from your solution:
the imports looks a bit strange and unnecessarily complex, but otherwise it seems fine, I'm not sure where the root issue you are trying to solve is. |
@ChristianMurphy it's because without the remark AST it's harder to find the import nodes, or combine the ASTs from two different MDX files (prebuilt transclusion, etc). You can see where it's happening here in the linked plugin. (Also see #1423 for a similar, but not exactly the same use case). I also have a use case for this in Sector, the block editor I'm building on top of MDX and the workaround I use is the same as the one posted here (copy/paste the createMdxAstCompiler bits without the hast plugin) and I've talked to @johno about including this behavior as if Sector needs it then others will as well to build more sophisticated tools on top of MDX. @ChristianMurphy In the future, if you don't understand the use case please avoid linking to the xy problem question. You will get better responses from users if you don't link to 10 year old stackexchange questions. |
I'm aware why the remark+MDX AST exists 🙂 .
I welcome a better link for explaining the XY Problem. |
We have to import
"every possible combination" is hyperbole. The only case I want to be supported out-of-the-box is "everything MDX does by default, excluding HTML-specific stuff". |
I second that these questions raised here seem like XY problems: the original request for exposing such a function might be fine, but the solutions for a) the custom import syntax is interesting, but novel to this particular plugin, and in my opinion unclear (import without assignment typically indicates something imported for side effects) whereas the in my opinion clearer:
…works in other places already. b) vite supports rollup plugins to compile mdx -> js, which when used would handle “recursion” (as in, one mdx file loading another) @aleclarson what is the reason for this new syntax? Have you looked at using a rollup plugin? I was also looking at esbuild, but unfortunately vite doesn’t allow esbuild plugins. Might be something to ask vite folks about. |
The side effect is "pre-built transclusion", as @ChristopherBiscardi so eloquently put it.
Convenience and intuitiveness. I originally tried this syntax hoping it would work.
Vite plugins are Rollup plugins, with added capabilities (like dev server hooks).
Not interested. I have a working solution already! :) |
I've hit another use case for having this feature: |
@aleclarson this comes back to the XY problem. You've both suggested a solution, adding a new API to access the Markdown+MDX AST, inside What is missing, is that this is not the only solution.
Which leads to which approach should be taken? Adding a new method to An alternative, improving the documentation and guides to add more information on Markdown+MDX AST and how to work with it with the existing API, seems to address the core concern of making it more approachable and easier for new adopters to consider leveraging it, without increasing API surface or adding a potentially confusing method. This is the approach I would currently favor. That said, the approaches are not mutually exclusive, a method could be added and the documentation improved.
Not entirely. We could add one to access the Markdown+MDX AST specifically. That seems like a broader discussion, which should include @mdx-js/core and @mdx-js/maintainers. Personally I see these as better suited for documentation, than an ever increasing core API. Thoughts? 💭 |
This particular issue subject can be solved by doing: import {unified} from 'unified'
import remarkParse from 'remark-parse'
import remarkMdx from 'remark-mdx' // install with `@next` for now.
const processor = unified().use(remarkParse).use(remarkMdx) This can be used like so: For the # Hi
import "./partial.mdx" ^-- where the content of partial is injected into the document, This can be achieved with a plugin using our new MDX 2 ASTs (see https://v2.mdxjs.com), either on the markdown or HTML AST. export default function remarkMdxTurnImportsWithoutSpecifiersIntoPartials() {
return (tree) {
const count = 0
visit(tree, 'mdxjsEsm', (node, index, parent) => {
const elements = []
node.data.estree.body.forEach((importExport) => {
// An import declaration w/o specifiers: `import "x"`
// This should maybe check if it looks like an `.mdx?` import though?
if (importExport.type === 'ImportDeclaration' && importExport.specifiers.length === 0) {
const name = 'MDXPartial' + (count++)
// Add a default specifier: `import MDXPartial1 from "x"`
importExport.specifiers.push({type: 'ImportDefaultSpecifier', local: {type: 'Identifier', name}})
// Add an element: `<MDXPartial1 />`
elements.push({type: 'mdxJsxFlowElement', name, attributes: [], children: []})
}
})
// Add the elements after the ESM:
parent.children.splice(index + 1, 0, ...elements)
})
}
} |
I want an export like
createMdxAstCompiler
but I also want to preserve the MDX syntax tree, instead of getting a HTML syntax tree back.This is for a Vite plugin that uses the local
@mdx-js/mdx
, so importingremark-parse
,remark-mdx
, etc is not viable.The text was updated successfully, but these errors were encountered: