https://github.com/bencode/logseq-runit-plugin
A Logseq plugin for running code
https://github.com/bencode/logseq-runit-plugin
Last synced: 8 months ago
JSON representation
A Logseq plugin for running code
- Host: GitHub
- URL: https://github.com/bencode/logseq-runit-plugin
- Owner: bencode
- License: mit
- Created: 2025-06-24T04:35:25.000Z (9 months ago)
- Default Branch: main
- Last Pushed: 2025-07-01T12:33:16.000Z (9 months ago)
- Last Synced: 2025-07-01T13:23:32.627Z (9 months ago)
- Language: TypeScript
- Homepage:
- Size: 3.62 MB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# logseq-runit-plugin
A Logseq plugin for running code snippets directly inside your notes.
Supports JavaScript, Python (via [Pyodide](https://pyodide.org/)), Scheme (via [BiwaScheme](https://www.biwascheme.org/)), and Clojure (via [SCI](https://github.com/babashka/sci)) with interactive output.
Built for [Logseq](https://logseq.com/).

## Features
- **Multi-language support**: Run JavaScript, Python, Scheme, and Clojure code blocks
- **Console output capture**: See `console.log`/`print` output inline
- **Last expression result**: Automatically displays the result of the last expression, similar to Jupyter notebooks
- **Dynamic imports**: Supports dynamic module imports for JavaScript
- **Easy insertion**: Use the `/Create Runit Snippet` slash command to insert runnable code blocks
## Installation
Install the plugin directly from the Logseq Marketplace:
1. Open Logseq
2. Go to Settings → Plugins
3. Click "Marketplace"
4. Search for "runit"
5. Click "Install" on the logseq-runit-plugin
## Usage
1. Use the `/Create Runit Snippet` command to insert a code block
2. Write your code in JavaScript, Python, Scheme, or Clojure inside the block
3. The output and console logs will be displayed below the code
**Note:** When using the `/Create Runit Snippet` command, ensure the generated macro remains attached to the current block. Use Shift+Enter for line breaks within the same block before running the slash command, rather than creating a new separate block.
### Importing External JavaScript Modules
You can import external JavaScript modules using the `$import` function:
```js
const R = await $import('https://esm.sh/ramda')
R.map(v => v + 1)([1, 2, 3, 4])
// output
Array [
2,
3,
4,
5,
]
```
The `$import` function supports both URLs and module names, allowing you to use libraries from CDNs like [esm.sh](https://esm.sh/) directly in your code snippets.
### Google Charts Integration
You can create interactive charts using Google Charts:
```js
const chart = await $import('google-charts')
const data = [
['Task', 'Hours per Day'],
['Work', 11],
['Eat', 2],
['Commute', 2],
['Watch TV', 2],
['Sleep', 7]
]
const options = {
title: 'My Daily Activities',
width: 500,
height: 300
}
chart.render('PieChart', data, options)
```

The Google Charts integration supports various chart types including PieChart, BarChart, LineChart, ScatterChart, Gauge, GeoChart, and Table. Charts are rendered as interactive SVG elements directly in your notes.
For more chart types and configuration options, see the [Google Charts Gallery](https://developers.google.com/chart/interactive/docs/gallery).
### Python Support
The plugin supports running Python code via [Pyodide](https://pyodide.org/), bringing the full Python scientific computing stack to your browser.
#### Basic Python Usage
```python
#% pip install numpy
import numpy as np
# Create a simple array
arr = np.array([1, 2, 3, 4, 5])
print("Array:", arr)
print("Sum:", np.sum(arr))
print("Mean:", np.mean(arr))
# Return the result
arr * 2
```
#### Creating Plots with Matplotlib
```python
#% pip install matplotlib numpy
import matplotlib.pyplot as plt
import numpy as np
# Create data
x = np.linspace(0, 10, 100)
y1 = np.sin(x)
y2 = np.cos(x)
# Create the plot
plt.figure(figsize=(10, 6))
plt.plot(x, y1, 'b-', linewidth=2, label='sin(x)')
plt.plot(x, y2, 'r--', linewidth=2, label='cos(x)')
plt.xlabel('x')
plt.ylabel('y')
plt.title('Trigonometric Functions')
plt.grid(True, alpha=0.3)
plt.legend()
# Render the plot
plt_show()
```

### Scheme Support
The plugin supports running Scheme code snippets via BiwaScheme, allowing you to execute and view results conveniently in your notes:
```scheme
(define (fib n)
(if (< n 2) n
(+ (fib (- n 1)) (fib (- n 2)))))
(fib 8)
```
### Clojure Support
This plugin supports running Clojure code snippets via sci (Small Clojure Interpreter), allowing you to execute ClojureScript code directly in your notes:
```clojure
(defn factorial [n]
(if (<= n 1)
1
(* n (factorial (dec n)))))
(factorial 5)
```
SCI provides a safe, sandboxed environment for executing Clojure code with most of the core ClojureScript functionality available.
## Development
- Clone the repo and run `pnpm install`
- Use `pnpm dev` to start development with hot reload
- Build for production with `pnpm build`
## Acknowledgments
Special thanks to [Klipse](https://github.com/viebel/klipse) for inspiration.
Originally, I wanted to integrate Klipse directly, but due to some technical limitations, I implemented a simplified "code runner" based on some of Klipse's ideas.
**Note:** Currently, code execution happens when the code editor loses focus, which is less interactive compared to Klipse.
## License
MIT