twibiral / obsidian-execute-code

Obsidian Plugin to execute code in a note.
MIT License
994 stars 61 forks source link

[FEATURE] In-Obsidian execution of JavaScript code (special case) #341

Open cybrox opened 3 months ago

cybrox commented 3 months ago

Firstly, thank you for writing and maintaining this plugin, it gets Obsidian a lot closer to the use-case I need it for!

Is your feature request related to a problem? Please describe. I have a lot of 2D / 3D vector math and transformation related notes that I would like to add examples to using a library like p5.js. Ideally, I'd want to be able to do something like this:

@html(`
<canvas id="myCanvas"></canvas>
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.9.2/p5.min.js"></script>
`)

// write any kind of p5 code here

Describe the solution you'd like This plugin allows me to run JavaScript snippets with the built-in executor that uses my NodeJS path.
For almost all languages, abstracting a generic REPL executor and running that in isolation is a very good idea.
However, JavaScript is a bit of a special case since Obsidian itself is running JavaScript when rendering the note, so in this special case it could be very beneficial to be able to run code directly inside Obsidian instead of going through the whole usual workflow of wrapping it, writing it to a file, executing it and capturing the stdin/stdout.

Describe alternatives you've considered Not many really, I've considered writing a plugin myself that adds the canvas and just eval()s the code but I am already using this plugin, so I figured this might be an interesting special case?

Let me know if you'd be interested in having support for this special case in this plugin.

twibiral commented 1 month ago

Hi! Thanks for your issue, I generally think that accessing the runtime of the Obsidian application is a bad idea. Nonetheless, you are not the first to ask for this feature (e.g. #351). If you have an idea how we can implement this as an optional feature, feel free to tell me more or open a pull request.