@observablehq/notebook-kit 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +13 -0
- package/README.md +7 -0
- package/dist/bin/build.d.ts +2 -0
- package/dist/bin/build.js +63 -0
- package/dist/bin/download.d.ts +2 -0
- package/dist/bin/download.js +49 -0
- package/dist/bin/notebooks.d.ts +2 -0
- package/dist/bin/notebooks.js +33 -0
- package/dist/bin/preview.d.ts +2 -0
- package/dist/bin/preview.js +48 -0
- package/dist/package.json +73 -0
- package/dist/src/index.d.ts +5 -0
- package/dist/src/index.js +5 -0
- package/dist/src/javascript/assignments.d.ts +2 -0
- package/dist/src/javascript/assignments.js +37 -0
- package/dist/src/javascript/assignments.test.d.ts +1 -0
- package/dist/src/javascript/assignments.test.js +33 -0
- package/dist/src/javascript/awaits.d.ts +2 -0
- package/dist/src/javascript/awaits.js +23 -0
- package/dist/src/javascript/awaits.test.d.ts +1 -0
- package/dist/src/javascript/awaits.test.js +22 -0
- package/dist/src/javascript/declarations.d.ts +2 -0
- package/dist/src/javascript/declarations.js +45 -0
- package/dist/src/javascript/files.d.ts +3 -0
- package/dist/src/javascript/files.js +18 -0
- package/dist/src/javascript/globals.d.ts +1 -0
- package/dist/src/javascript/globals.js +86 -0
- package/dist/src/javascript/imports/jsr.d.ts +2 -0
- package/dist/src/javascript/imports/jsr.js +6 -0
- package/dist/src/javascript/imports/npm.d.ts +2 -0
- package/dist/src/javascript/imports/npm.js +47 -0
- package/dist/src/javascript/imports/npm.test.d.ts +1 -0
- package/dist/src/javascript/imports/npm.test.js +32 -0
- package/dist/src/javascript/imports/observable.d.ts +8 -0
- package/dist/src/javascript/imports/observable.js +64 -0
- package/dist/src/javascript/imports/observable.test.d.ts +1 -0
- package/dist/src/javascript/imports/observable.test.js +13 -0
- package/dist/src/javascript/imports.d.ts +21 -0
- package/dist/src/javascript/imports.js +146 -0
- package/dist/src/javascript/observable.d.ts +2 -0
- package/dist/src/javascript/observable.js +72 -0
- package/dist/src/javascript/parse.d.ts +11 -0
- package/dist/src/javascript/parse.js +53 -0
- package/dist/src/javascript/references.d.ts +8 -0
- package/dist/src/javascript/references.js +129 -0
- package/dist/src/javascript/references.test.d.ts +1 -0
- package/dist/src/javascript/references.test.js +38 -0
- package/dist/src/javascript/sourcemap.d.ts +21 -0
- package/dist/src/javascript/sourcemap.js +143 -0
- package/dist/src/javascript/sourcemap.test.d.ts +1 -0
- package/dist/src/javascript/sourcemap.test.js +88 -0
- package/dist/src/javascript/strings.d.ts +8 -0
- package/dist/src/javascript/strings.js +42 -0
- package/dist/src/javascript/strings.test.d.ts +1 -0
- package/dist/src/javascript/strings.test.js +31 -0
- package/dist/src/javascript/syntaxError.d.ts +2 -0
- package/dist/src/javascript/syntaxError.js +5 -0
- package/dist/src/javascript/template.d.ts +3 -0
- package/dist/src/javascript/template.js +141 -0
- package/dist/src/javascript/template.test.d.ts +1 -0
- package/dist/src/javascript/template.test.js +32 -0
- package/dist/src/javascript/transpile.d.ts +25 -0
- package/dist/src/javascript/transpile.js +42 -0
- package/dist/src/javascript/transpile.test.d.ts +1 -0
- package/dist/src/javascript/transpile.test.js +29 -0
- package/dist/src/javascript/walk.d.ts +5 -0
- package/dist/src/javascript/walk.js +13 -0
- package/dist/src/lib/notebook.d.ts +35 -0
- package/dist/src/lib/notebook.js +19 -0
- package/dist/src/lib/notebook.test.d.ts +1 -0
- package/dist/src/lib/notebook.test.js +26 -0
- package/dist/src/lib/serialize.d.ts +5 -0
- package/dist/src/lib/serialize.js +97 -0
- package/dist/src/lib/serialize.test.d.ts +1 -0
- package/dist/src/lib/serialize.test.js +125 -0
- package/dist/src/lib/text.d.ts +1 -0
- package/dist/src/lib/text.js +3 -0
- package/dist/src/runtime/define.d.ts +28 -0
- package/dist/src/runtime/define.js +62 -0
- package/dist/src/runtime/display.d.ts +16 -0
- package/dist/src/runtime/display.js +60 -0
- package/dist/src/runtime/index.d.ts +11 -0
- package/dist/src/runtime/index.js +14 -0
- package/dist/src/runtime/inspect.d.ts +3 -0
- package/dist/src/runtime/inspect.js +43 -0
- package/dist/src/runtime/stdlib/assets.d.ts +4 -0
- package/dist/src/runtime/stdlib/assets.js +110 -0
- package/dist/src/runtime/stdlib/assets.test.d.ts +1 -0
- package/dist/src/runtime/stdlib/assets.test.js +78 -0
- package/dist/src/runtime/stdlib/dom/canvas.d.ts +1 -0
- package/dist/src/runtime/stdlib/dom/canvas.js +6 -0
- package/dist/src/runtime/stdlib/dom/context2d.d.ts +1 -0
- package/dist/src/runtime/stdlib/dom/context2d.js +9 -0
- package/dist/src/runtime/stdlib/dom/index.d.ts +5 -0
- package/dist/src/runtime/stdlib/dom/index.js +5 -0
- package/dist/src/runtime/stdlib/dom/svg.d.ts +1 -0
- package/dist/src/runtime/stdlib/dom/svg.js +7 -0
- package/dist/src/runtime/stdlib/dom/text.d.ts +1 -0
- package/dist/src/runtime/stdlib/dom/text.js +3 -0
- package/dist/src/runtime/stdlib/dom/uid.d.ts +8 -0
- package/dist/src/runtime/stdlib/dom/uid.js +25 -0
- package/dist/src/runtime/stdlib/dot.d.ts +2 -0
- package/dist/src/runtime/stdlib/dot.js +34 -0
- package/dist/src/runtime/stdlib/duckdb.d.ts +24 -0
- package/dist/src/runtime/stdlib/duckdb.js +379 -0
- package/dist/src/runtime/stdlib/fileAttachment.d.ts +71 -0
- package/dist/src/runtime/stdlib/fileAttachment.js +199 -0
- package/dist/src/runtime/stdlib/generators/index.d.ts +5 -0
- package/dist/src/runtime/stdlib/generators/index.js +5 -0
- package/dist/src/runtime/stdlib/generators/input.d.ts +1 -0
- package/dist/src/runtime/stdlib/generators/input.js +45 -0
- package/dist/src/runtime/stdlib/generators/now.d.ts +1 -0
- package/dist/src/runtime/stdlib/generators/now.js +5 -0
- package/dist/src/runtime/stdlib/generators/observe.d.ts +1 -0
- package/dist/src/runtime/stdlib/generators/observe.js +31 -0
- package/dist/src/runtime/stdlib/generators/queue.d.ts +1 -0
- package/dist/src/runtime/stdlib/generators/queue.js +27 -0
- package/dist/src/runtime/stdlib/generators/width.d.ts +1 -0
- package/dist/src/runtime/stdlib/generators/width.js +13 -0
- package/dist/src/runtime/stdlib/highlight.d.ts +2 -0
- package/dist/src/runtime/stdlib/highlight.js +76 -0
- package/dist/src/runtime/stdlib/index.d.ts +56 -0
- package/dist/src/runtime/stdlib/index.js +23 -0
- package/dist/src/runtime/stdlib/inputs.css +15 -0
- package/dist/src/runtime/stdlib/inputs.d.ts +2 -0
- package/dist/src/runtime/stdlib/inputs.js +2 -0
- package/dist/src/runtime/stdlib/leaflet.d.ts +1 -0
- package/dist/src/runtime/stdlib/leaflet.js +7 -0
- package/dist/src/runtime/stdlib/mapboxgl.d.ts +1 -0
- package/dist/src/runtime/stdlib/mapboxgl.js +5 -0
- package/dist/src/runtime/stdlib/md.d.ts +5 -0
- package/dist/src/runtime/stdlib/md.js +72 -0
- package/dist/src/runtime/stdlib/mermaid.d.ts +2 -0
- package/dist/src/runtime/stdlib/mermaid.js +11 -0
- package/dist/src/runtime/stdlib/mutable.d.ts +8 -0
- package/dist/src/runtime/stdlib/mutable.js +30 -0
- package/dist/src/runtime/stdlib/observer.d.ts +16 -0
- package/dist/src/runtime/stdlib/observer.js +42 -0
- package/dist/src/runtime/stdlib/recommendedLibraries.d.ts +25 -0
- package/dist/src/runtime/stdlib/recommendedLibraries.js +26 -0
- package/dist/src/runtime/stdlib/require.d.ts +4 -0
- package/dist/src/runtime/stdlib/require.js +40 -0
- package/dist/src/runtime/stdlib/sampleDatasets.d.ts +12 -0
- package/dist/src/runtime/stdlib/sampleDatasets.js +31 -0
- package/dist/src/runtime/stdlib/sql.d.ts +5 -0
- package/dist/src/runtime/stdlib/sql.js +5 -0
- package/dist/src/runtime/stdlib/template.d.ts +7 -0
- package/dist/src/runtime/stdlib/template.js +2 -0
- package/dist/src/runtime/stdlib/tex.d.ts +7 -0
- package/dist/src/runtime/stdlib/tex.js +18 -0
- package/dist/src/runtime/stdlib/vega-lite.d.ts +1 -0
- package/dist/src/runtime/stdlib/vega-lite.js +4 -0
- package/dist/src/styles/abstract-dark.css +14 -0
- package/dist/src/styles/abstract-light.css +14 -0
- package/dist/src/styles/global.css +266 -0
- package/dist/src/styles/highlight.css +47 -0
- package/dist/src/styles/index.css +14 -0
- package/dist/src/styles/inspector.css +89 -0
- package/dist/src/styles/plot.css +7 -0
- package/dist/src/styles/syntax-dark.css +12 -0
- package/dist/src/styles/syntax-light.css +12 -0
- package/dist/src/styles/theme-air.css +7 -0
- package/dist/src/styles/theme-coffee.css +7 -0
- package/dist/src/styles/theme-cotton.css +7 -0
- package/dist/src/styles/theme-deep-space.css +16 -0
- package/dist/src/styles/theme-glacier.css +7 -0
- package/dist/src/styles/theme-ink.css +7 -0
- package/dist/src/styles/theme-midnight.css +7 -0
- package/dist/src/styles/theme-near-midnight.css +7 -0
- package/dist/src/styles/theme-ocean-floor.css +7 -0
- package/dist/src/styles/theme-parchment.css +7 -0
- package/dist/src/styles/theme-slate.css +7 -0
- package/dist/src/styles/theme-stark.css +16 -0
- package/dist/src/styles/theme-sun-faded.css +7 -0
- package/dist/src/templates/default.html +31 -0
- package/dist/src/vite/config.d.ts +2 -0
- package/dist/src/vite/config.js +30 -0
- package/dist/src/vite/index.d.ts +2 -0
- package/dist/src/vite/index.js +2 -0
- package/dist/src/vite/observable.d.ts +12 -0
- package/dist/src/vite/observable.js +176 -0
- package/package.json +73 -0
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
@import url("./syntax-dark.css");
|
|
2
|
+
|
|
3
|
+
:root {
|
|
4
|
+
--theme-foreground: #ffffff;
|
|
5
|
+
--theme-foreground-focus: #fff61f;
|
|
6
|
+
--theme-background-a: #000000;
|
|
7
|
+
--theme-background-b: color-mix(in srgb, var(--theme-foreground) 15%, var(--theme-background-a));
|
|
8
|
+
--theme-background: var(--theme-background-a);
|
|
9
|
+
--theme-background-alt: var(--theme-background-b);
|
|
10
|
+
--theme-foreground-alt: color-mix(in srgb, var(--theme-foreground) 90%, var(--theme-background-a));
|
|
11
|
+
--theme-foreground-muted: color-mix(in srgb, var(--theme-foreground) 60%, var(--theme-background-a));
|
|
12
|
+
--theme-foreground-faint: color-mix(in srgb, var(--theme-foreground) 50%, var(--theme-background-a));
|
|
13
|
+
--theme-foreground-fainter: color-mix(in srgb, var(--theme-foreground) 50%, var(--theme-background-a));
|
|
14
|
+
--theme-foreground-faintest: color-mix(in srgb, var(--theme-foreground) 50%, var(--theme-background-a));
|
|
15
|
+
color-scheme: dark;
|
|
16
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
<!doctype html>
|
|
2
|
+
<html lang="en">
|
|
3
|
+
<head>
|
|
4
|
+
<meta charset="utf-8">
|
|
5
|
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
6
|
+
<style type="text/css">
|
|
7
|
+
@import url("observable:styles/index.css");
|
|
8
|
+
@import "@fontsource/spline-sans-mono/400.css";
|
|
9
|
+
@import "@fontsource/spline-sans-mono/600.css";
|
|
10
|
+
@import "@fontsource/spline-sans-mono/700.css";
|
|
11
|
+
@import "@fontsource/spline-sans-mono/400-italic.css";
|
|
12
|
+
@import "@fontsource/spline-sans-mono/600-italic.css";
|
|
13
|
+
@import "@fontsource/spline-sans-mono/700-italic.css";
|
|
14
|
+
@import "@fontsource/source-serif-4/400.css";
|
|
15
|
+
@import "@fontsource/source-serif-4/700.css";
|
|
16
|
+
@import "@fontsource/source-serif-4/400-italic.css";
|
|
17
|
+
@import "@fontsource/source-serif-4/700-italic.css";
|
|
18
|
+
@import "@fontsource/inter/400.css";
|
|
19
|
+
@import "@fontsource/inter/500.css";
|
|
20
|
+
@import "@fontsource/inter/600.css";
|
|
21
|
+
@import "@fontsource/inter/700.css";
|
|
22
|
+
@import "@fontsource/inter/400-italic.css";
|
|
23
|
+
@import "@fontsource/inter/500-italic.css";
|
|
24
|
+
@import "@fontsource/inter/600-italic.css";
|
|
25
|
+
@import "@fontsource/inter/700-italic.css";
|
|
26
|
+
</style>
|
|
27
|
+
</head>
|
|
28
|
+
<body>
|
|
29
|
+
<main></main>
|
|
30
|
+
</body>
|
|
31
|
+
</html>
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { dirname, resolve } from "node:path";
|
|
2
|
+
import { fileURLToPath } from "node:url";
|
|
3
|
+
const __filename = fileURLToPath(import.meta.url);
|
|
4
|
+
const __dirname = dirname(__filename);
|
|
5
|
+
export function config() {
|
|
6
|
+
return {
|
|
7
|
+
base: "./",
|
|
8
|
+
esbuild: {
|
|
9
|
+
supported: {
|
|
10
|
+
"top-level-await": true
|
|
11
|
+
}
|
|
12
|
+
},
|
|
13
|
+
resolve: {
|
|
14
|
+
alias: [
|
|
15
|
+
{
|
|
16
|
+
find: /^npm:(.*)$/,
|
|
17
|
+
replacement: "https://cdn.jsdelivr.net/npm/$1/+esm"
|
|
18
|
+
},
|
|
19
|
+
{
|
|
20
|
+
find: /^jsr:(.*)$/,
|
|
21
|
+
replacement: "https://esm.sh/jsr/$1"
|
|
22
|
+
},
|
|
23
|
+
{
|
|
24
|
+
find: /^observable:(.*)$/,
|
|
25
|
+
replacement: resolve(__dirname, "../$1")
|
|
26
|
+
}
|
|
27
|
+
]
|
|
28
|
+
}
|
|
29
|
+
};
|
|
30
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { PluginOption } from "vite";
|
|
2
|
+
export interface ObservableOptions {
|
|
3
|
+
/** The global window, for the default parser and serializer implementations. */
|
|
4
|
+
window?: Pick<typeof globalThis, "DOMParser" | "XMLSerializer">;
|
|
5
|
+
/** The parser implementation; defaults to `new window.DOMParser()`. */
|
|
6
|
+
parser?: DOMParser;
|
|
7
|
+
/** The serializer implementation; defaults to `new window.XMLSerializer()`. */
|
|
8
|
+
serializer?: XMLSerializer;
|
|
9
|
+
/** The path to the page template; defaults to the default template. */
|
|
10
|
+
template?: string;
|
|
11
|
+
}
|
|
12
|
+
export declare function observable({ window, parser, serializer, template }?: ObservableOptions): PluginOption;
|
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
import { existsSync } from "node:fs";
|
|
2
|
+
import { readFile } from "node:fs/promises";
|
|
3
|
+
import { dirname, join, resolve } from "node:path";
|
|
4
|
+
import { fileURLToPath } from "node:url";
|
|
5
|
+
import { JSDOM } from "jsdom";
|
|
6
|
+
import { deserialize } from "../lib/serialize.js";
|
|
7
|
+
import { Sourcemap } from "../javascript/sourcemap.js";
|
|
8
|
+
import { transpile } from "../javascript/transpile.js";
|
|
9
|
+
import { parseTemplate } from "../javascript/template.js";
|
|
10
|
+
import { collectAssets } from "../runtime/stdlib/assets.js";
|
|
11
|
+
import { highlight } from "../runtime/stdlib/highlight.js";
|
|
12
|
+
import { MarkdownRenderer } from "../runtime/stdlib/md.js";
|
|
13
|
+
export function observable({ window = new JSDOM().window, parser = new window.DOMParser(), serializer = new window.XMLSerializer(), template = fileURLToPath(import.meta.resolve("../templates/default.html")) } = {}) {
|
|
14
|
+
return {
|
|
15
|
+
name: "observable",
|
|
16
|
+
buildStart() {
|
|
17
|
+
this.addWatchFile(template);
|
|
18
|
+
},
|
|
19
|
+
handleHotUpdate(context) {
|
|
20
|
+
if (context.file === resolve(template)) {
|
|
21
|
+
context.server.hot.send({ type: "full-reload" });
|
|
22
|
+
}
|
|
23
|
+
},
|
|
24
|
+
transformIndexHtml: {
|
|
25
|
+
order: "pre",
|
|
26
|
+
async handler(input, context) {
|
|
27
|
+
const notebook = deserialize(input, { parser });
|
|
28
|
+
const tsource = await readFile(template, "utf-8");
|
|
29
|
+
const document = parser.parseFromString(tsource, "text/html");
|
|
30
|
+
const statics = new Set();
|
|
31
|
+
const assets = new Set();
|
|
32
|
+
const md = MarkdownRenderer({ document });
|
|
33
|
+
const { version } = (await import("../../package.json", { with: { type: "json" } })).default;
|
|
34
|
+
let generator = document.querySelector("meta[name=generator]");
|
|
35
|
+
generator ?? (generator = document.head.appendChild(document.createElement("meta")));
|
|
36
|
+
generator.setAttribute("name", "generator");
|
|
37
|
+
generator.setAttribute("content", `Observable Notebook Kit v${version}`);
|
|
38
|
+
let title = document.querySelector("title");
|
|
39
|
+
title ?? (title = document.head.appendChild(document.createElement("title")));
|
|
40
|
+
title.insertBefore(document.createTextNode(notebook.title), title.firstChild);
|
|
41
|
+
let cells = document.querySelector("main");
|
|
42
|
+
cells ?? (cells = document.body.appendChild(document.createElement("main")));
|
|
43
|
+
for (const cell of notebook.cells) {
|
|
44
|
+
const { id, mode, pinned, value } = cell;
|
|
45
|
+
const contents = document.createDocumentFragment();
|
|
46
|
+
const div = contents.appendChild(document.createElement("div"));
|
|
47
|
+
div.id = `cell-${id}`;
|
|
48
|
+
div.className = "observablehq observablehq--cell";
|
|
49
|
+
if (mode === "md") {
|
|
50
|
+
const template = parseTemplate(value);
|
|
51
|
+
if (!template.expressions.length)
|
|
52
|
+
statics.add(cell);
|
|
53
|
+
const content = md([stripExpressions(template, value)]);
|
|
54
|
+
const codes = content.querySelectorAll("code[class^=language-]");
|
|
55
|
+
await Promise.all(Array.from(codes, highlight));
|
|
56
|
+
div.appendChild(content);
|
|
57
|
+
}
|
|
58
|
+
else if (mode === "html") {
|
|
59
|
+
const template = parseTemplate(value);
|
|
60
|
+
if (!template.expressions.length)
|
|
61
|
+
statics.add(cell);
|
|
62
|
+
div.innerHTML = stripExpressions(template, value);
|
|
63
|
+
}
|
|
64
|
+
collectAssets(assets, div);
|
|
65
|
+
if (pinned) {
|
|
66
|
+
const pre = contents.appendChild(document.createElement("pre"));
|
|
67
|
+
const code = pre.appendChild(document.createElement("code"));
|
|
68
|
+
code.className = `language-${mode}`;
|
|
69
|
+
code.textContent = value;
|
|
70
|
+
await highlight(code);
|
|
71
|
+
}
|
|
72
|
+
cells.appendChild(contents);
|
|
73
|
+
}
|
|
74
|
+
// Don’t error if assets are missing (matching Vite’s behavior).
|
|
75
|
+
filterMissingAssets(assets, dirname(context.filename));
|
|
76
|
+
const output = serializer.serializeToString(document);
|
|
77
|
+
const i = output.indexOf("</body>");
|
|
78
|
+
if (!(i >= 0))
|
|
79
|
+
throw new Error("body not found");
|
|
80
|
+
return (output.slice(0, i) +
|
|
81
|
+
`<style type="text/css">
|
|
82
|
+
@import url("observable:styles/theme-${notebook.theme}.css");
|
|
83
|
+
</style><script type="module">
|
|
84
|
+
import {define} from "observable:runtime/define";${Array.from(assets)
|
|
85
|
+
.map((asset, i) => `
|
|
86
|
+
import asset${i + 1} from ${JSON.stringify(`${asset}?url`)};`)
|
|
87
|
+
.join("")}${assets.size > 0
|
|
88
|
+
? `
|
|
89
|
+
|
|
90
|
+
const assets = new Map([
|
|
91
|
+
${Array.from(assets)
|
|
92
|
+
.map((asset, i) => ` [${JSON.stringify(asset)}, asset${i + 1}]`)
|
|
93
|
+
.join(",\n")}
|
|
94
|
+
]);`
|
|
95
|
+
: ""}
|
|
96
|
+
${notebook.cells
|
|
97
|
+
.filter((cell) => !statics.has(cell))
|
|
98
|
+
.map((cell) => {
|
|
99
|
+
const transpiled = transpile(cell.value, cell.mode, { resolveFiles: true });
|
|
100
|
+
return `
|
|
101
|
+
define(
|
|
102
|
+
{
|
|
103
|
+
root: document.getElementById(\`cell-${cell.id}\`),
|
|
104
|
+
expanded: [],
|
|
105
|
+
variables: []
|
|
106
|
+
},
|
|
107
|
+
{
|
|
108
|
+
id: ${cell.id},
|
|
109
|
+
body: ${escapeScript(transpiled.body)},
|
|
110
|
+
inputs: ${JSON.stringify(transpiled.inputs)},
|
|
111
|
+
outputs: ${JSON.stringify(transpiled.outputs)},
|
|
112
|
+
output: ${JSON.stringify(transpiled.output)},
|
|
113
|
+
assets: ${assets.size > 0 ? "assets" : "undefined"},
|
|
114
|
+
autodisplay: ${transpiled.autodisplay},
|
|
115
|
+
autoview: ${transpiled.autoview},
|
|
116
|
+
automutable: ${transpiled.automutable}
|
|
117
|
+
}
|
|
118
|
+
);`;
|
|
119
|
+
})
|
|
120
|
+
.join("")}
|
|
121
|
+
</script>` +
|
|
122
|
+
output.slice(i));
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
};
|
|
126
|
+
}
|
|
127
|
+
function filterMissingAssets(assets, dir) {
|
|
128
|
+
for (const asset of assets) {
|
|
129
|
+
if (!existsSync(join(dir, asset))) {
|
|
130
|
+
console.warn(`warning: asset not found: ${asset}`);
|
|
131
|
+
assets.delete(asset);
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
function stripExpressions(template, input) {
|
|
136
|
+
const source = new Sourcemap(input);
|
|
137
|
+
let index = template.start;
|
|
138
|
+
for (const q of template.quasis) {
|
|
139
|
+
if (q.start > index) {
|
|
140
|
+
// In a case such as <img src=${…} style=…>, we must replace the
|
|
141
|
+
// placeholder with a non-empty value or it will change the interpre-
|
|
142
|
+
// tation of the subsequent attribute to be part of the src attribute!
|
|
143
|
+
// But we also don’t want to use a non-empty src attribute because that
|
|
144
|
+
// would cause the browser to load an asset that does not exist (before
|
|
145
|
+
// it is replaced by the client-generated content).
|
|
146
|
+
if (hasPrecedingEquals(input, index)) {
|
|
147
|
+
source.replaceLeft(index, q.start, '""');
|
|
148
|
+
}
|
|
149
|
+
else {
|
|
150
|
+
source.delete(index, q.start);
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
index = q.end;
|
|
154
|
+
}
|
|
155
|
+
return String(source);
|
|
156
|
+
}
|
|
157
|
+
/** Returns true if the specified character is preceded by an equals sign, ignoring whitespace. */
|
|
158
|
+
function hasPrecedingEquals(input, index) {
|
|
159
|
+
let i = index - 1;
|
|
160
|
+
while (isSpaceCode(input.charCodeAt(i)))
|
|
161
|
+
--i;
|
|
162
|
+
return input.charCodeAt(i) === CODE_EQ;
|
|
163
|
+
}
|
|
164
|
+
const CODE_TAB = 9, CODE_LF = 10, CODE_FF = 12, CODE_CR = 13, CODE_SPACE = 32, CODE_EQ = 61;
|
|
165
|
+
/** Returns true if the specified character code is considered whitespace by HTML. */
|
|
166
|
+
function isSpaceCode(code) {
|
|
167
|
+
return (code === CODE_TAB ||
|
|
168
|
+
code === CODE_LF ||
|
|
169
|
+
code === CODE_FF ||
|
|
170
|
+
code === CODE_SPACE ||
|
|
171
|
+
code === CODE_CR);
|
|
172
|
+
}
|
|
173
|
+
/** Note: only suitable for use in a script element. */
|
|
174
|
+
function escapeScript(script) {
|
|
175
|
+
return script.replace(/<\/script>/g, "<\\/script>"); // TODO handle other contexts
|
|
176
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@observablehq/notebook-kit",
|
|
3
|
+
"license": "ISC",
|
|
4
|
+
"repository": {
|
|
5
|
+
"type": "git",
|
|
6
|
+
"url": "git+https://github.com/observablehq/notebook-kit.git"
|
|
7
|
+
},
|
|
8
|
+
"version": "1.0.0",
|
|
9
|
+
"type": "module",
|
|
10
|
+
"scripts": {
|
|
11
|
+
"test": "vitest",
|
|
12
|
+
"prepublishOnly": "rm -rf dist && tsc && chmod +x dist/bin/*.js && cp -r src/styles src/templates dist/src && cp src/runtime/stdlib/*.css dist/src/runtime/stdlib",
|
|
13
|
+
"lint": "tsc --noEmit && eslint bin src types",
|
|
14
|
+
"notebooks": "tsx bin/notebooks.ts",
|
|
15
|
+
"download": "tsx bin/notebooks.ts download",
|
|
16
|
+
"docs:preview": "tsx --watch bin/notebooks.ts preview --root docs --template docs/observable.tmpl",
|
|
17
|
+
"docs:build": "tsx bin/notebooks.ts build --root docs --template docs/observable.tmpl -- $(find docs -path 'docs/.observable' -prune -o -name '*.html' -print)"
|
|
18
|
+
},
|
|
19
|
+
"bin": {
|
|
20
|
+
"notebooks": "dist/bin/notebooks.js"
|
|
21
|
+
},
|
|
22
|
+
"files": [
|
|
23
|
+
"dist"
|
|
24
|
+
],
|
|
25
|
+
"exports": {
|
|
26
|
+
".": {
|
|
27
|
+
"types": "./dist/src/index.d.ts",
|
|
28
|
+
"import": "./dist/src/index.js"
|
|
29
|
+
},
|
|
30
|
+
"./runtime": {
|
|
31
|
+
"types": "./dist/src/runtime/index.d.ts",
|
|
32
|
+
"import": "./dist/src/runtime/index.js"
|
|
33
|
+
},
|
|
34
|
+
"./vite": {
|
|
35
|
+
"types": "./dist/src/vite/index.d.ts",
|
|
36
|
+
"import": "./dist/src/vite/index.js"
|
|
37
|
+
},
|
|
38
|
+
"./*.css": "./dist/src/styles/*.css"
|
|
39
|
+
},
|
|
40
|
+
"dependencies": {
|
|
41
|
+
"@fontsource/inter": "^5.2.6",
|
|
42
|
+
"@fontsource/source-serif-4": "^5.2.8",
|
|
43
|
+
"@fontsource/spline-sans-mono": "^5.2.6",
|
|
44
|
+
"@lezer/common": "^1.2.3",
|
|
45
|
+
"@lezer/css": "^1.2.1",
|
|
46
|
+
"@lezer/highlight": "^1.2.1",
|
|
47
|
+
"@lezer/html": "^1.3.10",
|
|
48
|
+
"@lezer/javascript": "^1.5.1",
|
|
49
|
+
"@lezer/markdown": "^1.4.3",
|
|
50
|
+
"@observablehq/inspector": "^5.0.1",
|
|
51
|
+
"@observablehq/parser": "^6.1.0",
|
|
52
|
+
"@observablehq/runtime": "^6.0.0",
|
|
53
|
+
"@sindresorhus/slugify": "^2.2.1",
|
|
54
|
+
"acorn": "^8.15.0",
|
|
55
|
+
"acorn-walk": "^8.3.4",
|
|
56
|
+
"markdown-it": "^14.1.0",
|
|
57
|
+
"markdown-it-anchor": "^9.2.0",
|
|
58
|
+
"vite": "^7.0.0"
|
|
59
|
+
},
|
|
60
|
+
"devDependencies": {
|
|
61
|
+
"@eslint/js": "^9.29.0",
|
|
62
|
+
"@types/jsdom": "^21.1.7",
|
|
63
|
+
"@types/markdown-it": "^14.1.2",
|
|
64
|
+
"eslint": "^9.29.0",
|
|
65
|
+
"globals": "^16.2.0",
|
|
66
|
+
"htl": "^0.3.1",
|
|
67
|
+
"jsdom": "^26.1.0",
|
|
68
|
+
"tsx": "^4.20.3",
|
|
69
|
+
"typescript": "^5.8.3",
|
|
70
|
+
"typescript-eslint": "^8.35.0",
|
|
71
|
+
"vitest": "^3.2.4"
|
|
72
|
+
}
|
|
73
|
+
}
|