-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Add remark-directive-mdx to MDX extensions list #2664
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
base: main
Are you sure you want to change the base?
Conversation
[`remark-directive-mdx`](https://github.com/re-xyr/remark-directive-mdx) transforms Markdown directives (`:directive[]{}`) parsed by `remark-directive` into MDX JSX elements. Signed-off-by: daylily <[email protected]>
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #2664 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 21 21
Lines 2649 2649
Branches 2 2
=========================================
Hits 2649 2649 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
remcohaszing
left a comment
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.
Thanks! This project looks really cool. I do have some tips.
- I believe you don’t need to define
_mdxExplicitJsxin any of the places where you do. - Don’t use
Object.assign()to modify nodes. Instead, replace them like this:visit(tree, ['containerDirective', 'leafDirective', 'textDirective'], (node, index, parent) => { if(index == null || parent == null) { return } // … parent.children[index] = newNode })
- Avoid type casting using
as. - I think you may get better help from TypeScript if you use object literals instead of
unist-builder. I may be wrong though. - You perform some logic to convert directive attributes, interpreted as HTML attributes, to JSX attributes. Your approach is a bit naive. This makes sense, as
class→classNameis a well-known example, but there’s more. You may be interested to have a look at or usehast-util-properties-to-mdx-jsx-attributes. On the other hand, users write those attributes without HTML. So maybe you don’t want to perform any conversion at all. - If you map over an array, it’s generally better to specify the generic type on the
.mapmethod or the return type of the mapper function. This provides better TypeScript support. - In
astroHandleLabelyou generate a<Fragment />element. However, you don’t insert aFragmentimport. - In your tests you build the AST manually and test on that. This way it’s fairly easy to make mistakes. I recommend testing your plugin with
compile()and testing it against a string input as an actual user would use it.
|
Hi! Thanks for your detailed feedback.
From
It would cause the old subtree to be traversed instead of the new one. Indeed, if I try to replace the node in the parent outright by [FAIL] test/index.test.ts > transforms directives to MDX JSX
AssertionError: expected { type: 'root', …(1) } to deeply equal { type: 'root', …(1) }
- Expected
+ Received
@@ -54,17 +54,13 @@
},
],
"type": "paragraph",
},
{
- "attributes": [],
"children": [],
- "data": {
- "_mdxExplicitJsx": true,
- },
"name": "inner-directive",
- "type": "mdxJsxFlowElement",
+ "type": "leafDirective",
},
],the nested directive is not transformed.
This is unavoidable; we are doing the same thing as
In any case, one fewer dependency is generally a good idea. I removed the dependency on
By default, the plugin performs no conversion on attributes; I just exposed a simple function that users can pass to the plugin to instruct it to do some basic transformations. If you think the way that function behaves is wrong or misleading, please elaborate and I'll mark the function as deprecated.
TypeScript inference seems to work fine to me, currently. Did you see any degenerate typing leaking out to the API?
This is already done by But in general, my current API for
This sounds like a good idea. I will do it later. |
Depends what you want! If a user passes
Is this a good case for
Generally I think there should be no conversion for things that users write themselves, and that MDX already has options for conversion for the stuff that users do not write. |
In that case, the plugin is already doing that (adding
This works, thanks for the suggestion. It saves us some object clones.
I think it would be nice to at least have the option to always use kebab-case for directive names; it works across all JSX frameworks (correct me if I'm wrong) and to me it makes the document more stylistically coherent. It only falls apart when you start using Web Components. Transformations on attributes might be more objectionable since only React/Solid have a uniform convention for attributes/props casing. But considering |
Initial checklist
Description of changes
remark-directive-mdxis a remark plugin that transforms Markdown directives (:directive[]{}) parsed byremark-directiveinto MDX JSX elements. This change adds this plugin to the plugins list in the docs.