Replies: 2 comments 3 replies
-
Welcome @lrholmes! 👋 It is possible. |
Beta Was this translation helpful? Give feedback.
-
Thanks for the prompt reply! Okay, I've got it now. Thanks for pointing me in the right direction with some examples — it turns out I was previously on the right track with manipulating I found what I was looking for, an example of a "literal" attribute which seems to work for my case, in the For posterity and possible future readers, here's my plugin snippet, updated with the relevant code to pass in a "prop" to my MDX component: const headings = [];
visit(tree, 'heading', (node) => {
const id = getID(node);
const value = toString(node)
headings.push({ id, value });
});
visit(tree, 'mdxJsxFlowElement', (node) => {
if (node.name === 'TOC') {
const attribute = {
type: 'mdxJsxAttribute',
name: 'headings', // prop name
value: {
type: 'mdxJsxAttributeValueExpression',
value: JSON.stringify(headings),
data: {
estree: {
type: 'Program',
body: [
{
type: 'ExpressionStatement',
expression: {
type: 'Literal',
value: headings,
},
},
],
sourceType: 'module',
comments: [],
},
},
},
};
node.attributes.push(attribute);
}
}); |
Beta Was this translation helpful? Give feedback.
-
Hi!
I have a question regarding if / how it's possible to manipulate component props inside a remark plugin. Let's say I'm implementing my own version of the
remark-toc
plugin: https://github.com/remarkjs/remark-toc.But instead of rendering a list under a relevant heading by inserting children on the AST, I want to be able to use a React component that accepts a
headings
prop. Would that be feasible to pull off within the bounds of a remark plugin?I've been playing around with something like this (I've simplified things a bit):
MDX:
Compilation using
next-mdx-remote
:The relevant part of
customTOCPlugin
(I think):I'm conscious that this problem can be solved via other means, such as it is in the
remark-toc
plugin, but for this example let's say I'm limited to rendering the table of contents via a React component.I've considered a couple of other options that involve passing the headings data up to the react/next context as
frontmatter
, but these don't seem as clean or convenient as being able to manipulate the props of the JSX as it's defined in the mdx.Apologies if this isn't the right place to ask such a question 🙏
Beta Was this translation helpful? Give feedback.
All reactions