@tinyvolt/pie 0.9.0 → 0.11.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/README.md +18 -1
- package/dist/components/SplitPane.d.ts +21 -0
- package/dist/components/SplitPane.jsx +113 -0
- package/dist/components/icons.d.ts +12 -0
- package/dist/components/icons.jsx +45 -0
- package/dist/lib/autodiff/index.d.ts +28 -0
- package/dist/lib/autodiff/index.js +39 -0
- package/dist/lib/customModules.d.ts +19 -0
- package/dist/lib/customModules.js +52 -0
- package/dist/lib/markdown/compile.d.ts +27 -0
- package/dist/lib/markdown/compile.js +26 -0
- package/dist/lib/markdown/components/CanvasPage.d.ts +9 -0
- package/dist/lib/markdown/components/CanvasPage.jsx +16 -0
- package/dist/lib/markdown/components/MarkdownBody.d.ts +11 -0
- package/dist/lib/markdown/components/MarkdownBody.jsx +37 -0
- package/dist/lib/markdown/components/PiePage.d.ts +14 -0
- package/dist/lib/markdown/components/PiePage.jsx +42 -0
- package/dist/lib/markdown/components/PythonEditor.d.ts +10 -0
- package/dist/lib/markdown/components/PythonEditor.jsx +209 -0
- package/dist/lib/markdown/features/build.d.ts +60 -0
- package/dist/lib/markdown/features/build.js +36 -0
- package/dist/lib/markdown/features/glossary/View.d.ts +9 -0
- package/dist/lib/markdown/features/glossary/View.jsx +62 -0
- package/dist/lib/markdown/features/glossary/build.d.ts +6 -0
- package/dist/lib/markdown/features/glossary/build.js +81 -0
- package/dist/lib/markdown/features/sidebar/View.d.ts +11 -0
- package/dist/lib/markdown/features/sidebar/View.jsx +45 -0
- package/dist/lib/markdown/features/sidebar/build.d.ts +3 -0
- package/dist/lib/markdown/features/sidebar/build.js +7 -0
- package/dist/lib/markdown/features/tags/build.d.ts +7 -0
- package/dist/lib/markdown/features/tags/build.js +22 -0
- package/dist/lib/markdown/features/tags/url.d.ts +6 -0
- package/dist/lib/markdown/features/tags/url.js +10 -0
- package/dist/lib/markdown/features/toc/build.d.ts +7 -0
- package/dist/lib/markdown/features/toc/build.js +62 -0
- package/dist/lib/markdown/features/views.d.ts +4 -0
- package/dist/lib/markdown/features/views.jsx +11 -0
- package/dist/lib/markdown/frontmatter.d.ts +44 -0
- package/dist/lib/markdown/frontmatter.js +109 -0
- package/dist/lib/markdown/render.d.ts +6 -0
- package/dist/lib/markdown/render.js +95 -0
- package/dist/lib/nodes/animation.d.ts +14 -0
- package/dist/lib/nodes/animation.js +34 -0
- package/dist/lib/nodes/components/CenteredGrid.d.ts +20 -0
- package/dist/lib/nodes/components/CenteredGrid.jsx +44 -0
- package/dist/lib/nodes/components/NodeCanvas.d.ts +7 -0
- package/dist/lib/nodes/components/NodeCanvas.jsx +50 -0
- package/dist/lib/nodes/execute.d.ts +27 -0
- package/dist/lib/nodes/execute.js +159 -0
- package/dist/lib/nodes/functions/Node2NodeFunc.d.ts +70 -0
- package/dist/lib/nodes/functions/Node2NodeFunc.js +223 -0
- package/dist/lib/nodes/functions/array.d.ts +12 -0
- package/dist/lib/nodes/functions/array.js +31 -0
- package/dist/lib/nodes/functions/autodiff.d.ts +12 -0
- package/dist/lib/nodes/functions/autodiff.js +60 -0
- package/dist/lib/nodes/functions/basic.d.ts +111 -0
- package/dist/lib/nodes/functions/basic.js +275 -0
- package/dist/lib/nodes/functions/registry.d.ts +9 -0
- package/dist/lib/nodes/functions/registry.js +45 -0
- package/dist/lib/nodes/functions/sideEffects.d.ts +78 -0
- package/dist/lib/nodes/functions/sideEffects.js +201 -0
- package/dist/lib/nodes/hooks/useCanvasGestures.d.ts +9 -0
- package/dist/lib/nodes/hooks/useCanvasGestures.js +120 -0
- package/dist/lib/nodes/hooks/useCanvasResize.d.ts +2 -0
- package/dist/lib/nodes/hooks/useCanvasResize.js +33 -0
- package/dist/lib/nodes/jax.d.ts +46 -0
- package/dist/lib/nodes/jax.js +120 -0
- package/dist/lib/nodes/render/renderLogic.d.ts +13 -0
- package/dist/lib/nodes/render/renderLogic.jsx +234 -0
- package/dist/lib/nodes/state/NameGenerator.d.ts +10 -0
- package/dist/lib/nodes/state/NameGenerator.js +29 -0
- package/dist/lib/nodes/state/Node.d.ts +134 -0
- package/dist/lib/nodes/state/Node.js +57 -0
- package/dist/lib/nodes/state/NodeStore.d.ts +49 -0
- package/dist/lib/nodes/state/NodeStore.js +138 -0
- package/dist/lib/nodes/tensors/broadcast.d.ts +18 -0
- package/dist/lib/nodes/tensors/broadcast.js +92 -0
- package/dist/lib/nodes/tensors/tensor.d.ts +13 -0
- package/dist/lib/nodes/tensors/tensor.js +34 -0
- package/dist/lib/python/lift.d.ts +51 -0
- package/dist/lib/python/lift.js +155 -0
- package/dist/lib/python/py/pie.py +303 -0
- package/dist/lib/python/py/pie_mpl.py +15 -0
- package/dist/lib/python/py/show_figures.py +5 -0
- package/dist/lib/python/runPage.d.ts +21 -0
- package/dist/lib/python/runPage.js +60 -0
- package/dist/lib/python/runner.d.ts +68 -0
- package/dist/lib/python/runner.js +243 -0
- package/dist/lib/python/worker.d.ts +1 -0
- package/dist/lib/python/worker.js +111 -0
- package/dist/lib/ui/UiLayer.d.ts +2 -0
- package/dist/lib/ui/UiLayer.jsx +15 -0
- package/dist/lib/ui/UiView.d.ts +23 -0
- package/dist/lib/ui/UiView.jsx +56 -0
- package/dist/lib/ui/components/checkboxes.d.ts +10 -0
- package/dist/lib/ui/components/checkboxes.jsx +30 -0
- package/dist/lib/ui/components/latex.d.ts +8 -0
- package/dist/lib/ui/components/latex.jsx +41 -0
- package/dist/lib/ui/components/radio.d.ts +6 -0
- package/dist/lib/ui/components/radio.jsx +19 -0
- package/dist/lib/ui/components/slider.d.ts +8 -0
- package/dist/lib/ui/components/slider.jsx +7 -0
- package/dist/lib/ui/components/textbox.d.ts +5 -0
- package/dist/lib/ui/components/textbox.jsx +21 -0
- package/dist/lib/ui/dragUi.d.ts +10 -0
- package/dist/lib/ui/dragUi.js +129 -0
- package/dist/lib/ui/nodeHandle.d.ts +15 -0
- package/dist/lib/ui/nodeHandle.js +37 -0
- package/dist/lib/ui/registry.d.ts +10 -0
- package/dist/lib/ui/registry.js +42 -0
- package/dist/runtime.css +696 -0
- package/dist/runtime.d.ts +10 -0
- package/dist/runtime.jsx +7 -0
- package/init/index.js +2 -2
- package/init/site-agents.md +2 -1
- package/package.json +15 -6
- package/runtime-agents.md +168 -0
- package/src/app.css +7 -665
- package/src/lib/customModules.ts +67 -0
- package/src/lib/markdown/compile.ts +49 -0
- package/src/lib/markdown/components/PiePage.tsx +49 -0
- package/src/lib/markdown/features/build.ts +15 -6
- package/src/lib/markdown/features/sidebar/View.tsx +4 -2
- package/src/lib/markdown/features/tags/build.ts +6 -3
- package/src/lib/markdown/pages.ts +1 -9
- package/src/lib/markdown/vitePlugin.ts +4 -8
- package/src/lib/nodes/functions/registry.ts +30 -21
- package/src/lib/nodes/functions/sideEffects.ts +14 -8
- package/src/lib/nodes/functions/vitePlugin.ts +8 -10
- package/src/lib/nodes/state/Node.ts +27 -19
- package/src/lib/python/runPage.ts +78 -0
- package/src/lib/python/runner.ts +189 -164
- package/src/lib/siteModules.ts +13 -0
- package/src/lib/ui/dragUi.ts +4 -2
- package/src/lib/ui/registry.ts +35 -14
- package/src/page.css +664 -0
- package/src/routes/[...slug].tsx +1 -0
- package/src/runtime.css +29 -0
- package/src/runtime.tsx +16 -0
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { setCustomFunctions } from './nodes/functions/registry';
|
|
2
|
+
import { setCustomNodes } from './nodes/state/Node';
|
|
3
|
+
import { setCustomUis } from './ui/registry';
|
|
4
|
+
|
|
5
|
+
/** A file of a site's or a host's own code. `exports`: what `import * as m from '...'` gives. `file` is for error messages. */
|
|
6
|
+
export interface CustomModule {
|
|
7
|
+
file: string;
|
|
8
|
+
exports: Record<string, unknown>;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
/** Own code, by kind: `Node2NodeFunc`s and `SideEffectFunc`s, `NodeDefinition`s, and UIs (PascalCase components). */
|
|
12
|
+
export interface CustomModules {
|
|
13
|
+
functions?: CustomModule[];
|
|
14
|
+
nodes?: CustomModule[];
|
|
15
|
+
ui?: CustomModule[];
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/** Replaces the custom functions, node types and UIs. Built-ins always stay. Throws on a name used twice, leaving none. */
|
|
19
|
+
export function setCustomModules(modules: CustomModules) {
|
|
20
|
+
try {
|
|
21
|
+
setCustomFunctions(modules.functions ?? []);
|
|
22
|
+
setCustomNodes(modules.nodes ?? []);
|
|
23
|
+
setCustomUis(modules.ui ?? []);
|
|
24
|
+
} catch (e) {
|
|
25
|
+
setCustomFunctions([]);
|
|
26
|
+
setCustomNodes([]);
|
|
27
|
+
setCustomUis([]);
|
|
28
|
+
throw e;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const KINDS = ['functions', 'nodes', 'ui'] as const;
|
|
33
|
+
|
|
34
|
+
/** Same files with the same exports. A host may build a new `modules` object each render, so the objects can differ. */
|
|
35
|
+
const sameModules = (a: CustomModules, b: CustomModules) =>
|
|
36
|
+
KINDS.every((kind) => {
|
|
37
|
+
const x = a[kind] ?? [];
|
|
38
|
+
const y = b[kind] ?? [];
|
|
39
|
+
return x.length === y.length && x.every((m, i) => m.file === y[i].file && m.exports === y[i].exports);
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
// The modules set by `holdCustomModules`, and how many holders use them.
|
|
43
|
+
let held: { modules: CustomModules; count: number } | undefined;
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Sets `modules` until the returned function is called. Other holders can use the same modules meanwhile;
|
|
47
|
+
* different ones throw `conflict`. The last holder to let go clears them.
|
|
48
|
+
* Throws on a name used twice, as `setCustomModules` does.
|
|
49
|
+
*/
|
|
50
|
+
export function holdCustomModules(modules: CustomModules, conflict: string): () => void {
|
|
51
|
+
if (held) {
|
|
52
|
+
if (!sameModules(held.modules, modules)) throw new Error(conflict);
|
|
53
|
+
held.count++;
|
|
54
|
+
} else {
|
|
55
|
+
setCustomModules(modules);
|
|
56
|
+
held = { modules, count: 1 };
|
|
57
|
+
}
|
|
58
|
+
let released = false;
|
|
59
|
+
return () => {
|
|
60
|
+
if (released) return;
|
|
61
|
+
released = true;
|
|
62
|
+
if (--held!.count === 0) {
|
|
63
|
+
held = undefined;
|
|
64
|
+
setCustomModules({});
|
|
65
|
+
}
|
|
66
|
+
};
|
|
67
|
+
}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { buildFeatures, type FeatureData, type FeatureValues } from './features/build';
|
|
2
|
+
import { parseFrontmatter, type Frontmatter } from './frontmatter';
|
|
3
|
+
import { renderMarkdown } from './render';
|
|
4
|
+
|
|
5
|
+
/** One page as HTML, with its frontmatter and its features' data. */
|
|
6
|
+
export interface Page {
|
|
7
|
+
frontmatter: Frontmatter;
|
|
8
|
+
html: string;
|
|
9
|
+
features: FeatureData;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Compiles the markdown of the page `file`. Throws, naming the file, on bad frontmatter or a feature's bad value.
|
|
14
|
+
* `site`: each feature's value for a page that leaves its key out.
|
|
15
|
+
* `readFile(from, path)`: the markdown, without its frontmatter, of the file at `path`, set in `from`.
|
|
16
|
+
*/
|
|
17
|
+
export function compilePage(
|
|
18
|
+
file: string,
|
|
19
|
+
markdown: string,
|
|
20
|
+
site: Partial<FeatureValues>,
|
|
21
|
+
readFile: (from: string, path: string) => string,
|
|
22
|
+
tagLinks: boolean,
|
|
23
|
+
): Page {
|
|
24
|
+
const { frontmatter, raw, body } = parseFrontmatter(file, markdown);
|
|
25
|
+
const { data, extensions } = buildFeatures(file, raw, site, readFile, tagLinks);
|
|
26
|
+
return { frontmatter, html: renderMarkdown(body, extensions), features: data };
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export interface CompileOptions {
|
|
30
|
+
/** The page's name in error messages. Default: `page.md`. */
|
|
31
|
+
file?: string;
|
|
32
|
+
/** What a site's `site.yml` gives: each feature's value for a page that leaves its key out. */
|
|
33
|
+
site?: Partial<FeatureValues>;
|
|
34
|
+
/** The text of another `.md` file, e.g. `_topics.md` for `sidebar: _topics.md`. `path` is as the page wrote it. */
|
|
35
|
+
readFile?(path: string): string;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Compiles pie markdown into a `Page` for `PiePage`, as a site build would, in the browser or on a server.
|
|
40
|
+
* Throws with the messages a site build gives. Tags are plain text: there are no tag pages to link to.
|
|
41
|
+
*/
|
|
42
|
+
export function compilePie(markdown: string, options: CompileOptions = {}): Page {
|
|
43
|
+
const { file = 'page.md', site = {}, readFile } = options;
|
|
44
|
+
const read = (_from: string, path: string) => {
|
|
45
|
+
if (!readFile) throw new Error(`file ${path} does not exist`);
|
|
46
|
+
return parseFrontmatter(path, readFile(path)).body;
|
|
47
|
+
};
|
|
48
|
+
return compilePage(file, markdown, site, read, false);
|
|
49
|
+
}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { createMemo, onCleanup, Show } from 'solid-js';
|
|
2
|
+
import { isServer } from 'solid-js/web';
|
|
3
|
+
import 'katex/dist/katex.min.css';
|
|
4
|
+
import { holdCustomModules, type CustomModules } from '~/lib/customModules';
|
|
5
|
+
import type { Page } from '../compile';
|
|
6
|
+
import { wrapPage } from '../features/views';
|
|
7
|
+
import { CanvasPage } from './CanvasPage';
|
|
8
|
+
|
|
9
|
+
// Custom modules are global, so only one PiePage can use them at a time.
|
|
10
|
+
let mounted = false;
|
|
11
|
+
|
|
12
|
+
/** Sets `modules` until the calling component is cleaned up. */
|
|
13
|
+
function useModules(modules: CustomModules) {
|
|
14
|
+
if (mounted) throw new Error('Only one PiePage can be mounted at a time');
|
|
15
|
+
// A `runPage` running at the same time may hold the same modules.
|
|
16
|
+
const release = holdCustomModules(modules, "PiePage: modules differ from a running runPage's");
|
|
17
|
+
mounted = true;
|
|
18
|
+
onCleanup(() => {
|
|
19
|
+
release();
|
|
20
|
+
mounted = false;
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* A page from `compilePie`, shown as a pie site shows it, in a `.pie-page` box that fills its parent.
|
|
26
|
+
* `modules`: the host's own functions, node types and UIs. `css`: author CSS, limited to the page.
|
|
27
|
+
* A new `page`, `modules` or `css` starts a fresh canvas and fresh Python variables.
|
|
28
|
+
* Throws on a name used twice in `modules`, when another PiePage is mounted, or when a running `runPage` has other `modules`.
|
|
29
|
+
*/
|
|
30
|
+
export function PiePage(props: { page: Page; modules?: CustomModules; css?: string }) {
|
|
31
|
+
const current = createMemo(() => ({ page: props.page, modules: props.modules, css: props.css }));
|
|
32
|
+
|
|
33
|
+
return (
|
|
34
|
+
<div class="pie-page">
|
|
35
|
+
<Show when={current()} keyed>
|
|
36
|
+
{({ page, modules, css }) => {
|
|
37
|
+
// Only in the browser: the server draws no nodes and runs no Python, and every request would share its modules.
|
|
38
|
+
if (!isServer) useModules(modules ?? {});
|
|
39
|
+
return (
|
|
40
|
+
<>
|
|
41
|
+
{css && <style>{`.pie-page {\n${css}\n}`}</style>}
|
|
42
|
+
{wrapPage(page.features, <CanvasPage side={page.frontmatter.canvas} html={page.html} />)}
|
|
43
|
+
</>
|
|
44
|
+
);
|
|
45
|
+
}}
|
|
46
|
+
</Show>
|
|
47
|
+
</div>
|
|
48
|
+
);
|
|
49
|
+
}
|
|
@@ -12,7 +12,7 @@
|
|
|
12
12
|
* A feature that only changes the page's HTML, like `toc`, has no view.
|
|
13
13
|
*/
|
|
14
14
|
import type { MarkedExtension } from 'marked';
|
|
15
|
-
import type { Field } from '../frontmatter';
|
|
15
|
+
import type { Field, Values } from '../frontmatter';
|
|
16
16
|
import { glossary } from './glossary/build';
|
|
17
17
|
import { sidebar } from './sidebar/build';
|
|
18
18
|
import { tags } from './tags/build';
|
|
@@ -26,6 +26,8 @@ export interface BuildContext {
|
|
|
26
26
|
from: string;
|
|
27
27
|
/** The markdown of the `.md` file at `path`, relative to the folder of `from`, without its frontmatter. Throws if it's missing or outside the project. */
|
|
28
28
|
readFile(path: string): string;
|
|
29
|
+
/** False inside another app (`compilePie`), which has no tag pages to link to. */
|
|
30
|
+
tagLinks: boolean;
|
|
29
31
|
}
|
|
30
32
|
|
|
31
33
|
/** A feature's build half. `T`: its frontmatter value. `D`: the data its view gets. */
|
|
@@ -53,20 +55,27 @@ export const FEATURE_FIELDS = Object.fromEntries(
|
|
|
53
55
|
Object.entries(FEATURES).map(([key, feature]) => [key, feature.field]),
|
|
54
56
|
) as { [K in keyof typeof FEATURES]: (typeof FEATURES)[K]['field'] };
|
|
55
57
|
|
|
56
|
-
/**
|
|
58
|
+
/** Each feature's value, as `site.yml` gives it. */
|
|
59
|
+
export type FeatureValues = Values<typeof FEATURE_FIELDS>;
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Runs every feature's `build` for the page `file`. Throws, naming the file and key, on a bad value.
|
|
63
|
+
* A key that neither the page nor `site` sets takes its field's default.
|
|
64
|
+
*/
|
|
57
65
|
export function buildFeatures(
|
|
58
66
|
file: string,
|
|
59
67
|
raw: Record<string, string>,
|
|
60
|
-
site:
|
|
68
|
+
site: Partial<FeatureValues>,
|
|
61
69
|
readFile: (from: string, path: string) => string,
|
|
70
|
+
tagLinks: boolean,
|
|
62
71
|
): { data: FeatureData; extensions: MarkedExtension[] } {
|
|
63
72
|
const data: Record<string, unknown> = {};
|
|
64
73
|
const extensions: MarkedExtension[] = [];
|
|
65
|
-
for (const [key, feature] of Object.entries(FEATURES) as [
|
|
74
|
+
for (const [key, feature] of Object.entries(FEATURES) as [keyof FeatureValues, FeatureBuild<unknown, unknown>][]) {
|
|
66
75
|
const from = Object.hasOwn(raw, key) ? file : 'site.yml';
|
|
67
76
|
try {
|
|
68
|
-
const value = from === file ? feature.field.parse(raw[key]) : site[key];
|
|
69
|
-
const ctx: BuildContext = { file, from, readFile: path => readFile(from, path) };
|
|
77
|
+
const value = from === file ? feature.field.parse(raw[key]) : site[key] ?? feature.field.default;
|
|
78
|
+
const ctx: BuildContext = { file, from, readFile: path => readFile(from, path), tagLinks };
|
|
70
79
|
const built = feature.build(value, ctx);
|
|
71
80
|
if (built === undefined) continue;
|
|
72
81
|
data[key] = built;
|
|
@@ -14,9 +14,11 @@ const DRAWER_BELOW = '768px';
|
|
|
14
14
|
export function SidebarView(props: { data: string; children: JSX.Element }) {
|
|
15
15
|
const [open, setOpen] = createSignal(false);
|
|
16
16
|
const [drawer, setDrawer] = createSignal(false);
|
|
17
|
+
let page!: HTMLDivElement;
|
|
17
18
|
|
|
18
19
|
onMount(() => {
|
|
19
|
-
|
|
20
|
+
// Read here, not on `:root`, so a host can set it on its `.pie-page`.
|
|
21
|
+
const below = getComputedStyle(page).getPropertyValue('--sidebar-drawer-below').trim() || DRAWER_BELOW;
|
|
20
22
|
const query = matchMedia(`(width < ${below})`);
|
|
21
23
|
const update = () => setDrawer(query.matches);
|
|
22
24
|
update();
|
|
@@ -31,7 +33,7 @@ export function SidebarView(props: { data: string; children: JSX.Element }) {
|
|
|
31
33
|
const label = () => (open() ? 'Hide sidebar' : 'Show sidebar');
|
|
32
34
|
|
|
33
35
|
return (
|
|
34
|
-
<div class="sidebar-page" classList={{ 'sidebar-page--open': open(), 'sidebar-page--drawer': drawer() }}>
|
|
36
|
+
<div ref={page} class="sidebar-page" classList={{ 'sidebar-page--open': open(), 'sidebar-page--drawer': drawer() }}>
|
|
35
37
|
<aside id="sidebar" class="sidebar" onClick={closeOnLink}>
|
|
36
38
|
<MarkdownBody class="sidebar-text" html={props.data} />
|
|
37
39
|
</aside>
|
|
@@ -7,15 +7,18 @@ import { tagUrl } from './url';
|
|
|
7
7
|
/**
|
|
8
8
|
* `tags: #math #linear-algebra`: the page's tags, as links at the end of its text. Each link opens the page
|
|
9
9
|
* listing the pages with that tag, in `src/routes/_tags/`. Only a page's own frontmatter counts, not `site.yml`.
|
|
10
|
+
* Without `tagLinks`, the tags are plain text.
|
|
10
11
|
*/
|
|
11
12
|
export const tags: FeatureBuild<string[], true> = {
|
|
12
13
|
field: hashtags(),
|
|
13
14
|
build: (tags, ctx) => (ctx.from === ctx.file && tags.length > 0 ? true : undefined),
|
|
14
|
-
markdown(tags) {
|
|
15
|
-
const
|
|
15
|
+
markdown(tags, ctx) {
|
|
16
|
+
const items = tags.map(tag => (ctx.tagLinks
|
|
17
|
+
? `<a href="${escapeHtml(tagUrl(tag))}">#${escapeHtml(tag)}</a>`
|
|
18
|
+
: `<span>#${escapeHtml(tag)}</span>`)).join('');
|
|
16
19
|
return {
|
|
17
20
|
hooks: {
|
|
18
|
-
postprocess: html => `${html}<nav class="tags">${
|
|
21
|
+
postprocess: html => `${html}<nav class="tags">${items}</nav>\n`,
|
|
19
22
|
},
|
|
20
23
|
} satisfies MarkedExtension;
|
|
21
24
|
},
|
|
@@ -1,12 +1,4 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
import type { Frontmatter } from './frontmatter';
|
|
3
|
-
|
|
4
|
-
/** One `pages/**\/*.md`, as `vitePlugin.ts` turns it into a module at build time. */
|
|
5
|
-
export interface Page {
|
|
6
|
-
frontmatter: Frontmatter;
|
|
7
|
-
html: string;
|
|
8
|
-
features: FeatureData;
|
|
9
|
-
}
|
|
1
|
+
import type { Page } from './compile';
|
|
10
2
|
|
|
11
3
|
// Not eager: each page is its own file, downloaded only when that page is opened.
|
|
12
4
|
// A file whose name starts with `_` is not a page; it can still be used by a feature, e.g. as a sidebar.
|
|
@@ -1,11 +1,10 @@
|
|
|
1
1
|
import fs from 'node:fs';
|
|
2
2
|
import path from 'node:path';
|
|
3
3
|
import type { Plugin } from 'vite';
|
|
4
|
-
import {
|
|
4
|
+
import { compilePage } from './compile';
|
|
5
|
+
import { FEATURE_FIELDS } from './features/build';
|
|
5
6
|
import type { PageTags } from './features/tags/pages';
|
|
6
7
|
import { parseFrontmatter } from './frontmatter';
|
|
7
|
-
import type { Page } from './pages';
|
|
8
|
-
import { renderMarkdown } from './render';
|
|
9
8
|
import { APP_DIR } from '../appDir';
|
|
10
9
|
import { parseSite } from '../site/vitePlugin';
|
|
11
10
|
|
|
@@ -28,10 +27,8 @@ export function markdownPages(): Plugin {
|
|
|
28
27
|
const [filePath, query] = id.split('?');
|
|
29
28
|
const file = path.relative(root, filePath);
|
|
30
29
|
if (!file.startsWith(`pages${path.sep}`) || !file.endsWith('.md')) return;
|
|
31
|
-
const { frontmatter, raw, body } = parseFrontmatter(file, markdown);
|
|
32
|
-
|
|
33
30
|
if (new URLSearchParams(query).has('tags')) {
|
|
34
|
-
const { title, description } =
|
|
31
|
+
const { frontmatter: { title, description }, raw } = parseFrontmatter(file, markdown);
|
|
35
32
|
const page: PageTags = { title, description, tags: FEATURE_FIELDS.tags.parse(raw.tags ?? '') };
|
|
36
33
|
return { code: `export default ${JSON.stringify(page)};`, map: null };
|
|
37
34
|
}
|
|
@@ -53,8 +50,7 @@ export function markdownPages(): Plugin {
|
|
|
53
50
|
return parseFrontmatter(shown, fs.readFileSync(target, 'utf8')).body;
|
|
54
51
|
};
|
|
55
52
|
|
|
56
|
-
const
|
|
57
|
-
const page: Page = { frontmatter, html: renderMarkdown(body, extensions), features: data };
|
|
53
|
+
const page = compilePage(file, markdown, site, readFile, true);
|
|
58
54
|
return { code: `export default ${JSON.stringify(page)};`, map: null };
|
|
59
55
|
},
|
|
60
56
|
};
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { CustomModule } from '~/lib/customModules';
|
|
1
2
|
import { Node2NodeFunc, SideEffectFunc } from './Node2NodeFunc';
|
|
2
3
|
import * as basicNodes from './basic';
|
|
3
4
|
import * as autodiffNodes from './autodiff';
|
|
@@ -10,33 +11,41 @@ const isNode2NodeFunc = (fn: unknown): fn is Node2NodeFunc =>
|
|
|
10
11
|
const isSideEffectFunc = (fn: unknown): fn is SideEffectFunc =>
|
|
11
12
|
typeof fn === 'object' && fn !== null && typeof (fn as SideEffectFunc).run === 'function';
|
|
12
13
|
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
['src/lib/nodes/functions/basic.ts', basicNodes],
|
|
19
|
-
['src/lib/nodes/functions/autodiff.ts', autodiffNodes],
|
|
20
|
-
['src/lib/nodes/functions/array.ts', arrayNodes],
|
|
21
|
-
['src/lib/nodes/functions/sideEffects.ts', sideEffects],
|
|
22
|
-
...Object.entries(customModules).map(([path, mod]): [string, Record<string, unknown>] => [path.slice(1), mod]),
|
|
14
|
+
const BUILTINS: CustomModule[] = [
|
|
15
|
+
{ file: 'src/lib/nodes/functions/basic.ts', exports: basicNodes },
|
|
16
|
+
{ file: 'src/lib/nodes/functions/autodiff.ts', exports: autodiffNodes },
|
|
17
|
+
{ file: 'src/lib/nodes/functions/array.ts', exports: arrayNodes },
|
|
18
|
+
{ file: 'src/lib/nodes/functions/sideEffects.ts', exports: sideEffects },
|
|
23
19
|
];
|
|
24
20
|
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
21
|
+
let functionRegistry = new Map<string, Node2NodeFunc>();
|
|
22
|
+
let sideEffectRegistry = new Map<string, SideEffectFunc>();
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Replaces the custom functions with the `Node2NodeFunc`s and `SideEffectFunc`s that `modules` export, e.g. a site's `functions/**\/*.ts`.
|
|
26
|
+
* Throws on a keyword used twice, leaving the functions as they were.
|
|
27
|
+
*/
|
|
28
|
+
export function setCustomFunctions(modules: CustomModule[]) {
|
|
29
|
+
const functions = new Map<string, Node2NodeFunc>();
|
|
30
|
+
const effects = new Map<string, SideEffectFunc>();
|
|
31
|
+
const sourceFile = new Map<string, string>();
|
|
32
|
+
for (const { file, exports } of [...BUILTINS, ...modules]) {
|
|
33
|
+
for (const fn of Object.values(exports).filter(fn => isNode2NodeFunc(fn) || isSideEffectFunc(fn))) {
|
|
34
|
+
const other = sourceFile.get(fn.keyword);
|
|
35
|
+
if (other !== undefined) {
|
|
36
|
+
throw new Error(`Duplicate function keyword '${fn.keyword}': ${file} and ${other}`);
|
|
37
|
+
}
|
|
38
|
+
if (isSideEffectFunc(fn)) effects.set(fn.keyword, fn);
|
|
39
|
+
else functions.set(fn.keyword, fn);
|
|
40
|
+
sourceFile.set(fn.keyword, file);
|
|
33
41
|
}
|
|
34
|
-
if (isSideEffectFunc(fn)) sideEffectRegistry.set(fn.keyword, fn);
|
|
35
|
-
else functionRegistry.set(fn.keyword, fn);
|
|
36
|
-
sourceFile.set(fn.keyword, file);
|
|
37
42
|
}
|
|
43
|
+
functionRegistry = functions;
|
|
44
|
+
sideEffectRegistry = effects;
|
|
38
45
|
}
|
|
39
46
|
|
|
47
|
+
setCustomFunctions([]);
|
|
48
|
+
|
|
40
49
|
export function getFunction(keyword: string): Node2NodeFunc | undefined {
|
|
41
50
|
return functionRegistry.get(keyword);
|
|
42
51
|
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { createMemo, createRoot, createSignal, onCleanup } from 'solid-js';
|
|
2
|
-
import { SideEffectFunc } from './Node2NodeFunc';
|
|
2
|
+
import { ParameterDefinition, SideEffectFunc } from './Node2NodeFunc';
|
|
3
3
|
import { AnyNode, PointNode, ScalarNode, TextNode, checkColor, getNodeTypes } from '../state/Node';
|
|
4
4
|
import { NodeStore } from '../state/NodeStore';
|
|
5
5
|
import { Dtype, add, num, ref, scalar } from '../jax';
|
|
@@ -35,8 +35,14 @@ export function replaceNode<N extends AnyNode>(store: NodeStore, node: N, change
|
|
|
35
35
|
});
|
|
36
36
|
}
|
|
37
37
|
|
|
38
|
-
//
|
|
39
|
-
|
|
38
|
+
// A parameter that takes any node, but not a whole Array: an Array is looped over, so each item is hidden or shown.
|
|
39
|
+
// A getter, so it includes custom node types set after this file loads.
|
|
40
|
+
const anyItem = (name: string): ParameterDefinition => ({
|
|
41
|
+
name,
|
|
42
|
+
get type() {
|
|
43
|
+
return getNodeTypes().filter(type => type !== 'Array');
|
|
44
|
+
},
|
|
45
|
+
});
|
|
40
46
|
|
|
41
47
|
/**
|
|
42
48
|
* Hide a node: it is not drawn on the canvas.
|
|
@@ -44,7 +50,7 @@ const anyItem = getNodeTypes().filter(type => type !== 'Array');
|
|
|
44
50
|
*/
|
|
45
51
|
export const Hide: SideEffectFunc = {
|
|
46
52
|
keyword: 'hide',
|
|
47
|
-
parameters: [
|
|
53
|
+
parameters: [anyItem('node')],
|
|
48
54
|
run(inputs, store) {
|
|
49
55
|
store.updateNode(inputs[0].id, { hidden: true });
|
|
50
56
|
},
|
|
@@ -56,7 +62,7 @@ export const Hide: SideEffectFunc = {
|
|
|
56
62
|
*/
|
|
57
63
|
export const Show: SideEffectFunc = {
|
|
58
64
|
keyword: 'show',
|
|
59
|
-
parameters: [
|
|
65
|
+
parameters: [anyItem('node')],
|
|
60
66
|
run(inputs, store) {
|
|
61
67
|
store.updateNode(inputs[0].id, { hidden: false });
|
|
62
68
|
},
|
|
@@ -68,7 +74,7 @@ export const Show: SideEffectFunc = {
|
|
|
68
74
|
*/
|
|
69
75
|
export const Remove: SideEffectFunc = {
|
|
70
76
|
keyword: 'remove',
|
|
71
|
-
parameters: [
|
|
77
|
+
parameters: [anyItem('node')],
|
|
72
78
|
run(inputs, store) {
|
|
73
79
|
store.removeNode(inputs[0].id);
|
|
74
80
|
},
|
|
@@ -81,7 +87,7 @@ export const Remove: SideEffectFunc = {
|
|
|
81
87
|
*/
|
|
82
88
|
export const SetFill: SideEffectFunc = {
|
|
83
89
|
keyword: 'set_fill',
|
|
84
|
-
parameters: [
|
|
90
|
+
parameters: [anyItem('node'), { name: 'color', type: 'Text' }],
|
|
85
91
|
run(inputs, store) {
|
|
86
92
|
const fill = (inputs[1] as TextNode).value;
|
|
87
93
|
checkColor('set_fill', 'fill', fill);
|
|
@@ -96,7 +102,7 @@ export const SetFill: SideEffectFunc = {
|
|
|
96
102
|
*/
|
|
97
103
|
export const SetStroke: SideEffectFunc = {
|
|
98
104
|
keyword: 'set_stroke',
|
|
99
|
-
parameters: [
|
|
105
|
+
parameters: [anyItem('node'), { name: 'color', type: 'Text' }],
|
|
100
106
|
run(inputs, store) {
|
|
101
107
|
const stroke = (inputs[1] as TextNode).value;
|
|
102
108
|
checkColor('set_stroke', 'stroke', stroke);
|
|
@@ -3,8 +3,8 @@ import { runnerImport, type Plugin } from 'vite';
|
|
|
3
3
|
import { APP_DIR } from '../../appDir';
|
|
4
4
|
|
|
5
5
|
/**
|
|
6
|
-
* Loads `
|
|
7
|
-
* (e.g. a
|
|
6
|
+
* Loads `siteModules.ts` once at build start, so a bad `functions/**\/*.ts` (e.g. a keyword used twice),
|
|
7
|
+
* `nodes/**\/*.{ts,tsx}` (e.g. a type used twice) or `ui/**\/*.{ts,tsx}` (e.g. a name used twice) fails the build.
|
|
8
8
|
* Without this, the error only stops pages from prerendering.
|
|
9
9
|
*/
|
|
10
10
|
export function checkFunctions(): Plugin {
|
|
@@ -22,16 +22,14 @@ export function checkFunctions(): Plugin {
|
|
|
22
22
|
if (checked) return;
|
|
23
23
|
checked = true;
|
|
24
24
|
try {
|
|
25
|
-
|
|
26
|
-
configFile: false
|
|
25
|
+
await runnerImport(path.join(APP_DIR, 'lib/siteModules.ts'), {
|
|
26
|
+
configFile: false,
|
|
27
27
|
root,
|
|
28
|
-
logLevel: 'silent'
|
|
28
|
+
logLevel: 'silent',
|
|
29
29
|
resolve: { alias: { '~': APP_DIR } },
|
|
30
|
-
// Render functions use JSX. They never run here, so any JSX runtime that loads will do.
|
|
31
|
-
oxc: { jsx: { runtime: 'automatic'
|
|
32
|
-
};
|
|
33
|
-
await runnerImport(path.join(APP_DIR, 'lib/nodes/functions/registry.ts'), options);
|
|
34
|
-
await runnerImport(path.join(APP_DIR, 'lib/nodes/state/Node.ts'), options);
|
|
30
|
+
// Render functions and UIs use JSX. They never run here, so any JSX runtime that loads will do.
|
|
31
|
+
oxc: { jsx: { runtime: 'automatic', importSource: 'solid-js/h' } },
|
|
32
|
+
});
|
|
35
33
|
} catch (e) {
|
|
36
34
|
this.error((e as Error).message);
|
|
37
35
|
}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { Accessor, JSXElement } from 'solid-js';
|
|
2
|
+
import type { CustomModule } from '~/lib/customModules';
|
|
2
3
|
import type { Dtype } from '../jax';
|
|
3
4
|
import {
|
|
4
5
|
renderPoint,
|
|
@@ -176,32 +177,39 @@ export type NodeType = keyof NodeTypeMap;
|
|
|
176
177
|
const isNodeDefinition = (def: unknown): def is NodeDefinition =>
|
|
177
178
|
typeof def === 'object' && def !== null && typeof (def as NodeDefinition).type === 'string';
|
|
178
179
|
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
// File path -> its definitions. Built-ins first.
|
|
183
|
-
const definitionsByFile: [string, unknown[]][] = [
|
|
184
|
-
['src/lib/nodes/state/Node.ts', [
|
|
180
|
+
const BUILTINS: CustomModule[] = [{
|
|
181
|
+
file: 'src/lib/nodes/state/Node.ts',
|
|
182
|
+
exports: {
|
|
185
183
|
TextDefinition, PointDefinition, ScalarDefinition, TriangleDefinition, LineDefinition, CircleDefinition,
|
|
186
184
|
EllipseDefinition, BezierQuadraticDefinition, BezierCubicDefinition, ArcDefinition, PointGradDefinition,
|
|
187
185
|
ArrayDefinition, DummyDefinition,
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
186
|
+
},
|
|
187
|
+
}];
|
|
188
|
+
|
|
189
|
+
let nodeDefinitions = new Map<string, NodeDefinition>();
|
|
190
|
+
|
|
191
|
+
/**
|
|
192
|
+
* Replaces the custom node types with the `NodeDefinition`s that `modules` export, e.g. a site's `nodes/**\/*.{ts,tsx}`.
|
|
193
|
+
* Throws on a type used twice, leaving the types as they were.
|
|
194
|
+
*/
|
|
195
|
+
export function setCustomNodes(modules: CustomModule[]) {
|
|
196
|
+
const definitions = new Map<string, NodeDefinition>();
|
|
197
|
+
const sourceFile = new Map<string, string>();
|
|
198
|
+
for (const { file, exports } of [...BUILTINS, ...modules]) {
|
|
199
|
+
for (const def of Object.values(exports).filter(isNodeDefinition)) {
|
|
200
|
+
const other = sourceFile.get(def.type);
|
|
201
|
+
if (other !== undefined) {
|
|
202
|
+
throw new Error(`Duplicate node type '${def.type}': ${file} and ${other}`);
|
|
203
|
+
}
|
|
204
|
+
definitions.set(def.type, def);
|
|
205
|
+
sourceFile.set(def.type, file);
|
|
199
206
|
}
|
|
200
|
-
nodeDefinitions.set(def.type, def);
|
|
201
|
-
sourceFile.set(def.type, file);
|
|
202
207
|
}
|
|
208
|
+
nodeDefinitions = definitions;
|
|
203
209
|
}
|
|
204
210
|
|
|
211
|
+
setCustomNodes([]);
|
|
212
|
+
|
|
205
213
|
export function getNodeDefinition(type: string): NodeDefinition | undefined {
|
|
206
214
|
return nodeDefinitions.get(type);
|
|
207
215
|
}
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import type { Page } from '~/lib/markdown/compile';
|
|
2
|
+
import { holdCustomModules, type CustomModules } from '~/lib/customModules';
|
|
3
|
+
import { NodeStore } from '~/lib/nodes/state/NodeStore';
|
|
4
|
+
import { checkUi } from '~/lib/ui/UiView';
|
|
5
|
+
import { PythonRunner, PythonScope } from './runner';
|
|
6
|
+
|
|
7
|
+
export interface RunPageOptions {
|
|
8
|
+
/** Stop after this many milliseconds in total, including loading Python. Default 30000. */
|
|
9
|
+
timeoutMs?: number;
|
|
10
|
+
/** The host's own functions, node types and UIs, as `<PiePage modules>` takes them. */
|
|
11
|
+
modules?: CustomModules;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/** One block that ran. */
|
|
15
|
+
export interface BlockResult {
|
|
16
|
+
/** What the block printed. */
|
|
17
|
+
output: string;
|
|
18
|
+
/** The Python error, or `Timed out after N s`. Unset when the block ran without one. */
|
|
19
|
+
error?: string;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Runs `page`'s `python-editor` blocks in order, as a reader clicking Run on each would, without showing them.
|
|
24
|
+
* Stops at the first block with an error. Runs in its own Python worker, which it terminates at the end.
|
|
25
|
+
* Rejects when a mounted PiePage has other `modules`.
|
|
26
|
+
*/
|
|
27
|
+
export async function runPage(page: Page, { timeoutMs = 30_000, modules = {} }: RunPageOptions = {}): Promise<BlockResult[]> {
|
|
28
|
+
const doc = new DOMParser().parseFromString(page.html, 'text/html');
|
|
29
|
+
// The blocks `MarkdownBody` turns into editors.
|
|
30
|
+
const codes = Array.from(doc.querySelectorAll('pre > code.language-python-editor'), (el) => el.textContent ?? '');
|
|
31
|
+
|
|
32
|
+
const release = holdCustomModules(modules, "runPage: modules differ from the open PiePage's");
|
|
33
|
+
const runner = new PythonRunner();
|
|
34
|
+
const store = new NodeStore('right', 'none');
|
|
35
|
+
const scope = new PythonScope(store, runner);
|
|
36
|
+
const results: BlockResult[] = [];
|
|
37
|
+
let timedOut = false;
|
|
38
|
+
const timer = setTimeout(() => {
|
|
39
|
+
timedOut = true;
|
|
40
|
+
runner.kill();
|
|
41
|
+
}, timeoutMs);
|
|
42
|
+
|
|
43
|
+
try {
|
|
44
|
+
for (const code of codes) {
|
|
45
|
+
if (timedOut) break;
|
|
46
|
+
const result: BlockResult = { output: '' };
|
|
47
|
+
results.push(result);
|
|
48
|
+
await new Promise<void>((resolve) => {
|
|
49
|
+
scope.run(
|
|
50
|
+
code,
|
|
51
|
+
(status, error) => {
|
|
52
|
+
if (status === 'finished') {
|
|
53
|
+
if (error) result.error = error;
|
|
54
|
+
resolve();
|
|
55
|
+
} else if (status === 'killed') {
|
|
56
|
+
result.error = `Timed out after ${timeoutMs / 1000} s`;
|
|
57
|
+
resolve();
|
|
58
|
+
}
|
|
59
|
+
},
|
|
60
|
+
(text) => {
|
|
61
|
+
result.output += text;
|
|
62
|
+
},
|
|
63
|
+
() => {},
|
|
64
|
+
// Raises in Python as on a page; the UI isn't drawn.
|
|
65
|
+
(call) => checkUi(store, call),
|
|
66
|
+
);
|
|
67
|
+
});
|
|
68
|
+
if (result.error !== undefined) break;
|
|
69
|
+
}
|
|
70
|
+
} finally {
|
|
71
|
+
clearTimeout(timer);
|
|
72
|
+
runner.kill();
|
|
73
|
+
scope.dispose();
|
|
74
|
+
for (const id in store.nodes) store.nodes[id]?.dispose?.();
|
|
75
|
+
release();
|
|
76
|
+
}
|
|
77
|
+
return results;
|
|
78
|
+
}
|