/ob-deno

Babel Functions for Javascript/TypeScript with Deno.

Primary LanguageEmacs LispGNU General Public License v3.0GPL-3.0

ob-deno

https://melpa.org/packages/ob-deno-badge.svg

Babel Functions for Javascript/TypeScript with Deno.

ob-deno is based on ob-js. (However, there is no session support.)

Requirements

Installation

ob-deno can be installed from MELPA.

M-x package-install RET ob-deno

Setup

(require 'ob-deno)

(add-to-list 'org-babel-load-languages '(deno . t))
(org-babel-do-load-languages 'org-babel-load-languages org-babel-load-languages)

;; optional (required the typescript.el)
(add-to-list 'org-src-lang-modes '("deno" . typescript))

Customization

M-x customize-group RET ob-deno RET

  • Ob Deno Cmd
  • Ob Deno Variable Prefix

Usage

The results parameter is default value.

#+begin_src deno
const arr = [1, 2, 3];
return arr;
#+end_src

#+RESULTS:
| 1 | 2 | 3 |

The results parameter is output.

#+begin_src deno :results output
const arr = [1, 2, 3];
console.log(arr);
#+end_src

#+RESULTS:
: [ 1, 2, 3 ]

Specify variables.

#+begin_src deno :var foo="bar" baz='("qux")
return {foo, baz};
#+end_src

#+RESULTS:
: { foo: "bar", baz: [ "qux" ] }

Run with --allow-net. (equivalent to deno run --allow-net)

#+begin_src deno :allow net :results output
  const response = await fetch("https://httpbin.org/get?hello=world");
  const json = await response.json();

  console.log(json.args);
#+end_src

#+RESULTS:
: { hello: "world" }

More complex. (equivalent to deno run --allow-net=httpbin.org,example.com --allow-env)

#+begin_src deno :allow '((net . (httpbin.org example.com)) env) :results output
  const response = await fetch(`https://httpbin.org/get?user=${Deno.env.get("USER")}`);
  const json = await response.json();

  console.log(json.args);
#+end_src

#+RESULTS:
: { user: "taiju" }

It also supports imports and table inputs. You can use M-x org-babel-expand-src-block to check the generated code.

#+NAME: writing-speed-practice
| Date         | Speed     | Accuracy |
|--------------+-----------+----------|
| [2024-10-26] | 73.6 WPM  |      91% |
| [2024-10-27] | 73.75 WPM |    88.5% |
| [2024-10-28] | 76.2 WPM  |    92.5% |
| [2024-11-01] | 75.66 WPM |   90.05% |
| [2024-11-02] | 77.69 WPM |   90.76% |
| [2024-11-04] | 79.3 WPM  |    91.6% |
| [2024-11-10] | 78.9 WPM  |    91.3% |

#+begin_src deno :allow 'all :var data=writing-speed-practice :file /tmp/plot.svg :results output file
import * as Plot from "npm:@observablehq/plot";
import { JSDOM } from "npm:jsdom";

data.forEach((x) => {
  x.date = new Date(x.date.replace(/[\[\]]/g, ""));
  x.speed = parseFloat(x.speed);
  x.accuracy = parseFloat(x.accuracy);
});

const plot = Plot.plot({
  document: new JSDOM("").window.document,
  legend: true,
  grid: true,
  margin: 50,
  marks: [
    Plot.line(data, {x: "date", y: "speed", stroke: "blue"}),
    Plot.line(data, {x: "date", y: "accuracy", stroke: "red",}),
  ],
});

plot.setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns", "http://www.w3.org/2000/svg");
plot.setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns:xlink", "http://www.w3.org/1999/xlink");

console.log(plot.outerHTML);
#+end_src

#+RESULTS:
[[file:/tmp/plot.svg]]