pngwn / MDsveX

A markdown preprocessor for Svelte.
https://mdsvex.pngwn.io
MIT License
2.27k stars 96 forks source link

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

Open NazCodeland opened 4 days ago

NazCodeland commented 4 days ago

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!