Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/rameshvarun/blog-cells

Add interactive code snippets to any blog or webpage.
https://github.com/rameshvarun/blog-cells

blogging javascript literate-programming live-coding notebooks python typescript

Last synced: about 2 months ago
JSON representation

Add interactive code snippets to any blog or webpage.

Awesome Lists containing this project

README

        

# blog-cells
[![Node.js CI](https://github.com/rameshvarun/blog-cells/actions/workflows/node.js.yml/badge.svg)](https://github.com/rameshvarun/blog-cells/actions/workflows/node.js.yml)
[![npm](https://img.shields.io/npm/v/blog-cells)](https://www.npmjs.com/package/blog-cells)



[VIEW DEMO]


Add interactive code cells to any webpage, similar to [Jupyter](https://jupyter.org/) or [ObservableHQ](https://observablehq.com/). Works with direct HTML editing, static site generators like Jekyll / Hugo, and more.

## Usage

### Quickstart

Just drop in JS / CSS imports and start creating code cells using `` elements. <b>blog-cells</b> will transform these script tags into interactive, runnable code snippets.

```html
<!-- Import blog-cells JS and CSS files. -->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/blog-cells.css" />
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/blog-cells.js">

console.log("Hello World!");

```

Try it on [CodePen](https://codepen.io/varunramesh/pen/WNYVNQQ) or [JSFiddle](https://jsfiddle.net/varunramesh/o217xpc5/latest/).

### Other Languages

In addition to JavaScript, you can also embed code in other languages by adding a `data-kernel` attribute.

```html

print("Hello World!")

```

The following kernel values are currently supported:
- `javascript` (Default)
- `python`

### Cell Attributes

Cells can be configured with the following attributes:

- `data-autorun="true"` - Automatically run a cell on page load. Autorun cells are run in the order that they appear on the page.
- `data-hidden="true"` - Make a cell hidden by default - readers can toggle the cell's visibility.

### Using `

` tags instead of `` tags

Script tags are great for defining notebook cells since they can hold pretty much any code without escaping. However, you can also use `<pre class="notebook-cell">` tags instead. When using `pre` tags, reserved HTML characters should be escaped using HTML entities (this can be done by your static site generator).

```html
<pre class="notebook-cell">
console.log("&lt;b&gt;HELLO&lt;/b&gt;");
</pre>
```

### Creating a Custom Kernel

You can easily define and use your own custom kernels.

```html
<!-- Import blog-cells JS and CSS files. -->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/blog-cells.css" />
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/blog-cells.js">

class JSONFormatKernel extends BlogCells.Kernel {
async run(code, onOutput) {
const data = JSON.parse(code.trim());
onOutput({ type: "log", line: JSON.stringify(data, undefined, 2) });
}
}
BlogCells.registerKernel("json-format", () => new JSONFormatKernel());

[4, {"hello": 3}]

```

## Developing

```bash
git clone https://github.com/rameshvarun/blog-cells.git
cd blog-cells
npm install
npm start
```

## Attributions

This repo contains assets from other open source projects.

- [https://github.com/SamHerbert/SVG-Loaders](https://github.com/SamHerbert/SVG-Loaders) (MIT)

## Alternatives
- https://starboard.gg/
- https://observablehq.com/
- https://jupyter.org/try-jupyter/lab/
- https://www.typecell.org/