# blog-cells

> A tool for turning blog posts into interactive code notebooks.

Latest version **0.7.1** (published 2023-09-08) · 0 weekly downloads

## Install

```sh
npm install blog-cells
pnpm add blog-cells
yarn add blog-cells
bun add blog-cells
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.7.1 |
| Published | 2023-09-08 |
| First published | 2023-08-18 |
| Weekly downloads | 0 |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 9 |
| Unpacked size | 2.6 MB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 501 |
| Maintainers | vramesh |

## Links

- npm: https://www.npmjs.com/package/blog-cells
- Repository: https://github.com/rameshvarun/blog-cells
- Homepage: https://rameshvarun.github.io/blog-cells/
- Issues: https://github.com/rameshvarun/blog-cells/issues
- npm.io page: https://npm.io/package/blog-cells

## Dependencies (9)

- [react](https://npm.io/package/react.md) ^18.2.0
- [react-dom](https://npm.io/package/react-dom.md) ^18.2.0
- [codemirror](https://npm.io/package/codemirror.md) ^6.0.1
- [@codemirror/view](https://npm.io/package/@codemirror/view.md) ^6.16.0
- [@babel/standalone](https://npm.io/package/@babel/standalone.md) ^7.22.13
- [@codemirror/language](https://npm.io/package/@codemirror/language.md) ^6.9.0
- [@types/babel__standalone](https://npm.io/package/@types/babel__standalone.md) ^7.1.4
- [@codemirror/theme-one-dark](https://npm.io/package/@codemirror/theme-one-dark.md) ^6.1.2
- [@codemirror/lang-javascript](https://npm.io/package/@codemirror/lang-javascript.md) ^6.1.9

## Recent versions

- 0.7.1 (latest) — 2023-09-08
- 0.7.0 — 2023-09-08
- 0.6.0 — 2023-09-06
- 0.5.0 — 2023-08-31
- 0.4.1 — 2023-08-18
- 0.4.0 — 2023-08-18

## 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)

<a href="https://rameshvarun.github.io/blog-cells/">
<p align="center"><img width="400px" src="./screenshot.png"></img></p>
<p align="center">[VIEW DEMO]</p>
</a>

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 `<script type="text/notebook-cell">` 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/blog-cells@0.7.1/dist/blog-cells.css" />
<script src="https://cdn.jsdelivr.net/npm/blog-cells@0.7.1/dist/blog-cells.js"></script>

<!-- Create your code cells in script tags -->
<script type="text/notebook-cell">
console.log("Hello World!");
</script>
```

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
<script type="text/notebook-cell" data-kernel="python">
print("Hello World!");
</script>
```

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 `<pre>` tags instead of `<script>` 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/blog-cells@0.7.1/dist/blog-cells.css" />
<script src="https://cdn.jsdelivr.net/npm/blog-cells@0.7.1/dist/blog-cells.js"></script>

<!-- Define custom kernel -->
<script>
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());
</script>
  
<!-- Use custom Kernel -->
<script type="text/notebook-cell" data-kernel="json-format">
[4, {"hello": 3}]
</script>
```

## 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/

---
_Source: https://npm.io/package/blog-cells · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
