@alex.garcia/unofficial-observablehq-compiler CircleCI

An unoffical compiler for Observable notebooks (glue between the Observable parser and runtime)

This compiler will compile "observable syntax" into "javascript syntax". For example -

import compiler from "@alex.garcia/unofficial-observablehq-compiler";
import { Inspector, Runtime } from "@observablehq/runtime";

const compile = new compiler.Compiler();

const define = compile.module(`
import {text} from '@jashkenas/inputs'

viewof name = text({
  title: "what's your name?",
  value: ''
})

md\`Hello **\${name}**, it's nice to meet you!\`

`);

const runtime = new Runtime();

const module = runtime.module(define, Inpsector.into(document.body));

For more live examples and functionality, take a look at the announcement notebook and this test page.

API Reference

Compiler

# new Compiler(resolve = defaultResolver) <>

Returns a new compiler. resolve is an optional function that, given a path string, will resolve a new define function for a new module. This is used when the compiler comes across an import statement - for example:

import {chart} from "@d3/bar-chart"

In this case, resolve gets called with path="@d3/bar-chart". The defaultResolver function will lookup the given path on observablehq.com and return the define function to define that notebook.

For example, if you have your own set of notebooks on some other server, you could use something like:

const resolve = path =>
  import(`other.server.com/notebooks/${path}.js`).then(
    module => module.default
  );

const compile = new Compiler(resolve);

#compile.module(contents)

Returns a define function. contents is a string that defines a "module", which is a list of "cells" (both defintions from @observablehq/parser). It must be compatible with parseModule.

For example:

const define = compile.module(`a = 1
b = 2
c = a + b`);

You can now use define with the Observable runtime:

const runtime = new Runtime();
const main = runtime.module(define, Inspector.into(document.body));

#compile.cell(contents)

WARNING this isn't implemented yet! I'm not 100% sure how to structure it :/

License

This library is MIT, but it relies and gets heavy inspiration from the following libraries licensed under ISC:

Contributing

Feel free to send in PR's as you wish! Take a look at the issues to find something to work on. Just please follow the Contributor Covenant in all your interactions 😄