Markdown Preview Exec
This extension is EXPERIMENTAL.
Markdown Preview Exec is a Visual Studio Code extension that enriches markdown preview with ability to execute JavaScript code blocks.
Usage
Mark the code block with the exec
language identifier:
```exec
"Hello, <b>world</b>!";
```
The result is rendered in the preview:
Hello, world!
Screenshot:
If the file /scripts/hello.js
is included by markdown-exec.include
, it can be called as follows:
```exec
require('/scripts/hello').print();
```
Configuration
timeout
- timeout (in seconds) for the script to complete
include
- list of glob patters to include scripts from, relative to the first workspace folder
Example:
"markdown-exec.timeout": 0,
"markdown-exec.include": [
"scripts/kb/**/*.js"
]
Limitations
The default strict security profile of the markdown preview disables the eval
functions. Therefore, the code is evaluated through JS interpreter, which only supports ES5. This means that:
- Code is transpiled into ES5 with Babel
- No access to the DOM and other browser APIs
- No access to the Node.js APIs
- Performance is slow (in comparison to the normal JS code execution)
- The script result should be searialisable to JSON (no object pasing from the sandbox)
require
is very basic, supports only absolute paths from the worspace root folder
By selecting a different security profile, the code can be executed using eval, which enables modern JavaScript with access to the DOM. However, this approach is not recommended due to the associated security risks. Additionally, the sandboxed nature of the preview means that Node.js APIs, typically accessible to VS Code extensions, will remain unavailable.
TODO
- [ ] Add JS polyfill
- [ ] Provide access to some of the browser APIs (e.g.
console
, document
, window
, etc.)
- [ ] Perfomance optimisations