@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,209 @@
|
|
|
1
|
+
import { createSignal, For, onCleanup, onMount, Show } from 'solid-js';
|
|
2
|
+
import { Dynamic } from 'solid-js/web';
|
|
3
|
+
import { basicSetup, EditorView } from 'codemirror';
|
|
4
|
+
import { keymap } from '@codemirror/view';
|
|
5
|
+
import { python } from '@codemirror/lang-python';
|
|
6
|
+
import { CheckIcon, Chevron, CopyIcon, CrossIcon, PlayIcon, ResetIcon, StopIcon } from '../../../components/icons';
|
|
7
|
+
import { checkUi, UiView } from '../../ui/UiView';
|
|
8
|
+
const STATUS_TEXT = {
|
|
9
|
+
idle: '',
|
|
10
|
+
waiting: 'Waiting for another block…',
|
|
11
|
+
loading: 'Loading Python…',
|
|
12
|
+
running: 'Running…',
|
|
13
|
+
finished: '',
|
|
14
|
+
stopped: '',
|
|
15
|
+
killed: '',
|
|
16
|
+
};
|
|
17
|
+
const END_NOTE = {
|
|
18
|
+
stopped: 'Stopped.',
|
|
19
|
+
killed: 'Stopped. Python restarted, so variables from earlier blocks are gone.',
|
|
20
|
+
};
|
|
21
|
+
const RESULT = {
|
|
22
|
+
success: { icon: CheckIcon, text: 'Ran successfully' },
|
|
23
|
+
error: { icon: CrossIcon, text: 'Error' },
|
|
24
|
+
stopped: { icon: StopIcon, text: 'Stopped' },
|
|
25
|
+
};
|
|
26
|
+
const isActive = (status) => status === 'waiting' || status === 'loading' || status === 'running';
|
|
27
|
+
/**
|
|
28
|
+
* An editable block of Python. Run draws its `pie.*` calls on the scope's canvas.
|
|
29
|
+
* `collapsed`: start with only the toolbar showing.
|
|
30
|
+
*/
|
|
31
|
+
export function PythonEditor(props) {
|
|
32
|
+
let host;
|
|
33
|
+
let view;
|
|
34
|
+
let stop;
|
|
35
|
+
const [status, setStatus] = createSignal('idle');
|
|
36
|
+
const [output, setOutput] = createSignal([]);
|
|
37
|
+
// `pie.ui.*` calls of the last run. Shown below the editor, not in its output, so they stay when it collapses.
|
|
38
|
+
const [uis, setUis] = createSignal([]);
|
|
39
|
+
const [error, setError] = createSignal('');
|
|
40
|
+
const [note, setNote] = createSignal('');
|
|
41
|
+
const [collapsed, setCollapsed] = createSignal(props.collapsed ?? false);
|
|
42
|
+
const [copied, setCopied] = createSignal(false);
|
|
43
|
+
const active = () => isActive(status());
|
|
44
|
+
const result = () => {
|
|
45
|
+
if (status() === 'finished')
|
|
46
|
+
return error() ? 'error' : 'success';
|
|
47
|
+
if (status() === 'stopped' || status() === 'killed')
|
|
48
|
+
return 'stopped';
|
|
49
|
+
};
|
|
50
|
+
const initialCode = props.code.replace(/\n$/, '');
|
|
51
|
+
const addText = (text) => {
|
|
52
|
+
const last = output().at(-1);
|
|
53
|
+
if (last?.type === 'text') {
|
|
54
|
+
last.setText((prev) => prev + text);
|
|
55
|
+
return;
|
|
56
|
+
}
|
|
57
|
+
const [value, setValue] = createSignal(text);
|
|
58
|
+
setOutput((prev) => [...prev, { type: 'text', text: value, setText: setValue }]);
|
|
59
|
+
};
|
|
60
|
+
const run = () => {
|
|
61
|
+
if (active() || !view)
|
|
62
|
+
return;
|
|
63
|
+
setOutput([]);
|
|
64
|
+
setUis([]);
|
|
65
|
+
setError('');
|
|
66
|
+
setNote('');
|
|
67
|
+
stop = props.scope.run(view.state.doc.toString(), (next, err) => {
|
|
68
|
+
setStatus(next);
|
|
69
|
+
if (isActive(next))
|
|
70
|
+
return;
|
|
71
|
+
stop = undefined;
|
|
72
|
+
if (err)
|
|
73
|
+
setError(err);
|
|
74
|
+
setNote(END_NOTE[next] ?? '');
|
|
75
|
+
}, addText, (png) => setOutput((prev) => [...prev, { type: 'plot', png }]), (call) => {
|
|
76
|
+
// The runner calls this only when the page has a canvas.
|
|
77
|
+
checkUi(props.scope.store, call);
|
|
78
|
+
setUis((prev) => [...prev, call]);
|
|
79
|
+
});
|
|
80
|
+
};
|
|
81
|
+
/** Printed text and the error, without plots or the stop note. */
|
|
82
|
+
const copyText = () => [
|
|
83
|
+
...output().flatMap((entry) => (entry.type === 'text' ? [entry.text()] : [])),
|
|
84
|
+
error(),
|
|
85
|
+
]
|
|
86
|
+
.map((part) => part.replace(/\n+$/, ''))
|
|
87
|
+
.filter(Boolean)
|
|
88
|
+
.join('\n');
|
|
89
|
+
let copiedTimer;
|
|
90
|
+
const copy = async () => {
|
|
91
|
+
try {
|
|
92
|
+
await navigator.clipboard.writeText(copyText());
|
|
93
|
+
}
|
|
94
|
+
catch {
|
|
95
|
+
return; // Clipboard is only available on https or localhost.
|
|
96
|
+
}
|
|
97
|
+
setCopied(true);
|
|
98
|
+
clearTimeout(copiedTimer);
|
|
99
|
+
copiedTimer = setTimeout(() => setCopied(false), 1500);
|
|
100
|
+
};
|
|
101
|
+
/** Puts the original code back. Ctrl/Cmd+Z undoes it. */
|
|
102
|
+
const reset = () => {
|
|
103
|
+
if (!view)
|
|
104
|
+
return;
|
|
105
|
+
view.dispatch({ changes: { from: 0, to: view.state.doc.length, insert: initialCode } });
|
|
106
|
+
};
|
|
107
|
+
const toggleCollapsed = () => {
|
|
108
|
+
setCollapsed((prev) => !prev);
|
|
109
|
+
// CodeMirror can't measure while hidden, so measure again once it's shown.
|
|
110
|
+
if (!collapsed())
|
|
111
|
+
view?.requestMeasure();
|
|
112
|
+
};
|
|
113
|
+
/** Drag the bar under the code to set the editor's height. */
|
|
114
|
+
const startResize = (e) => {
|
|
115
|
+
if (!view || e.button !== 0)
|
|
116
|
+
return;
|
|
117
|
+
e.preventDefault(); // no text selection while dragging
|
|
118
|
+
const handle = e.currentTarget;
|
|
119
|
+
const editor = view.dom;
|
|
120
|
+
const startY = e.clientY;
|
|
121
|
+
const startHeight = editor.getBoundingClientRect().height;
|
|
122
|
+
// About one line, plus the 4px top and bottom padding CodeMirror puts on its content.
|
|
123
|
+
const minHeight = view.defaultLineHeight + 8;
|
|
124
|
+
const move = (ev) => {
|
|
125
|
+
editor.style.maxHeight = 'none';
|
|
126
|
+
editor.style.height = `${Math.max(minHeight, startHeight + ev.clientY - startY)}px`;
|
|
127
|
+
};
|
|
128
|
+
const end = () => {
|
|
129
|
+
handle.removeEventListener('pointermove', move);
|
|
130
|
+
handle.removeEventListener('pointerup', end);
|
|
131
|
+
handle.removeEventListener('pointercancel', end);
|
|
132
|
+
};
|
|
133
|
+
handle.setPointerCapture(e.pointerId);
|
|
134
|
+
handle.addEventListener('pointermove', move);
|
|
135
|
+
handle.addEventListener('pointerup', end);
|
|
136
|
+
handle.addEventListener('pointercancel', end);
|
|
137
|
+
};
|
|
138
|
+
const runFromKeyboard = () => {
|
|
139
|
+
run();
|
|
140
|
+
return true;
|
|
141
|
+
};
|
|
142
|
+
onMount(() => {
|
|
143
|
+
view = new EditorView({
|
|
144
|
+
doc: initialCode,
|
|
145
|
+
extensions: [
|
|
146
|
+
// Before basicSetup, so these win over its Enter bindings.
|
|
147
|
+
keymap.of([
|
|
148
|
+
{ key: 'Shift-Enter', run: runFromKeyboard },
|
|
149
|
+
{ key: 'Mod-Enter', run: runFromKeyboard },
|
|
150
|
+
]),
|
|
151
|
+
basicSetup,
|
|
152
|
+
python(),
|
|
153
|
+
],
|
|
154
|
+
parent: host,
|
|
155
|
+
});
|
|
156
|
+
});
|
|
157
|
+
onCleanup(() => {
|
|
158
|
+
stop?.();
|
|
159
|
+
clearTimeout(copiedTimer);
|
|
160
|
+
view?.destroy();
|
|
161
|
+
});
|
|
162
|
+
return (<>
|
|
163
|
+
<div class="python-editor" classList={{ 'python-editor-collapsed': collapsed() }}>
|
|
164
|
+
<div class="python-editor-code" ref={host}/>
|
|
165
|
+
<div class="python-editor-resize" title="Drag to resize" onPointerDown={startResize}/>
|
|
166
|
+
<div class="python-editor-toolbar">
|
|
167
|
+
<button type="button" class="python-editor-button" title={active() ? 'Stop' : 'Run (Shift+Enter)'} aria-label={active() ? 'Stop' : 'Run'} onClick={() => (active() ? stop?.() : run())}>
|
|
168
|
+
{active() ? <StopIcon /> : <PlayIcon />}
|
|
169
|
+
</button>
|
|
170
|
+
<button type="button" class="python-editor-button python-editor-reset" title="Reset code" aria-label="Reset code" onClick={reset}>
|
|
171
|
+
<ResetIcon />
|
|
172
|
+
</button>
|
|
173
|
+
<span class="python-editor-status">{STATUS_TEXT[status()]}</span>
|
|
174
|
+
<Show when={result()}>
|
|
175
|
+
{(r) => (<span class={`python-editor-result python-editor-result-${r()}`}>
|
|
176
|
+
<Dynamic component={RESULT[r()].icon}/>
|
|
177
|
+
{RESULT[r()].text}
|
|
178
|
+
</span>)}
|
|
179
|
+
</Show>
|
|
180
|
+
<button type="button" class="python-editor-button python-editor-collapse" title={collapsed() ? 'Expand' : 'Collapse'} aria-label={collapsed() ? 'Expand' : 'Collapse'} aria-expanded={!collapsed()} onClick={toggleCollapsed}>
|
|
181
|
+
<Chevron direction={collapsed() ? 'down' : 'up'}/>
|
|
182
|
+
</button>
|
|
183
|
+
</div>
|
|
184
|
+
<Show when={output().length > 0 || error() || note()}>
|
|
185
|
+
<div class="python-editor-output">
|
|
186
|
+
<Show when={copyText()}>
|
|
187
|
+
<button type="button" class="python-editor-button python-editor-copy" title={copied() ? 'Copied' : 'Copy output'} aria-label={copied() ? 'Copied' : 'Copy output'} onClick={copy}>
|
|
188
|
+
{copied() ? <CheckIcon /> : <CopyIcon />}
|
|
189
|
+
</button>
|
|
190
|
+
</Show>
|
|
191
|
+
<For each={output()}>
|
|
192
|
+
{(entry) => entry.type === 'text' ? (<pre class="python-editor-text">{entry.text()}</pre>) : (<img class="python-editor-plot" src={`data:image/png;base64,${entry.png}`} alt="Plot"/>)}
|
|
193
|
+
</For>
|
|
194
|
+
<Show when={error() || note()}>
|
|
195
|
+
<pre class="python-editor-text">
|
|
196
|
+
<span class="python-editor-error">{error()}</span>
|
|
197
|
+
<span class="python-editor-note">{note()}</span>
|
|
198
|
+
</pre>
|
|
199
|
+
</Show>
|
|
200
|
+
</div>
|
|
201
|
+
</Show>
|
|
202
|
+
</div>
|
|
203
|
+
<Show when={uis().length > 0}>
|
|
204
|
+
<div class="python-editor-uis">
|
|
205
|
+
<For each={uis()}>{(call) => <UiView store={props.scope.store} call={call}/>}</For>
|
|
206
|
+
</div>
|
|
207
|
+
</Show>
|
|
208
|
+
</>);
|
|
209
|
+
}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A feature is an optional extra that a page turns on in its frontmatter, e.g. `sidebar: _topics.md`.
|
|
3
|
+
* If the page leaves the key out, the value in `site.yml` is used.
|
|
4
|
+
*
|
|
5
|
+
* Each feature has two halves:
|
|
6
|
+
* - Build half (this file). Runs in Node when the site is generated. Its `build` turns the frontmatter value
|
|
7
|
+
* into data, e.g. the path `_topics.md` into that file's HTML. The data is saved in the page's JSON.
|
|
8
|
+
* - View half (`views.tsx`). Runs in the browser. A component that gets that data and wraps the page,
|
|
9
|
+
* e.g. draws the sidebar next to it.
|
|
10
|
+
*
|
|
11
|
+
* To add a feature: add its build half to `FEATURES` below, and its view, if it has one, to `VIEWS` in `views.tsx`.
|
|
12
|
+
* A feature that only changes the page's HTML, like `toc`, has no view.
|
|
13
|
+
*/
|
|
14
|
+
import type { MarkedExtension } from 'marked';
|
|
15
|
+
import type { Field, Values } from '../frontmatter';
|
|
16
|
+
/** The second argument to a feature's `build`. */
|
|
17
|
+
export interface BuildContext {
|
|
18
|
+
/** The page, e.g. `pages/about.md`. */
|
|
19
|
+
file: string;
|
|
20
|
+
/** The file that set the value: the page, or `site.yml`. */
|
|
21
|
+
from: string;
|
|
22
|
+
/** 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. */
|
|
23
|
+
readFile(path: string): string;
|
|
24
|
+
/** False inside another app (`compilePie`), which has no tag pages to link to. */
|
|
25
|
+
tagLinks: boolean;
|
|
26
|
+
}
|
|
27
|
+
/** A feature's build half. `T`: its frontmatter value. `D`: the data its view gets. */
|
|
28
|
+
export interface FeatureBuild<T, D> {
|
|
29
|
+
field: Field<T>;
|
|
30
|
+
/** Returns `undefined` to turn the feature off on this page. */
|
|
31
|
+
build(value: T, ctx: BuildContext): D | undefined;
|
|
32
|
+
/** Extra markdown syntax for this page. Called only when `build` didn't turn the feature off. */
|
|
33
|
+
markdown?(value: T, ctx: BuildContext): MarkedExtension;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Keyed by frontmatter key. The last feature's `postprocess` runs first, so `tags`, being after `glossary`,
|
|
37
|
+
* ends the page's text and the glossary comes after it.
|
|
38
|
+
*/
|
|
39
|
+
declare const FEATURES: {
|
|
40
|
+
sidebar: FeatureBuild<string, string>;
|
|
41
|
+
toc: FeatureBuild<boolean, true>;
|
|
42
|
+
glossary: FeatureBuild<string, true>;
|
|
43
|
+
tags: FeatureBuild<string[], true>;
|
|
44
|
+
};
|
|
45
|
+
type DataOf<F> = F extends FeatureBuild<any, infer D> ? D : never;
|
|
46
|
+
export type FeatureData = {
|
|
47
|
+
[K in keyof typeof FEATURES]?: DataOf<(typeof FEATURES)[K]>;
|
|
48
|
+
};
|
|
49
|
+
export declare const FEATURE_FIELDS: { [K in keyof typeof FEATURES]: (typeof FEATURES)[K]['field']; };
|
|
50
|
+
/** Each feature's value, as `site.yml` gives it. */
|
|
51
|
+
export type FeatureValues = Values<typeof FEATURE_FIELDS>;
|
|
52
|
+
/**
|
|
53
|
+
* Runs every feature's `build` for the page `file`. Throws, naming the file and key, on a bad value.
|
|
54
|
+
* A key that neither the page nor `site` sets takes its field's default.
|
|
55
|
+
*/
|
|
56
|
+
export declare function buildFeatures(file: string, raw: Record<string, string>, site: Partial<FeatureValues>, readFile: (from: string, path: string) => string, tagLinks: boolean): {
|
|
57
|
+
data: FeatureData;
|
|
58
|
+
extensions: MarkedExtension[];
|
|
59
|
+
};
|
|
60
|
+
export {};
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { glossary } from './glossary/build';
|
|
2
|
+
import { sidebar } from './sidebar/build';
|
|
3
|
+
import { tags } from './tags/build';
|
|
4
|
+
import { toc } from './toc/build';
|
|
5
|
+
/**
|
|
6
|
+
* Keyed by frontmatter key. The last feature's `postprocess` runs first, so `tags`, being after `glossary`,
|
|
7
|
+
* ends the page's text and the glossary comes after it.
|
|
8
|
+
*/
|
|
9
|
+
const FEATURES = { sidebar, toc, glossary, tags };
|
|
10
|
+
// Today: `{ sidebar: sidebar.field, toc: toc.field, glossary: glossary.field, tags: tags.field }`. `site.yml` reads these too.
|
|
11
|
+
export const FEATURE_FIELDS = Object.fromEntries(Object.entries(FEATURES).map(([key, feature]) => [key, feature.field]));
|
|
12
|
+
/**
|
|
13
|
+
* Runs every feature's `build` for the page `file`. Throws, naming the file and key, on a bad value.
|
|
14
|
+
* A key that neither the page nor `site` sets takes its field's default.
|
|
15
|
+
*/
|
|
16
|
+
export function buildFeatures(file, raw, site, readFile, tagLinks) {
|
|
17
|
+
const data = {};
|
|
18
|
+
const extensions = [];
|
|
19
|
+
for (const [key, feature] of Object.entries(FEATURES)) {
|
|
20
|
+
const from = Object.hasOwn(raw, key) ? file : 'site.yml';
|
|
21
|
+
try {
|
|
22
|
+
const value = from === file ? feature.field.parse(raw[key]) : site[key] ?? feature.field.default;
|
|
23
|
+
const ctx = { file, from, readFile: path => readFile(from, path), tagLinks };
|
|
24
|
+
const built = feature.build(value, ctx);
|
|
25
|
+
if (built === undefined)
|
|
26
|
+
continue;
|
|
27
|
+
data[key] = built;
|
|
28
|
+
if (feature.markdown)
|
|
29
|
+
extensions.push(feature.markdown(value, ctx));
|
|
30
|
+
}
|
|
31
|
+
catch (e) {
|
|
32
|
+
throw new Error(`${from}: \`${key}\` ${e.message}`);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
return { data: data, extensions };
|
|
36
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { type JSX } from 'solid-js';
|
|
2
|
+
/**
|
|
3
|
+
* `children`, whose glossary terms show a popup on hover. A tap on a term also shows it, for touch screens,
|
|
4
|
+
* and a tap anywhere else hides it. The popup's "more" opens the page's glossary at the term.
|
|
5
|
+
*/
|
|
6
|
+
export declare function GlossaryView(props: {
|
|
7
|
+
data: true;
|
|
8
|
+
children: JSX.Element;
|
|
9
|
+
}): JSX.Element;
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { onCleanup, onMount } from 'solid-js';
|
|
2
|
+
/** A popup for `term`: the start of its definition, copied from the page's glossary, and a "more" link to all of it. */
|
|
3
|
+
function makePopup(term) {
|
|
4
|
+
const id = term.dataset.term;
|
|
5
|
+
const definition = document.getElementById(id).nextElementSibling;
|
|
6
|
+
const popup = document.createElement('div');
|
|
7
|
+
popup.className = 'term-popup';
|
|
8
|
+
popup.innerHTML = `<div class="term-popup-text">${definition.innerHTML}</div><a class="term-more" href="#${id}">more</a>`;
|
|
9
|
+
return popup;
|
|
10
|
+
}
|
|
11
|
+
/** Gives `term` its popup if it has none, and puts it under the term, or over it when the term is in the lower half of the screen. */
|
|
12
|
+
function place(term) {
|
|
13
|
+
const popup = term.querySelector(':scope > .term-popup') ?? term.appendChild(makePopup(term));
|
|
14
|
+
const rect = term.getBoundingClientRect();
|
|
15
|
+
const height = document.documentElement.clientHeight;
|
|
16
|
+
const below = rect.bottom < height / 2;
|
|
17
|
+
popup.style.top = below ? `${rect.bottom}px` : '';
|
|
18
|
+
popup.style.bottom = below ? '' : `${height - rect.top}px`;
|
|
19
|
+
popup.style.setProperty('--term-left', `${rect.left}px`);
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* `children`, whose glossary terms show a popup on hover. A tap on a term also shows it, for touch screens,
|
|
23
|
+
* and a tap anywhere else hides it. The popup's "more" opens the page's glossary at the term.
|
|
24
|
+
*/
|
|
25
|
+
export function GlossaryView(props) {
|
|
26
|
+
onMount(() => {
|
|
27
|
+
const onPointerOver = (e) => {
|
|
28
|
+
const term = e.target.closest('.term');
|
|
29
|
+
if (term)
|
|
30
|
+
place(term);
|
|
31
|
+
};
|
|
32
|
+
// Capture: runs before the router follows a "more" link, so the glossary is open when it scrolls to the term.
|
|
33
|
+
const onClick = (e) => {
|
|
34
|
+
const target = e.target;
|
|
35
|
+
const inPopup = target.closest('.term-popup') !== null;
|
|
36
|
+
const more = target.closest('.term-more') !== null;
|
|
37
|
+
if (inPopup && !more)
|
|
38
|
+
return;
|
|
39
|
+
if (more)
|
|
40
|
+
document.querySelector('.glossary').open = true;
|
|
41
|
+
const term = inPopup ? null : target.closest('.term');
|
|
42
|
+
for (const open of document.querySelectorAll('.term--open'))
|
|
43
|
+
if (open !== term)
|
|
44
|
+
open.classList.remove('term--open');
|
|
45
|
+
if (term) {
|
|
46
|
+
place(term);
|
|
47
|
+
term.classList.toggle('term--open');
|
|
48
|
+
}
|
|
49
|
+
};
|
|
50
|
+
// A popup is fixed to the screen, so it's moved to follow its term.
|
|
51
|
+
const onScroll = () => document.querySelectorAll('.term--open, .term:hover').forEach(place);
|
|
52
|
+
document.addEventListener('pointerover', onPointerOver);
|
|
53
|
+
document.addEventListener('click', onClick, true);
|
|
54
|
+
document.addEventListener('scroll', onScroll, true);
|
|
55
|
+
onCleanup(() => {
|
|
56
|
+
document.removeEventListener('pointerover', onPointerOver);
|
|
57
|
+
document.removeEventListener('click', onClick, true);
|
|
58
|
+
document.removeEventListener('scroll', onScroll, true);
|
|
59
|
+
});
|
|
60
|
+
});
|
|
61
|
+
return props.children;
|
|
62
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { FeatureBuild } from '../build';
|
|
2
|
+
/**
|
|
3
|
+
* `glossary: _terms.md`: `[[term]]` and `[[shown|term]]` in the page become terms with a popup (see `View.tsx`),
|
|
4
|
+
* and the page ends with a collapsed glossary of the terms it uses. A term missing from the file fails the build.
|
|
5
|
+
*/
|
|
6
|
+
export declare const glossary: FeatureBuild<string, true>;
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import { Lexer } from 'marked';
|
|
2
|
+
import { markdownFile } from '../../frontmatter';
|
|
3
|
+
import { renderInline, renderMarkdown } from '../../render';
|
|
4
|
+
/** Matching ignores case. */
|
|
5
|
+
const key = (name) => name.trim().toLowerCase();
|
|
6
|
+
/** The section's title when the glossary file has no `#` heading. */
|
|
7
|
+
const DEFAULT_TITLE = 'Glossary';
|
|
8
|
+
/**
|
|
9
|
+
* The glossary file `file`. `title`: its `#` heading, as HTML. `terms`: by `key`, in the file's order.
|
|
10
|
+
* Each `##` heading is a term, and the text up to the next one is its definition.
|
|
11
|
+
* Other text before the first `##` is ignored.
|
|
12
|
+
*/
|
|
13
|
+
function parseGlossary(file, markdown) {
|
|
14
|
+
let title = DEFAULT_TITLE;
|
|
15
|
+
const sections = [];
|
|
16
|
+
for (const token of Lexer.lex(markdown)) {
|
|
17
|
+
if (token.type === 'heading' && token.depth === 2)
|
|
18
|
+
sections.push({ name: token.text, body: '' });
|
|
19
|
+
else if (sections.length > 0)
|
|
20
|
+
sections[sections.length - 1].body += token.raw;
|
|
21
|
+
else if (token.type === 'heading' && token.depth === 1)
|
|
22
|
+
title = renderInline(token.text);
|
|
23
|
+
}
|
|
24
|
+
const terms = new Map();
|
|
25
|
+
sections.forEach(({ name, body }, i) => {
|
|
26
|
+
if (terms.has(key(name)))
|
|
27
|
+
throw new Error(`file ${file} has the term ${JSON.stringify(name)} twice`);
|
|
28
|
+
terms.set(key(name), { id: `term-${i + 1}`, name: renderInline(name), html: renderMarkdown(body) });
|
|
29
|
+
});
|
|
30
|
+
return { title, terms };
|
|
31
|
+
}
|
|
32
|
+
/** The page's glossary, collapsed: the terms it uses, in the glossary file's order. */
|
|
33
|
+
function glossaryHtml(title, terms) {
|
|
34
|
+
const entries = terms.map(t => `<dt id="${t.id}">${t.name}</dt><dd>${t.html}</dd>`).join('');
|
|
35
|
+
return `<details class="glossary"><summary>${title}</summary><dl>${entries}</dl></details>\n`;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* `glossary: _terms.md`: `[[term]]` and `[[shown|term]]` in the page become terms with a popup (see `View.tsx`),
|
|
39
|
+
* and the page ends with a collapsed glossary of the terms it uses. A term missing from the file fails the build.
|
|
40
|
+
*/
|
|
41
|
+
export const glossary = {
|
|
42
|
+
field: markdownFile(),
|
|
43
|
+
build: file => (file === '' || file === 'none' ? undefined : true),
|
|
44
|
+
markdown(file, ctx) {
|
|
45
|
+
const { title, terms } = parseGlossary(file, ctx.readFile(file));
|
|
46
|
+
const used = new Set();
|
|
47
|
+
return {
|
|
48
|
+
extensions: [
|
|
49
|
+
{
|
|
50
|
+
name: 'term',
|
|
51
|
+
level: 'inline',
|
|
52
|
+
start(src) {
|
|
53
|
+
const at = src.indexOf('[[');
|
|
54
|
+
return at === -1 ? undefined : at;
|
|
55
|
+
},
|
|
56
|
+
tokenizer(src) {
|
|
57
|
+
const match = /^\[\[([^[\]|]+)(?:\|([^[\]|]+))?\]\]/.exec(src);
|
|
58
|
+
if (!match)
|
|
59
|
+
return undefined;
|
|
60
|
+
const [raw, shown, name = shown] = match;
|
|
61
|
+
const term = terms.get(key(name));
|
|
62
|
+
if (!term)
|
|
63
|
+
throw new Error(`[[${name}]]: ${file} has no such term`);
|
|
64
|
+
used.add(term);
|
|
65
|
+
return { type: 'term', raw, term, tokens: this.lexer.inlineTokens(shown) };
|
|
66
|
+
},
|
|
67
|
+
renderer(token) {
|
|
68
|
+
return `<span class="term" data-term="${token.term.id}">${this.parser.parseInline(token.tokens)}</span>`;
|
|
69
|
+
},
|
|
70
|
+
},
|
|
71
|
+
],
|
|
72
|
+
hooks: {
|
|
73
|
+
postprocess(html) {
|
|
74
|
+
if (used.size === 0)
|
|
75
|
+
return html;
|
|
76
|
+
return html + glossaryHtml(title, [...terms.values()].filter(t => used.has(t)));
|
|
77
|
+
},
|
|
78
|
+
},
|
|
79
|
+
};
|
|
80
|
+
},
|
|
81
|
+
};
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { type JSX } from 'solid-js';
|
|
2
|
+
/**
|
|
3
|
+
* `children` with a sidebar on the left showing `data`, the sidebar file's HTML. It starts collapsed; opening it squeezes `children`.
|
|
4
|
+
* On screens narrower than `--sidebar-drawer-below`, it opens over the page instead,
|
|
5
|
+
* and a tap outside it or on one of its links closes it.
|
|
6
|
+
* Its Python editors share variables only with each other, and have no canvas.
|
|
7
|
+
*/
|
|
8
|
+
export declare function SidebarView(props: {
|
|
9
|
+
data: string;
|
|
10
|
+
children: JSX.Element;
|
|
11
|
+
}): JSX.Element;
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { createSignal, onCleanup, onMount, Show } from 'solid-js';
|
|
2
|
+
import { Chevron } from '../../../../components/icons';
|
|
3
|
+
import { MarkdownBody } from '../../components/MarkdownBody';
|
|
4
|
+
// Used when `theme.css` doesn't set `--sidebar-drawer-below`.
|
|
5
|
+
const DRAWER_BELOW = '768px';
|
|
6
|
+
/**
|
|
7
|
+
* `children` with a sidebar on the left showing `data`, the sidebar file's HTML. It starts collapsed; opening it squeezes `children`.
|
|
8
|
+
* On screens narrower than `--sidebar-drawer-below`, it opens over the page instead,
|
|
9
|
+
* and a tap outside it or on one of its links closes it.
|
|
10
|
+
* Its Python editors share variables only with each other, and have no canvas.
|
|
11
|
+
*/
|
|
12
|
+
export function SidebarView(props) {
|
|
13
|
+
const [open, setOpen] = createSignal(false);
|
|
14
|
+
const [drawer, setDrawer] = createSignal(false);
|
|
15
|
+
let page;
|
|
16
|
+
onMount(() => {
|
|
17
|
+
// Read here, not on `:root`, so a host can set it on its `.pie-page`.
|
|
18
|
+
const below = getComputedStyle(page).getPropertyValue('--sidebar-drawer-below').trim() || DRAWER_BELOW;
|
|
19
|
+
const query = matchMedia(`(width < ${below})`);
|
|
20
|
+
const update = () => setDrawer(query.matches);
|
|
21
|
+
update();
|
|
22
|
+
query.addEventListener('change', update);
|
|
23
|
+
onCleanup(() => query.removeEventListener('change', update));
|
|
24
|
+
});
|
|
25
|
+
// In the drawer, following a link closes it, even a link to a place on this page.
|
|
26
|
+
const closeOnLink = (e) => {
|
|
27
|
+
if (drawer() && e.target.closest('a'))
|
|
28
|
+
setOpen(false);
|
|
29
|
+
};
|
|
30
|
+
const label = () => (open() ? 'Hide sidebar' : 'Show sidebar');
|
|
31
|
+
return (<div ref={page} class="sidebar-page" classList={{ 'sidebar-page--open': open(), 'sidebar-page--drawer': drawer() }}>
|
|
32
|
+
<aside id="sidebar" class="sidebar" onClick={closeOnLink}>
|
|
33
|
+
<MarkdownBody class="sidebar-text" html={props.data}/>
|
|
34
|
+
</aside>
|
|
35
|
+
<div class="sidebar-edge">
|
|
36
|
+
<button class="sidebar-toggle" onClick={() => setOpen(!open())} aria-expanded={open()} aria-controls="sidebar" aria-label={label()} title={label()}>
|
|
37
|
+
<Chevron direction={open() ? 'left' : 'right'}/>
|
|
38
|
+
</button>
|
|
39
|
+
</div>
|
|
40
|
+
<Show when={drawer() && open()}>
|
|
41
|
+
<div class="sidebar-backdrop" onClick={() => setOpen(false)}/>
|
|
42
|
+
</Show>
|
|
43
|
+
<div class="sidebar-page-content">{props.children}</div>
|
|
44
|
+
</div>);
|
|
45
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { markdownFile } from '../../frontmatter';
|
|
2
|
+
import { renderMarkdown } from '../../render';
|
|
3
|
+
/** `sidebar: _topics.md`: that file's HTML, shown in a sidebar. `none`: no sidebar, even when `site.yml` sets one. */
|
|
4
|
+
export const sidebar = {
|
|
5
|
+
field: markdownFile(),
|
|
6
|
+
build: (file, ctx) => (file === '' || file === 'none' ? undefined : renderMarkdown(ctx.readFile(file))),
|
|
7
|
+
};
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { FeatureBuild } from '../build';
|
|
2
|
+
/**
|
|
3
|
+
* `tags: #math #linear-algebra`: the page's tags, as links at the end of its text. Each link opens the page
|
|
4
|
+
* listing the pages with that tag, in `src/routes/_tags/`. Only a page's own frontmatter counts, not `site.yml`.
|
|
5
|
+
* Without `tagLinks`, the tags are plain text.
|
|
6
|
+
*/
|
|
7
|
+
export declare const tags: FeatureBuild<string[], true>;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { hashtags } from '../../frontmatter';
|
|
2
|
+
import { escapeHtml } from '../../render';
|
|
3
|
+
import { tagUrl } from './url';
|
|
4
|
+
/**
|
|
5
|
+
* `tags: #math #linear-algebra`: the page's tags, as links at the end of its text. Each link opens the page
|
|
6
|
+
* listing the pages with that tag, in `src/routes/_tags/`. Only a page's own frontmatter counts, not `site.yml`.
|
|
7
|
+
* Without `tagLinks`, the tags are plain text.
|
|
8
|
+
*/
|
|
9
|
+
export const tags = {
|
|
10
|
+
field: hashtags(),
|
|
11
|
+
build: (tags, ctx) => (ctx.from === ctx.file && tags.length > 0 ? true : undefined),
|
|
12
|
+
markdown(tags, ctx) {
|
|
13
|
+
const items = tags.map(tag => (ctx.tagLinks
|
|
14
|
+
? `<a href="${escapeHtml(tagUrl(tag))}">#${escapeHtml(tag)}</a>`
|
|
15
|
+
: `<span>#${escapeHtml(tag)}</span>`)).join('');
|
|
16
|
+
return {
|
|
17
|
+
hooks: {
|
|
18
|
+
postprocess: html => `${html}<nav class="tags">${items}</nav>\n`,
|
|
19
|
+
},
|
|
20
|
+
};
|
|
21
|
+
},
|
|
22
|
+
};
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
/** The page listing every tag. Starts with `_` so it can't be taken by a site's own page. */
|
|
2
|
+
export declare const TAGS_URL = "/_tags";
|
|
3
|
+
/** The last part of the URL of the page for `tag`. Not `encodeURIComponent`: prerendering would encode its `%2B` again. */
|
|
4
|
+
export declare function tagSegment(tag: string): string;
|
|
5
|
+
/** The page listing the pages with `tag`. */
|
|
6
|
+
export declare function tagUrl(tag: string): string;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/** The page listing every tag. Starts with `_` so it can't be taken by a site's own page. */
|
|
2
|
+
export const TAGS_URL = '/_tags';
|
|
3
|
+
/** The last part of the URL of the page for `tag`. Not `encodeURIComponent`: prerendering would encode its `%2B` again. */
|
|
4
|
+
export function tagSegment(tag) {
|
|
5
|
+
return encodeURI(tag);
|
|
6
|
+
}
|
|
7
|
+
/** The page listing the pages with `tag`. */
|
|
8
|
+
export function tagUrl(tag) {
|
|
9
|
+
return `${TAGS_URL}/${tagSegment(tag)}`;
|
|
10
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { FeatureBuild } from '../build';
|
|
2
|
+
/**
|
|
3
|
+
* `toc: true`: a table of contents of the page's `##` and `###` headings, after its leading `#` title,
|
|
4
|
+
* or at the very start if the page doesn't begin with one. Every heading gets an `id` to link to.
|
|
5
|
+
* No list when the page has no such heading.
|
|
6
|
+
*/
|
|
7
|
+
export declare const toc: FeatureBuild<boolean, true>;
|