Copy to clipboard button with rehype-shiki
and mdx
#785
Unanswered
nikolailehbrink
asked this question in
Q&A
Replies: 2 comments
-
Hey @nikolailehbrink I'm not sure this is the most elegant solution but I managed to do it with function transformerRawStringAsProp(): ShikiTransformer {
return {
preprocess(code) {
(this.meta as any).rawCodeString = code;
},
pre(node) {
node.properties['rawCodeString'] = (this.meta as any).rawCodeString;
},
};
} Simply pass it to the transformers array and then you can use the |
Beta Was this translation helpful? Give feedback.
0 replies
-
Actually here's a cleaner solution 😄 function transformerSourceAsPreProp(): ShikiTransformer {
return {
pre(node) {
node.properties['rawCodeString'] = this.source;
},
};
} |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Hi @antfu,
in a recent
remix
project I tried out usingmdx
with mdx-js/rollup and@shikijs/rehype
.I wanted to incorporate a copy to clipboard button for all my code blocks but I have a hard time doing that. Now I searched the docs on how to do that and only found out about the
preprocess
step mentioned in https://shiki.style/guide/transformers. I could log the initial code with that function but don't know how to proceed there.A different solution was mentioned in this blog article and doing it with traversing the tree, but I didn't want to install yet another package (
unist-util-visit
) to make it work. Would be nice to get a detailed explanation on how one would tackle that problem, since it is a pretty common use case I guess.Beta Was this translation helpful? Give feedback.
All reactions