The CodeBlock Runner project for Obsidian.md provides a simple and intuitive way to compile and run code from directly within Obsidian.
Making use of the GodBolt Compiler Explorer API and Obsidian's Markdown features, it's possible to extract code blocks, identify the langauge they're written in and run the code remotely, sending the output back to your device.
Currently, as the package is not available on the Obsidian store (I want to get this sorted out quickly!), you will need to clone the repository and build it yourself. Luckily, this is very easy if you have npm and git installed.
- Open the
plugins
folder of Obsidian- You will need to enable "Community Plugins" for this to work.
- Open the settings panel in Obsidian, and navigate to the Plugins section
- Enable community plugins and then click "open plugins folder"
- Open a terminal window in this folder
- Run:
git clone https://github.com/Pencilcaseman/codeblock-runner.git
cd
into the cloned directory- Run:
npm i
- Run:
npm run build
- Enable the plugin from the "Community Plugins" settings page
- Enjoy!
This will produce a main.js
file inside the repository folder, which Obsidian can open and make use of directly.
There's nothing else you need to do.
The most basic usage of this Obsidian plugin is to simply create a code block and hit run!
For example, when provided with the following code block inside Obsidian:
```python
print("Hello, World!")
```
the plugin create a button which runs the code!
This will also work for a huge number of other languages, such as C++. Simply change the language used in the code block:
In addition to providing the simple interface shown above, this plugin also allows you to specify more advanced arguments at the top of your program using a JSON string.
To specify these arguments, place the JSON object inside some <compile> ... </compile>
tags at the top of your
program.
For example:
<compile>
{
"mode": "run",
"language": "c++",
"libraries": [
{"id": "fmt", "version": "trunk"}
]
}
</compile>
// The rest of your program
The compiler (or interpreter/bytecode-generator) can be specified with the "compiler"
field inside the JSON object.
This must be a valid identifier for a Godbolt compiler. For example, "python311"
(Python 3.11) or "g95"
(g++ 9.5)
Exactly like specifying the compiler, the language can be specified with the "language"
element of the JSON object.
Libraries and tools can be specified with the "libraries"
and "tools"
options. The request libraries and tools must
be supported by the Godbolt Compiler Explorer.
More work is required on documenting valid values for these optoins. If anyone would like to help with this, it would be greatly appreciated!
The plugin also supports the generation of compiled/intermediate results, such as assembly language or bytecode. This
can be controlled by setting the "mode"
option to either "run"
or "asm"
, for running the code or generating
assembly respectively.
Thank you to Cy4Shot for helping me develop this plugin.