@tinyvolt/pie 0.8.2 → 0.10.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.
Files changed (139) hide show
  1. package/README.md +18 -1
  2. package/dist/components/SplitPane.d.ts +21 -0
  3. package/dist/components/SplitPane.jsx +113 -0
  4. package/dist/components/icons.d.ts +12 -0
  5. package/dist/components/icons.jsx +45 -0
  6. package/dist/lib/autodiff/index.d.ts +28 -0
  7. package/dist/lib/autodiff/index.js +39 -0
  8. package/dist/lib/customModules.d.ts +13 -0
  9. package/dist/lib/customModules.js +17 -0
  10. package/dist/lib/markdown/compile.d.ts +27 -0
  11. package/dist/lib/markdown/compile.js +26 -0
  12. package/dist/lib/markdown/components/CanvasPage.d.ts +9 -0
  13. package/dist/lib/markdown/components/CanvasPage.jsx +16 -0
  14. package/dist/lib/markdown/components/MarkdownBody.d.ts +11 -0
  15. package/dist/lib/markdown/components/MarkdownBody.jsx +37 -0
  16. package/dist/lib/markdown/components/PiePage.d.ts +14 -0
  17. package/dist/lib/markdown/components/PiePage.jsx +41 -0
  18. package/dist/lib/markdown/components/PythonEditor.d.ts +10 -0
  19. package/dist/lib/markdown/components/PythonEditor.jsx +209 -0
  20. package/dist/lib/markdown/features/build.d.ts +60 -0
  21. package/dist/lib/markdown/features/build.js +36 -0
  22. package/dist/lib/markdown/features/glossary/View.d.ts +9 -0
  23. package/dist/lib/markdown/features/glossary/View.jsx +62 -0
  24. package/dist/lib/markdown/features/glossary/build.d.ts +6 -0
  25. package/dist/lib/markdown/features/glossary/build.js +81 -0
  26. package/dist/lib/markdown/features/sidebar/View.d.ts +11 -0
  27. package/dist/lib/markdown/features/sidebar/View.jsx +45 -0
  28. package/dist/lib/markdown/features/sidebar/build.d.ts +3 -0
  29. package/dist/lib/markdown/features/sidebar/build.js +7 -0
  30. package/dist/lib/markdown/features/tags/build.d.ts +7 -0
  31. package/dist/lib/markdown/features/tags/build.js +22 -0
  32. package/dist/lib/markdown/features/tags/url.d.ts +6 -0
  33. package/dist/lib/markdown/features/tags/url.js +10 -0
  34. package/dist/lib/markdown/features/toc/build.d.ts +7 -0
  35. package/dist/lib/markdown/features/toc/build.js +62 -0
  36. package/dist/lib/markdown/features/views.d.ts +4 -0
  37. package/dist/lib/markdown/features/views.jsx +11 -0
  38. package/dist/lib/markdown/frontmatter.d.ts +44 -0
  39. package/dist/lib/markdown/frontmatter.js +109 -0
  40. package/dist/lib/markdown/render.d.ts +6 -0
  41. package/dist/lib/markdown/render.js +95 -0
  42. package/dist/lib/nodes/animation.d.ts +14 -0
  43. package/dist/lib/nodes/animation.js +34 -0
  44. package/dist/lib/nodes/components/CenteredGrid.d.ts +20 -0
  45. package/dist/lib/nodes/components/CenteredGrid.jsx +44 -0
  46. package/dist/lib/nodes/components/NodeCanvas.d.ts +7 -0
  47. package/dist/lib/nodes/components/NodeCanvas.jsx +50 -0
  48. package/dist/lib/nodes/execute.d.ts +27 -0
  49. package/dist/lib/nodes/execute.js +159 -0
  50. package/dist/lib/nodes/functions/Node2NodeFunc.d.ts +70 -0
  51. package/dist/lib/nodes/functions/Node2NodeFunc.js +223 -0
  52. package/dist/lib/nodes/functions/array.d.ts +12 -0
  53. package/dist/lib/nodes/functions/array.js +31 -0
  54. package/dist/lib/nodes/functions/autodiff.d.ts +12 -0
  55. package/dist/lib/nodes/functions/autodiff.js +60 -0
  56. package/dist/lib/nodes/functions/basic.d.ts +111 -0
  57. package/dist/lib/nodes/functions/basic.js +275 -0
  58. package/dist/lib/nodes/functions/registry.d.ts +9 -0
  59. package/dist/lib/nodes/functions/registry.js +45 -0
  60. package/dist/lib/nodes/functions/sideEffects.d.ts +78 -0
  61. package/dist/lib/nodes/functions/sideEffects.js +201 -0
  62. package/dist/lib/nodes/hooks/useCanvasGestures.d.ts +9 -0
  63. package/dist/lib/nodes/hooks/useCanvasGestures.js +120 -0
  64. package/dist/lib/nodes/hooks/useCanvasResize.d.ts +2 -0
  65. package/dist/lib/nodes/hooks/useCanvasResize.js +33 -0
  66. package/dist/lib/nodes/jax.d.ts +46 -0
  67. package/dist/lib/nodes/jax.js +120 -0
  68. package/dist/lib/nodes/render/renderLogic.d.ts +13 -0
  69. package/dist/lib/nodes/render/renderLogic.jsx +234 -0
  70. package/dist/lib/nodes/state/NameGenerator.d.ts +10 -0
  71. package/dist/lib/nodes/state/NameGenerator.js +29 -0
  72. package/dist/lib/nodes/state/Node.d.ts +134 -0
  73. package/dist/lib/nodes/state/Node.js +57 -0
  74. package/dist/lib/nodes/state/NodeStore.d.ts +49 -0
  75. package/dist/lib/nodes/state/NodeStore.js +138 -0
  76. package/dist/lib/nodes/tensors/broadcast.d.ts +18 -0
  77. package/dist/lib/nodes/tensors/broadcast.js +92 -0
  78. package/dist/lib/nodes/tensors/tensor.d.ts +13 -0
  79. package/dist/lib/nodes/tensors/tensor.js +34 -0
  80. package/dist/lib/python/lift.d.ts +51 -0
  81. package/dist/lib/python/lift.js +155 -0
  82. package/dist/lib/python/py/pie.py +303 -0
  83. package/dist/lib/python/py/pie_mpl.py +15 -0
  84. package/dist/lib/python/py/show_figures.py +5 -0
  85. package/dist/lib/python/runner.d.ts +36 -0
  86. package/dist/lib/python/runner.js +221 -0
  87. package/dist/lib/python/worker.d.ts +1 -0
  88. package/dist/lib/python/worker.js +111 -0
  89. package/dist/lib/ui/UiLayer.d.ts +2 -0
  90. package/dist/lib/ui/UiLayer.jsx +15 -0
  91. package/dist/lib/ui/UiView.d.ts +23 -0
  92. package/dist/lib/ui/UiView.jsx +56 -0
  93. package/dist/lib/ui/components/checkboxes.d.ts +10 -0
  94. package/dist/lib/ui/components/checkboxes.jsx +30 -0
  95. package/dist/lib/ui/components/latex.d.ts +8 -0
  96. package/dist/lib/ui/components/latex.jsx +41 -0
  97. package/dist/lib/ui/components/radio.d.ts +6 -0
  98. package/dist/lib/ui/components/radio.jsx +19 -0
  99. package/dist/lib/ui/components/slider.d.ts +8 -0
  100. package/dist/lib/ui/components/slider.jsx +7 -0
  101. package/dist/lib/ui/components/textbox.d.ts +5 -0
  102. package/dist/lib/ui/components/textbox.jsx +21 -0
  103. package/dist/lib/ui/dragUi.d.ts +10 -0
  104. package/dist/lib/ui/dragUi.js +129 -0
  105. package/dist/lib/ui/nodeHandle.d.ts +15 -0
  106. package/dist/lib/ui/nodeHandle.js +37 -0
  107. package/dist/lib/ui/registry.d.ts +10 -0
  108. package/dist/lib/ui/registry.js +42 -0
  109. package/dist/runtime.css +696 -0
  110. package/dist/runtime.d.ts +9 -0
  111. package/dist/runtime.jsx +6 -0
  112. package/init/index.js +2 -2
  113. package/init/site-agents.md +7 -3
  114. package/package.json +15 -6
  115. package/pages/about.md +4 -0
  116. package/runtime-agents.md +146 -0
  117. package/src/app.css +7 -665
  118. package/src/components/SplitPane.tsx +11 -9
  119. package/src/lib/customModules.ts +30 -0
  120. package/src/lib/markdown/compile.ts +49 -0
  121. package/src/lib/markdown/components/CanvasPage.tsx +9 -4
  122. package/src/lib/markdown/components/PiePage.tsx +48 -0
  123. package/src/lib/markdown/features/build.ts +15 -6
  124. package/src/lib/markdown/features/sidebar/View.tsx +4 -2
  125. package/src/lib/markdown/features/tags/build.ts +6 -3
  126. package/src/lib/markdown/pages.ts +1 -9
  127. package/src/lib/markdown/vitePlugin.ts +4 -8
  128. package/src/lib/nodes/functions/registry.ts +30 -21
  129. package/src/lib/nodes/functions/sideEffects.ts +48 -8
  130. package/src/lib/nodes/functions/vitePlugin.ts +8 -10
  131. package/src/lib/nodes/state/Node.ts +27 -19
  132. package/src/lib/nodes/state/NodeStore.ts +12 -1
  133. package/src/lib/siteModules.ts +13 -0
  134. package/src/lib/ui/dragUi.ts +4 -2
  135. package/src/lib/ui/registry.ts +35 -14
  136. package/src/page.css +664 -0
  137. package/src/routes/[...slug].tsx +2 -3
  138. package/src/runtime.css +29 -0
  139. package/src/runtime.tsx +15 -0
