npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@alex.garcia/unofficial-observablehq-compiler

v0.6.0-alpha.9

Published

An unofficial compiler to bind @observablehq/parser and @observablehq/runtime

Downloads

61

Readme

unofficial-observablehq-compiler

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

This library has two parts: The Interpreter and the Compiler. The Interpreter will interpret "Observable syntax" into "javascript syntax" live in a javascript environment. For example:

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

async function main() {
  const runtime = new Runtime();
  const main = runtime.module();
  const observer = Inspector.into(document.body);
  const interpret = new Intepreter({ module: main, observer });

  await interpret.module(`
import {text} from '@jashkenas/inputs'

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

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

`);
}
main();

The Compiler will compile "Observable syntax" into javascript source code (as an ES module).

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

API Reference

new Interpreter(params)

Interpreter is a class that encompasses all logic to interpret Observable js code. params is an optional object with the following allowed configuration:

resolveImportPath(path, specifers): An async function that resolves to the define definition for a notebook. path is the string provided in the import statement, and specifiers is a array of string if the imported cell name specifiers in the statement (cells inside import {...}). specifiers is useful when implementing tree-shaking. For example, import {chart as CHART} from "@d3/bar-chart" would supply path="@d3/bar-chart" and specifiers=["chart"]. Default imports from observablehq.com, eg https://api.observablehq.com/@d3/bar-chart.js?v=3 resolveFileAttachments: A function that, given the name of a FileAttachment, returns the URL that the FileAttachment will be fetched from. Defaults to name => name. defineImportMarkdown: A boolean, whether to define a markdown description cell for imports in the notebook. Defaults true. observeViewofValues: A boolean, whether to pass in an observer for the value of viewof cells. module: A default Observable runtime module that will be used in operations such as .cell and .module, if no other module is passed in. observer: A default Observable runtime observer that will be used in operations such as .cell and .module, if no other observer is passed in.

Keep in mind, there is no sandboxing done, so it has the same security implications as eval().

interpret.cell(source [, module, observer])

Parses the given string source with the Observable parser .parseCell() and interprets the source, passing it and the observer along to the Observable module. Returns a Promise that resolves to an array of runtime variables that were defined when interpreting the source. More than one variable can be defined with a single cell, like with viewof, mutable, and import cells. source can also be a pre-parsed cell instead of source code.

interpret.module(source [, module, observer])

Parses the given string source with the Observable parser .parseModule() and interprets the source, passing it and the observer along to the Observable module. Returns a Promise that resolves to an array of an array of runtime variables that were defined when interpreting the source. source can also be a pre-parsed program instead of source code.

interpret.notebook(source [, module, observer])

TODO

new Compiler(params)

Compiler is a class that encompasses all logic to compile Observable javascript code into vanilla Javascript code, as an ES module. params is an optional object with the following allowed configuration.

resolveImportPath(path, specifers): A function that returns a URL to where the notebook is defined. path is the string provided in the import statement, and specifiers is a array of string if the imported cell name specifiers in the statement (cells inside import {...}). specifiers is useful when implementing tree-shaking. For example, import {chart as CHART} from "@d3/bar-chart" would supply path="@d3/bar-chart" and specifiers=["chart"]. Default imports from observablehq.com, eg https://api.observablehq.com/@d3/bar-chart.js?v=3

resolveFileAttachments: A function that, given the name of a FileAttachment, returns the URL that the FileAttachment will be fetched from. Defaults to name => name.

UNSAFE_allowJavascriptFileAttachments A boolean. When true, the resolveFileAttachments function will resolve to raw JavaScript when calculating the value of a FileAttachment reference. This is useful if you need to use new URL or import.meta.url when determining where a FileAttachment url should resolve too. This is unsafe because the Compiler will not escape any quotes when including it in the compiled output, so do use with extreme caution when dealing with user input.


// This can be unsafe since FileAttachment names can include quotes.
// Instead, map file attachments names to something deterministic and escape-safe,
// like SHA hashes.
const resolveFileAttachments = name => `new URL("./files/${name}", import.meta.url)`

Compiled output when:

// UNSAFE_allowJavascriptFileAttachments == false
const fileAttachments = new Map([["a", "new URL(\"./files/a\", import.meta.url)"]]);

// UNSAFE_allowJavascriptFileAttachments == true
const fileAttachments = new Map([["a", new URL("./files/a", import.meta.url)]]);

defineImportMarkdown - A boolean, whether to define a markdown description cell for imports in the notebook. Defaults true.

observeViewofValues - A boolean, whether or not to pass in the observer function for viewof value cells. Defaults true.

observeMutableValues - A boolean, whether or not to pass in the observer function for mutable value cells. Defaults true.

compile.module(source)

TODO

compile.notebook(source)

TODO

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 :smile: