Skip to content
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

Allow usage of JavaScript defined in mdsvex code blocks for interactivity #608

Open
NazCodeland opened this issue Jul 3, 2024 · 2 comments

Comments

@NazCodeland
Copy link

I’m writing a blog post in mdsvex and my article contains JavaScript code blocks. I’d like to be able to use the code within the JavaScript block to provide an interactive illustration of the code. Currently, I have to duplicate the code the script tag to make it interactive which is not ideal.

Here’s an example:

function privateFunction() {
  console.log("This is a private function")
}
<button on:click={privateFunction}>Click me</button>

In this example, I’d like the privateFunction to be callable from the button’s click event without having to also define it in a script tag. This feature would make writing interactive blog posts much easier. Thank you for considering this request!

@github-project-automation github-project-automation bot moved this to Triage + Refine in mdsvex Jul 3, 2024
@danbulant
Copy link

you could write a remark plugin to do that for you - mdsvex accepts remarkPlugins array, pass it a function that returns a function accepting arguments tree, file.
Tree contains the remark AST, so a node with a string type and some children. You can alter the AST, for example by adding new raw blocks based on the code block/node.

@einarpersson
Copy link

@NazCodeland Did you solve it with a custom plugin as suggested? If so would be very nice of you to share (or share a link to your interactive blog ;) )

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Triage + Refine
Development

No branches or pull requests

3 participants