@@ -3,7 +3,8 @@ import { Chevron, type Direction } from './icons';
3
3
 
4
4
  /** The edge the first pane sits against. */
5
5
  export type SplitSide = 'left' | 'right' | 'top' | 'bottom';
6
- type Collapsed = 'none' | 'first' | 'second';
6
+ /** The collapsed pane, if any. */
7
+ export type Collapsed = 'none' | 'first' | 'second';
7
8
 
8
9
  // Limits on the first pane's share, in %, while dragging.
9
10
  const MIN_PERCENT = 20;
@@ -113,6 +114,8 @@ export interface SplitPaneProps {
113
114
  /** Names used on the collapse buttons, e.g. "Hide canvas". */
114
115
  firstLabel: string;
115
116
  secondLabel: string;
117
+ collapsed: Collapsed;
118
+ setCollapsed: (c: Collapsed) => void;
116
119
  class?: string;
117
120
  }
118
121
 
@@ -123,7 +126,6 @@ export interface SplitPaneProps {
123
126
  export function SplitPane(props: SplitPaneProps) {
124
127
  let ref: HTMLDivElement | undefined;
125
128
  const [firstPercent, setFirstPercent] = createSignal(50);
126
- const [collapsed, setCollapsed] = createSignal<Collapsed>('none');
127
129
 
128
130
  const startResize = (e: PointerEvent) => {
129
131
  e.preventDefault();
@@ -131,23 +133,23 @@ export function SplitPane(props: SplitPaneProps) {
131
133
  if (!ref) return;
132
134
  const pct = firstPercentAt(props.side, ref.getBoundingClientRect(), ev.clientX, ev.clientY);
133
135
  if (pct < SNAP_PERCENT) {
134
- setCollapsed('first');
136
+ props.setCollapsed('first');
135
137
  } else if (pct > 100 - SNAP_PERCENT) {
136
- setCollapsed('second');
138
+ props.setCollapsed('second');
137
139
  } else {
138
- setCollapsed('none');
140
+ props.setCollapsed('none');
139
141
  setFirstPercent(Math.min(MAX_PERCENT, Math.max(MIN_PERCENT, pct)));
140
142
  }
141
143
  });
142
144
  };
143
145
 
144
146
  const firstStyle = (): JSX.CSSProperties => {
145
- const c = collapsed();
147
+ const c = props.collapsed;
146
148
  if (c === 'first') return HIDDEN_PANE;
147
149
  if (c === 'second') return { flex: '1 1 0%' };
148
150
  return { flex: `0 0 ${firstPercent()}%` };
149
151
  };
150
- const secondStyle = (): JSX.CSSProperties => (collapsed() === 'second' ? HIDDEN_PANE : { flex: '1 1 0%' });
152
+ const secondStyle = (): JSX.CSSProperties => (props.collapsed === 'second' ? HIDDEN_PANE : { flex: '1 1 0%' });
151
153
 
152
154
  return (
153
155
  <div class={`split-pane split-pane--${props.side} ${props.class ?? ''}`} ref={ref}>
@@ -156,8 +158,8 @@ export function SplitPane(props: SplitPaneProps) {
156
158
  </div>
157
159
  <SplitDivider
158
160
  side={props.side}
159
- collapsed={collapsed()}
160
- setCollapsed={setCollapsed}
161
+ collapsed={props.collapsed}
162
+ setCollapsed={props.setCollapsed}
161
163
  firstLabel={props.firstLabel}
162
164
  secondLabel={props.secondLabel}
163
165
  onPointerDown={startResize}
@@ -0,0 +1,30 @@
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
+ }
@@ -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
+ }
@@ -5,14 +5,19 @@ import { UiLayer } from '~/lib/ui/UiLayer';
5
5
  import type { CanvasSide } from '../frontmatter';
6
6
  import { MarkdownBody } from './MarkdownBody';
7
7
 
8
- /** A canvas on `side` of the page's text. `python-editor` blocks in the text draw on it. */
9
- export function CanvasPage(props: { side: Exclude<CanvasSide, 'none'>; html: string }) {
10
- const store = new NodeStore();
8
+ /**
9
+ * A canvas on `side` of the page's text. `python-editor` blocks in the text draw on it.
10
+ * `none`: the canvas starts collapsed, on the right.
11
+ */
12
+ export function CanvasPage(props: { side: CanvasSide; html: string }) {
13
+ const store = props.side === 'none' ? new NodeStore('right', 'first') : new NodeStore(props.side, 'none');
11
14
 
12
15
  return (
13
16
  <SplitPane
14
17
  class="canvas-page"
15
- side={props.side}
18
+ side={store.canvasSide()}
19
+ collapsed={store.collapsedPane()}
20
+ setCollapsed={store.setCollapsedPane}
16
21
  first={
17
22
  <div class="canvas-page-canvas">
18
23
  <NodeCanvas store={store} />
@@ -0,0 +1,48 @@
1
+ import { createMemo, onCleanup, Show } from 'solid-js';
2
+ import { isServer } from 'solid-js/web';
3
+ import 'katex/dist/katex.min.css';
4
+ import { setCustomModules, 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
+ setCustomModules(modules);
16
+ mounted = true;
17
+ onCleanup(() => {
18
+ setCustomModules({});
19
+ mounted = false;
20
+ });
21
+ }
22
+
23
+ /**
24
+ * A page from `compilePie`, shown as a pie site shows it, in a `.pie-page` box that fills its parent.
25
+ * `modules`: the host's own functions, node types and UIs. `css`: author CSS, limited to the page.
26
+ * A new `page`, `modules` or `css` starts a fresh canvas and fresh Python variables.
27
+ * Throws on a name used twice in `modules`, or when another PiePage is mounted.
28
+ */
29
+ export function PiePage(props: { page: Page; modules?: CustomModules; css?: string }) {
30
+ const current = createMemo(() => ({ page: props.page, modules: props.modules, css: props.css }));
31
+
32
+ return (
33
+ <div class="pie-page">
34
+ <Show when={current()} keyed>
35
+ {({ page, modules, css }) => {
36
+ // Only in the browser: the server draws no nodes and runs no Python, and every request would share its modules.
37
+ if (!isServer) useModules(modules ?? {});
38
+ return (
39
+ <>
40
+ {css && <style>{`.pie-page {\n${css}\n}`}</style>}
41
+ {wrapPage(page.features, <CanvasPage side={page.frontmatter.canvas} html={page.html} />)}
42
+ </>
43
+ );
44
+ }}
45
+ </Show>
46
+ </div>
47
+ );
48
+ }
@@ -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
- /** Runs every feature's `build` for the page `file`. Throws, naming the file and key, on a bad value. */
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: Record<string, unknown>,
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 [string, FeatureBuild<unknown, unknown>][]) {
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
- const below = getComputedStyle(document.documentElement).getPropertyValue('--sidebar-drawer-below').trim() || DRAWER_BELOW;
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 links = tags.map(tag => `<a href="${escapeHtml(tagUrl(tag))}">#${escapeHtml(tag)}</a>`).join('');
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">${links}</nav>\n`,
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 { FeatureData } from './features/build';
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 { buildFeatures, FEATURE_FIELDS } from './features/build';
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 } = frontmatter;
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 { data, extensions } = buildFeatures(file, raw, site, readFile);
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
- // The site's own functions: each `functions/**/*.ts` exports `Node2NodeFunc`s, like `basic.ts`.
14
- const customModules = import.meta.glob<Record<string, unknown>>('/functions/**/*.ts', { eager: true });
15
-
16
- // File path -> its exports. Built-ins first.
17
- const modules: [string, Record<string, unknown>][] = [
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
- export const functionRegistry = new Map<string, Node2NodeFunc>();
26
- export const sideEffectRegistry = new Map<string, SideEffectFunc>();
27
- const sourceFile = new Map<string, string>();
28
- for (const [file, mod] of modules) {
29
- for (const fn of Object.values(mod).filter(fn => isNode2NodeFunc(fn) || isSideEffectFunc(fn))) {
30
- const other = sourceFile.get(fn.keyword);
31
- if (other !== undefined) {
32
- throw new Error(`Duplicate function keyword '${fn.keyword}': ${file} and ${other}`);
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,9 +1,10 @@
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';
6
6
  import type { Track } from '../animation';
7
+ import type { SplitSide } from '~/components/SplitPane';
7
8
 
8
9
  /**
9
10
  * Saves `node` with `changes`, new values for some of its numbers, in place of `node`.
@@ -34,8 +35,14 @@ export function replaceNode<N extends AnyNode>(store: NodeStore, node: N, change
34
35
  });
35
36
  }
36
37
 
37
- // Any node, but not a whole Array: an Array is looped over, so each item is hidden or shown.
38
- const anyItem = getNodeTypes().filter(type => type !== 'Array');
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
+ });
39
46
 
40
47
  /**
41
48
  * Hide a node: it is not drawn on the canvas.
@@ -43,7 +50,7 @@ const anyItem = getNodeTypes().filter(type => type !== 'Array');
43
50
  */
44
51
  export const Hide: SideEffectFunc = {
45
52
  keyword: 'hide',
46
- parameters: [{ name: 'node', type: anyItem }],
53
+ parameters: [anyItem('node')],
47
54
  run(inputs, store) {
48
55
  store.updateNode(inputs[0].id, { hidden: true });
49
56
  },
@@ -55,7 +62,7 @@ export const Hide: SideEffectFunc = {
55
62
  */
56
63
  export const Show: SideEffectFunc = {
57
64
  keyword: 'show',
58
- parameters: [{ name: 'node', type: anyItem }],
65
+ parameters: [anyItem('node')],
59
66
  run(inputs, store) {
60
67
  store.updateNode(inputs[0].id, { hidden: false });
61
68
  },
@@ -67,7 +74,7 @@ export const Show: SideEffectFunc = {
67
74
  */
68
75
  export const Remove: SideEffectFunc = {
69
76
  keyword: 'remove',
70
- parameters: [{ name: 'node', type: anyItem }],
77
+ parameters: [anyItem('node')],
71
78
  run(inputs, store) {
72
79
  store.removeNode(inputs[0].id);
73
80
  },
@@ -80,7 +87,7 @@ export const Remove: SideEffectFunc = {
80
87
  */
81
88
  export const SetFill: SideEffectFunc = {
82
89
  keyword: 'set_fill',
83
- parameters: [{ name: 'node', type: anyItem }, { name: 'color', type: 'Text' }],
90
+ parameters: [anyItem('node'), { name: 'color', type: 'Text' }],
84
91
  run(inputs, store) {
85
92
  const fill = (inputs[1] as TextNode).value;
86
93
  checkColor('set_fill', 'fill', fill);
@@ -95,7 +102,7 @@ export const SetFill: SideEffectFunc = {
95
102
  */
96
103
  export const SetStroke: SideEffectFunc = {
97
104
  keyword: 'set_stroke',
98
- parameters: [{ name: 'node', type: anyItem }, { name: 'color', type: 'Text' }],
105
+ parameters: [anyItem('node'), { name: 'color', type: 'Text' }],
99
106
  run(inputs, store) {
100
107
  const stroke = (inputs[1] as TextNode).value;
101
108
  checkColor('set_stroke', 'stroke', stroke);
@@ -114,6 +121,39 @@ export const Clear: SideEffectFunc = {
114
121
  },
115
122
  };
116
123
 
124
+ const CANVAS_SIDES: SplitSide[] = ['left', 'right', 'top', 'bottom'];
125
+
126
+ /**
127
+ * Show the canvas, and move it to `side` when given.
128
+ * @param side - 'left', 'right', 'top' or 'bottom' (default: where it is now)
129
+ */
130
+ export const ShowCanvas: SideEffectFunc = {
131
+ keyword: 'show_canvas',
132
+ parameters: [{ name: 'side', type: 'Text', defaultValue: null }],
133
+ broadcastable: false,
134
+ run(inputs, store) {
135
+ const side = (inputs[0] as TextNode | undefined)?.value;
136
+ if (side !== undefined) {
137
+ if (!CANVAS_SIDES.includes(side as SplitSide)) {
138
+ throw new Error(`show_canvas: side must be one of ${CANVAS_SIDES.join(', ')}, got '${side}'`);
139
+ }
140
+ store.setCanvasSide(side as SplitSide);
141
+ }
142
+ if (store.collapsedPane() === 'first') store.setCollapsedPane('none');
143
+ },
144
+ };
145
+
146
+ /**
147
+ * Hide the canvas. Its nodes stay.
148
+ */
149
+ export const HideCanvas: SideEffectFunc = {
150
+ keyword: 'hide_canvas',
151
+ parameters: [],
152
+ run(_inputs, store) {
153
+ store.setCollapsedPane('first');
154
+ },
155
+ };
156
+
117
157
  /**
118
158
  * Move a point to (x, y). With `animate=True` it moves there over half a second.
119
159
  * @param point - Point to move
@@ -3,8 +3,8 @@ import { runnerImport, type Plugin } from 'vite';
3
3
  import { APP_DIR } from '../../appDir';
4
4
 
5
5
  /**
6
- * Loads `registry.ts` and `state/Node.ts` once at build start, so a bad `functions/**\/*.ts`
7
- * (e.g. a keyword used twice) or `nodes/**\/*.{ts,tsx}` (e.g. a type used twice) fails the build.
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
- const options = {
26
- configFile: false as const,
25
+ await runnerImport(path.join(APP_DIR, 'lib/siteModules.ts'), {
26
+ configFile: false,
27
27
  root,
28
- logLevel: 'silent' as const,
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' as const, importSource: 'solid-js/h' } },
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
- // The site's own node types: each `nodes/**/*.{ts,tsx}` exports `NodeDefinition`s, like `nodes/square.tsx`.
180
- const customModules = import.meta.glob<Record<string, unknown>>('/nodes/**/*.{ts,tsx}', { eager: true });
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
- ...Object.entries(customModules).map(([path, mod]): [string, unknown[]] => [path.slice(1), Object.values(mod)]),
190
- ];
191
-
192
- const nodeDefinitions = new Map<string, NodeDefinition>();
193
- const sourceFile = new Map<string, string>();
194
- for (const [file, defs] of definitionsByFile) {
195
- for (const def of defs.filter(isNodeDefinition)) {
196
- const other = sourceFile.get(def.type);
197
- if (other !== undefined) {
198
- throw new Error(`Duplicate node type '${def.type}': ${file} and ${other}`);
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
  }