-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.html
997 lines (802 loc) · 35.3 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>@celine/celine</title>
<link rel="stylesheet" href="celine/cell.css" />
<link rel="stylesheet" href="libertine/libertine.css" />
<meta name="theme-color" content="#ffffff">
<link rel="apple-touch-icon" sizes="180x180" href="static/apple-touch-icon.png">
<link rel="icon" type="image/png" sizes="32x32" href="static/favicon-32x32.png">
<link rel="icon" type="image/png" sizes="16x16" href="static/favicon-16x16.png">
<link rel="manifest" href="static/site.webmanifest">
<meta name="description" content="A library for building reactive HTML notebooks with inline contenteditable <script>s">
<meta name="author" content="Max Bo">
<meta name="keywords" content="celine, celine, reactive, html, notebook, observable, runtime, api, library, esm, module, script, style, element, contenteditable, reevaluate, blur, cell, viewof, mutable, counter, fizzbuzz, name, greeting, ref, increment, sword">
<meta name="date" content="2024-09-18">
<meta name="license" content="MIT">
<meta property="og:title" content="@celine/celine">
<meta property="og:description" content="A library for building reactive HTML notebooks with inline contenteditable <script>s">
<meta property="og:url" content="https://maxbo.me/celine/">
<meta property="og:type" content="website">
<meta property="og:site_name" content="Max Bo">
<meta property="og:image" content="https://maxbo.me/celine/static/og.png">
<meta name="twitter:card" content="summary">
<meta name="twitter:creator" content="@_max_bo_">
<meta name="twitter:title" content="@celine/celine">
<meta name="twitter:description" content="A library for building reactive HTML notebooks with inline contenteditable <script>s">
<meta name="twitter:image" content="https://maxbo.me/celine/static/og.png">
<meta name="twitter:card" content="summary_large_image">
<script defer>
document.addEventListener("DOMContentLoaded", () => {
const toc = document.getElementById("toccontainer");
const headers = document.querySelectorAll("h2, h3, h4, h5, h6");
// Create an object to store the current ol element at each level
const levels = {};
let prevLevel = 1; // Start with h2 (level 1)
for (const header of headers) {
const level = parseInt(header.tagName.substring(1)) - 2; // Convert h2-h6 to 0-4
// If going deeper, create new ol
if (level > prevLevel) {
const parentLi = levels[prevLevel].lastElementChild;
const newOl = document.createElement("ol");
parentLi.appendChild(newOl);
levels[level] = newOl;
}
// If going back up, remove references to deeper levels
if (level < prevLevel) {
for (let i = prevLevel; i > level; i--) {
delete levels[i];
}
}
// Create list item with link
const li = document.createElement("li");
const a = document.createElement("a");
const headerLink = header.querySelector('a');
if (!headerLink) {
continue;
}
a.href = `#${header.querySelector('a').id}`;
a.innerHTML = header.innerHTML;
// Remove the anchor links
a.querySelectorAll('a[href^="#"]').forEach(link => link.remove());
li.appendChild(a);
// Add to the appropriate ol
if (!levels[level]) {
const newOl = document.createElement("ol");
toc.appendChild(newOl);
levels[level] = newOl;
}
levels[level].appendChild(li);
prevLevel = level;
}
});
</script>
</head>
<body>
<main>
<div style="text-align: right; text-decoration: none; font-family: 'Libertinus Sans', sans-serif;">
<a style="text-decoration: none;" href="libertine/index.html">/libertine</a>,
<a style="text-decoration: none;" href="bibhtml/index.html">/bibhtml</a>
</div>
<h1 class="title">@celine/celine</h1>
<style>
.postit {
float: right;
width: 100px;
z-index: 10;
filter: contrast(150%) brightness(120%);
}
</style>
<img src="static/snail.webp" alt="chiocciolina (Italian: small snail)" class="postit">
<div class="authors">
<div class="author">
<span class="author-name"><a href="https://maxbo.me">Max Bo</a></span>
</div>
</div>
<section class="abstract">
<p>
<cite>@celine/celine</cite> is a library for building reactive HTML notebooks with <code>display: block</code> <code>contenteditable</code> <code><script></code> elements.
It wraps a subset of the <a href="https://observablehq.com/documentation/notebooks/">Observable Notebook</a> <a href="https://github.com/observablehq/runtime">runtime</a> to power inter-cell reactivity, just like <a href="https://observablehq.com/framework/reactivity">Observable Framework</a> and <a href="https://quarto.org/docs/interactive/ojs/">Quarto</a>. It aims to make it easier to publish research as HTML files rather than as PDF files.
</p>
<p>
I initially considered calling this library <i>incel</i>, short for <q>inline cell</q>, but was advised against it.
</p>
<style>
@keyframes bounce {
0%, 20%, 50%, 80%, 100% {
transform: translateY(0);
}
40% {
transform: translateY(-2px);
}
60% {
transform: translateY(-1px);
}
}
.bounce {
animation: bounce 2s infinite;
}
</style>
<p class="bounce"><a href="#demo"><b>Jump straight to the demo?</a></b></p>
</section>
<table class="header">
<tbody>
<tr>
<th>GitHub</th>
<td class="width-auto"><a href="https://github.com/MaxwellBo/celine">MaxwellBo/<wbr>celine</a><span id="stars"></span> + <a href="https://github.com/MaxwellBo/celine/issues">/issues</a> </td>
<th class="width-min">License</th>
<td><a href="https://github.com/MaxwellBo/celine/blob/master/LICENSE">MIT</a></td>
</tr>
<script type="module">
// ping the GitHub API for the number of stars
fetch("https://api.github.com/repos/MaxwellBo/celine")
.then(d => d.json())
.then(resp => {
const stars = resp.stargazers_count;
document.getElementById("stars").textContent = ` (⭐ ${stars})`;
})
</script>
<tr>
<th>JSR</th>
<td class="width-auto"><a href="https://jsr.io/@celine/celine">jsr.io/<wbr>@celine/<wbr>celine</a> + <a href="https://jsr.io/@celine/celine/doc">/docs</a></td>
<th>Version</th>
<!-- these should be updated to the best of your ability -->
<td class="width-min"><a href="https://jsr.io/@celine/celine/versions" id="version">4.1.0</a>, <time id="updated">Oct 15 2024</time></td>
</tr>
<script type="module">
fetch("https://npm.jsr.io/@jsr/celine__celine")
.then(d => d.json())
.then(resp => {
const latestVersion = resp["dist-tags"].latest;
document.getElementById("version").textContent = latestVersion;
const date = new Date(resp.time[latestVersion]);
const formattedDate = `${date.toLocaleString('default', { month: 'short' })} ${date.getDate()} ${date.getFullYear()}`;
document.getElementById("updated").textContent = formattedDate;
})
</script>
</tbody>
</table>
<br />
<h2 id="toc"><a href="#toc" id="toc"></a>Table of Contents</h2>
<style>
.table-of-contents > * {
animation: fadeIn 0.3s ease-in;
}
@keyframes fadeIn {
from {
opacity: 0;
}
to {
opacity: 1;
}
}
</style>
<div class="table-of-contents" id="toccontainer">
</div>
<h2><a href="#installation" id="installation"></a>Installation</h2>
<p>Add the following <code><script></code> element to your HTML file's <code><head></code> block:</p>
<script type="module" class="echo reflect" id="import">
import { CelineModule, registerScriptReevaluationOnBlur } from 'https://esm.sh/jsr/@celine/[email protected]';
import * as Inputs from 'https://esm.run/@observablehq/[email protected]';
import * as htl from 'https://esm.run/[email protected]';
window.celine = CelineModule.usingNewObservableRuntimeAndModule(document);
window.library = celine.library; /* @observablehq/stdlib */
window.Inputs = Inputs;
window.htl = htl;
registerScriptReevaluationOnBlur(document, /*class=*/'echo');
</script>
<style>
script#installation::before {
content: "<script type=\"module\">";
display: block;
}
</style>
<p>Link <a href="#.echo">cell.css</a> in your <code><head></code> block:</p>
<pre class="echo" style="margin-bottom: 1ch;">
<link
rel="stylesheet"
href="https://esm.sh/jsr/@celine/[email protected]/cell.css" />
</pre>
<p>
You may want to include <cite>@celine/celine</cite>'s drop-in stylesheet, <a href="libertine/index.html">libertine.css</a>:
</p>
<pre class="echo" style="margin-bottom: 1ch;">
<link
rel="stylesheet"
href="https://esm.sh/jsr/@celine/[email protected]/libertine.css" />
</pre>
<h2><a href="#demo" id="demo"></a>Demo: Observable Plot + SQLite</h2>
<p><i>Try removing a <code>0</code> from the <code>WHERE</code> condition, then click away from the <code><script></code> to <abbr title="lose focus">blur</abbr> and reevaluate.</i></p>
<script type="module" class="echo reflect" id="chinook" contenteditable="true">
import * as Plot from 'https://esm.run/@observablehq/[email protected]';
celine.cell("chinook", async () => {
const url = "https://maxbo.me/celine/static/chinook.db";
const db = await library.SQLiteDatabaseClient().open(url);
const tracks = await db.sql`SELECT * FROM tracks WHERE Milliseconds < 1000000`;
return Plot.plot({
title: htl.html`<h3>Track length distribution</h3>`,
marks: [
Plot.rectY(
tracks,
Plot.binX({ y: "count" }, { x: "Milliseconds", tip: true })
),
Plot.ruleY([0])
]
});
});
</script>
<small>
<a href="https://observablehq.com/framework/lib/sqlite"><code>SQLiteDatabaseClient</code> docs</a>
</small>
<h2><a href="#api" id="api"></a>API</h2>
<p>The following <code><styles></code>s are marked <code>contenteditable</code> and reevaluate on edit.</p>
<h3><a href="#.echo" id=".echo"></a><code>.echo</code></h3>
<p>The <code>.echo</code> class can display <code><script></code> and <code><style></code> elements inline, using a <a href="https://blog.glyphdrawing.club/font-with-built-in-syntax-highlighting/"><cite>font with built-in syntax highlighting</cite></a>.</p>
<p><i>Try changing the <code>border</code> thickness!</i></p>
<style class="echo" id="echo-style" contenteditable="true">#echo-style {
border: 3px solid fuchsia;
}
</style>
<p><code>.echo</code> has a dark mode. Set the <code>class</code> attribute to <code>echo dark</code> to enable:</p>
<style class="echo dark" id="echo-dark-style" contenteditable="true">#echo-dark-style {
transform: rotate(0.5deg);
}
</style>
<h3><a href="#.reflect" id=".reflect"></a><code>.reflect</code></h3>
<p>The <code>.reflect</code> class forces <code><script></code> and <code><style></code> elements to display their opening and closing tags, <code>type</code>, <code>class</code>, <code>id</code>, and <code>contenteditable</code> attributes (a little trick from <a href="https://secretgeek.github.io/html_wysiwyg/html.html"><cite>This page is a truly naked, brutalist html quine</cite></a>).</p>
<style class="echo reflect" id="reflect-style" contenteditable="true">
#reflect-style {
border: 3px solid lime;
}
</style>
<p>All of the following <code><script></code>s are marked <code>contenteditable</code> and reevaluate on <abbr title="loss of focus">blur</abbr>.</p>
<h3><a href="#cell" id="cell"></a><code>celine.cell(name, [inputs, ]definition)</code></h3>
<p>The <code>cell</code> constructor declares a reactive cell called <code>"${name}"</code>.</p>
<p>The <code>definition</code> can be <code>T</code> or <code>(...inputs) => T</code>, where <code>T</code> can be <code>object</code>, <code>Promise<?></code>, <code>Iterator<?></code>, or <code>AsyncIterator<?></code>.</p>
<p>Cells render their current value above an element that has an <code>id</code> the same as the cell's <code>name</code>. Thus, to render the counter value above the <code><script></code>, we set <code>id="counter"</code> on the <code><script></code>:</p>
<script type="module" class="echo reflect" id="counter" contenteditable="true">
celine.cell("counter", async function* () {
let i = 0;
while (true) {
await library.Promises.delay(1000);
yield i++;
}
});
</script>
<p>The <code>cell</code> constructor accepts <code>inputs</code>, a list of other cell names to depend on.</p></p>
<p>Here we use <a href="https://observablehq.com/@observablehq/htl">Hypertext Literal</a>'s <code>html</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals">template literal</a>, to transform the value of another cell:</p>
<script type="module" class="echo reflect" id="fizzbuzz" contenteditable="true">
celine.cell("fizzbuzz", ["counter"], (counter) => {
if (counter % 3 === 0 && counter % 5 === 0) {
return htl.html`<b style="color: purple">FizzBuzz</b>`;
} else if (counter % 3 === 0) {
return htl.html`<b style="color: red">Fizz</b>`;
} else if (counter % 5 === 0) {
return htl.html`<b style="color: blue">Buzz</b>`;
} else {
return htl.html`<b>${counter}</b>`;
}
});
</script>
<p>A <code><script></code> declaring a cell can be hidden inside a <code><details></code>s element.</p>
<details id="hue">
<summary>Show code</summary>
<script type="module" class="echo reflect" contenteditable="true">
celine.cell("hue", async function* () {
let hue = 0;
while (true) {
await library.Promises.delay(100);
yield htl.html`<span style="color: hsl(${hue}, 100%, 40%);">
Hello from inside the <details> element
</span>`;
hue = (hue + 10) % 360;
}
});
</script>
</details>
<br />
<p>To display the cell's current value above the <code><details></code> element, rather than above the <code><script></code>, we add <code>id="hue"</code> to the <code><details></code> element, as the cell's <code>name</code> is <code>"hue"</code>:</p>
<script type="module" id="innerhue">
celine.cell("innerhue", () => {
return htl.html`<span class='echo'>${document.getElementById("hue").outerHTML}</span>`;
});
</script>
<h3><a href="#viewof" id="viewof"></a><code>celine.viewof(name, [inputs, ]definition)</code></h3>
<p>The <code>viewof</code> constructor is a special constructor designed to work with <a href="https://github.com/observablehq/inputs">Observable Inputs</a>.</p>
<p><p>It declares 2 reactive cells: a cell called <code>"${name}"</code>, and a cell called <code>"viewof ${name}"</code> - one for the value, and one for the DOM element itself.</p>
<p>To display the DOM element above another element <code><script></code>, set <code>id="viewof ${name}"</code> on the element to which the input should be prepended.</p>
<p>Here, we want to display an input above the <code><script></code> element, so we set <code>id="viewof password"</code> on the <code><script></code>:</p>
<script type="module" class="echo reflect" id="viewof password" contenteditable="true">
celine.viewof("password", () => {
return Inputs.text({ label: "Password", placeholder: "hunter2" });
});
</script>
<p>We still have to depend on the cell called <code>"password"</code> to use the input's value:</p>
<script type="module" class="echo reflect" id="strength" contenteditable="true">
celine.cell("strength", ["password"], (password) => {
// please don't use this for real password strength checking
if (password.length >= 12) {
return htl.html`<b style="color: green">Strong</b>`;
} else if (password.length >= 10) {
return htl.html`<b style="color: lime">Fairly Strong</b>`;
} else if (password.length >= 8) {
return htl.html`<b style="color: orange">Medium</b>`;
} else if (password.length >= 6) {
return htl.html`<b style="color: gold">Weak</b>`;
} else if (password.length >= 4) {
return htl.html`<b style="color: red">Very Weak</b>`;
} else {
return htl.html`<b style="color: darkred">Extremely Weak</b>`;
}
});
</script>
<p>For further information on how to create custom inputs, see the <a href="https://observablehq.com/@observablehq/synchronized-inputs"><cite>Synchronized Inputs</cite></a> guide.</p>
<h3><a href="#silent" id="silent"></a><code>celine.silentCell(name, [inputs, ]definition)</code></h3>
<p>The <code>silentCell</code> constructor declares a cell that doesn't try to display its current value anywhere.</p>
<script type="module" class="echo reflect" id="silent" contenteditable="true">
celine.silentCell("silent", () => {
return "This string does NOT render anywhere, but can be used by other cells.";
});
</script>
<h3><a href="#mutable" id="mutable"></a><code>celine.mutable(name, value)</code> / <code>celine.silentMutable(name, value)</code></h3>
<p>The <code>mutable</code> (and <code>silentMutable</code>) constructor declares a cell <em>and</em> returns a reference that can be mutated. Mutations propagate to cells that depend upon it.</p>
<script type="module" class="echo reflect" id="ref" contenteditable="true">
window.ref = celine.mutable("ref", 3)
</script>
<script type="module" class="echo reflect" id="viewof increment" contenteditable="true">
celine.viewof("increment", () => {
const increment = () => ++ref.value;
const reset = () => ref.value = 0;
return Inputs.button([["Increment", increment], ["Reset", reset]]);
});
</script>
<script type="module" class="echo reflect" id="sword" contenteditable="true">
celine.cell("sword", ["ref"], (ref) => {
return htl.html`↜(╰ •ω•)╯ |${'═'.repeat(ref)}═ﺤ`
});
</script>
<h3><a href="#library" id="library"></a><code>celine.library</code> / Observable standard library</h3>
<p>There are many useful utilities in the <a href="https://github.com/observablehq/stdlib">Observable standard library</a>. Inspect the contents of the <code>celine.library</code> object:</p>
<script type="module" class="echo reflect" id="lib" contenteditable="true">
celine.cell("lib", () => {
return celine.library;
});
</script>
<h4><a href="#tex" id="tex"></a>TeX</h4>
<script type="module" class="echo reflect" id="eq1" contenteditable="true">
celine.cell("eq1", async () => {
const tex = await celine.library.tex();
return tex`c = \pm\sqrt{a^2 + b^2}`;
});
</script>
<h5><a href="#celine_tex" id="#celine_tex"></a><code>celine.tex</code></h5>
<p>Because rendering TeX is so useful, <cite>@celine/celine</cite> provides a shorthand <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals">template literal</a>, <code>celine.tex</code>:</p>
<script type="module" class="echo reflect" id="eq2" contenteditable="true">
celine.cell("eq2", celine.tex`c = \pm\sqrt{a^2 + b^2}`);
</script>
<p>Because cells render their contents <code>display: inline</code> (<i>celine ⇒ <q>cell inline</q></i>), we can embed the script <script type="module" class="echo reflect" id="eq3" contenteditable="true">celine.cell("eq3", celine.tex`c = \pm\sqrt{a^2 + b^2}`)</script> in the middle of the <code><p></code> element.</p>
<p>In non-demonstration use, we'd also leave off the <code>.echo</code> and <code>.reflect</code> classes, to render <script type="module" id="eq4">celine.cell("eq4", celine.tex`c = \pm\sqrt{a^2 + b^2}`)</script> inline.</p>
<p>To render TeX centered, wrap the <code><script></code> with a <code><div style="text-align: center"></code>:</p>
<div style="text-align: center; margin-top: 2ch; margin-bottom: 1ch;">
<script type="module" id="eq5" contenteditable="true">
celine.cell("eq5", celine.tex`c = \pm\sqrt{a^2 + b^2}`);
</script>
</div>
<p>Both <code>tex</code> template literals are unconfigurable. You will need to import the <a href="https://katex.org">KaTeX</a> library proper if you'd like to modify any of its <a href="https://katex.org/docs/options">options</a>.</p>
<h4><a href="#markdown" id="markdown"></a>Markdown</h4>
<script type="module" class="echo reflect" id="md" contenteditable="true">
celine.cell("md", async () => {
const elements = [
{ "symbol": "Co", "name": "Cobalt", "number": 27 },
{ "symbol": "Cu", "name": "Copper", "number": 29 },
{ "symbol": "Sn", "name": "Tin", "number": 50 },
{ "symbol": "Pb", "name": "Lead", "number": 82 }
];
const md = await celine.library.md();
return md`
| Name | Symbol | Atomic number |
|-----------|-------------|---------------|${elements.map(e => `
| ${e.name} | ${e.symbol} | ${e.number} |`)}
`;
});
</script>
<h5><a href="#celine_md" id="celine_md"></a><code>celine.md</code></h5>
<p>Markdown also has a shorthand <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals">template literal</a>, <code>celine.md</code>:</p>
<script type="module" class="echo reflect" id="md2" contenteditable="true">
celine.cell("md2", celine.md`
1. I
2. love
3. simple
4. lists
`);
</script>
<h4><a href="#graphviz" id="graphviz"></a>Graphviz</h4>
<p>
<a href="https://observablehq.com/framework/lib/dot">Docs</a>
</p>
<script type="module" class="echo reflect" id="dot" contenteditable="true">
celine.cell("dot", async () => {
const dot = await celine.library.dot();
return dot`
digraph G {
rankdir = LR
a -> b -> c
}`;
});
</script>
<h4><a href="#mermaid" id="mermaid"></a>Mermaid</h4>
<p>
<a href="https://observablehq.com/framework/lib/mermaid">Docs</a>
</p>
<script type="module" class="echo reflect" id="cmermaid" contenteditable="true">
celine.cell("cmermaid", async () => {
const mermaid = await celine.library.mermaid();
return mermaid`
graph TD;
A-->B;
A-->C;
B-->D;
C-->D;
`;
});
</script>
<h4><a href="#leaflet" id="leaflet"></a>Leaflet</h4>
<p>
<a href="https://observablehq.com/framework/lib/leaflet">Docs</a>
</p>
<script type="module" class="echo reflect" id="L" contenteditable="true">
celine.cell("L", async function* () {
const L = await celine.library.L();
const container = htl.html`<div id="map" style="height: 300px;"></div>`;
yield container;
const map = L.map(container).setView([-33.8688, 151.2093], 13);
L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
attribution: '© <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'
}).addTo(map);
});
</script>
<h4><a href="#duckdb" id="duckdb"></a>DuckDB</h4>
<p>
<a href="https://observablehq.com/framework/lib/duckdb">Docs</a>
</p>
<script type="module" class="echo reflect" id="duck" contenteditable="true">
import * as Plot from 'https://esm.run/@observablehq/[email protected]';
celine.cell("duck", async () => {
const db = await celine.library.DuckDBClient().of();
await db.sql`
CREATE TABLE gaia
AS SELECT *
FROM read_parquet('https://maxbo.me/celine/static/gaia-sample.parquet')`;
const bins = await db.sql`
SELECT
floor(ra / 2) * 2 + 1 AS ra,
floor(dec / 2) * 2 + 1 AS dec,
count() AS count
FROM gaia
GROUP BY 1, 2`
return Plot.plot({
aspectRatio: 1,
caption: htl.html`<p>Source: <a href="https://observablehq.com/@cmudig/peeking-into-the-gaia-star-catalog">Peeking into the Gaia Star Catalog</a></p>`,
x: {domain: [0, 360]},
y: {domain: [-90, 90]},
marks: [
Plot.frame({fill: 0}),
Plot.raster(bins, {
x: "ra",
y: "dec",
fill: "count",
width: 360 / 2,
height: 180 / 2,
imageRendering: "pixelated"
})
]
})
});
</script>
<h2><a href="#1st-party-pairings" id="1st-party-pairings"></a>1st-party library pairings</h2>
<!-- <h3><a href="#dfn" id="dfn"><dfn>: The Definition element</h3>
<p>
HTML has a little known element, <a href="https://developer.mozilla.org/en-US/docs/Web/HTML/Element/dfn"><dfn></a>, that can be used to define terms.
</p> -->
<h3><a href="#celine-libertine" id="celine-libertine"></a>@celine/libertine</h3>
<p><cite>@celine/libertine</cite> provides a stylesheet based around the <a href="https://en.wikipedia.org/wiki/Linux_Libertine">Linux Libertine</a> typeface, one common in academic typesetting.</p>
<p>
Information about it lives on a subpage, <a href="./libertine/index.html">/libertine</a>.
</p>
<h3><a href="#celine-bibhtml" id="celine-bibhtml"></a>@celine/bibhtml</h3>
<p>
<cite>@celine/bibhtml</cite> is a <a href="https://developer.mozilla.org/en-US/docs/Web/API/Web_components">Web
Components</a>-based referencing system for HTML documents.
</p>
<p>
Information about it lives on a subpage, <a href="./bibhtml/index.html">/bibhtml</a>.
</p>
<h2><a href="#3rd-party-library-pairings" id="#3rd-party-library-pairings"></a>3rd-party library pairings</h2>
<p>Some libraries that pair well with <cite>@celine/celine</cite> are:</p>
<h3><a id="pyodide" href="#pyodide"></a>Pyodide</h3>
<p><a href="https://pyodide.org/">Pyodide</a> is a port of CPython to WebAssembly.</p>
<script type="module" class="echo reflect" id="mean" contenteditable="true">
import { loadPyodide } from 'https://cdn.jsdelivr.net/pyodide/v0.26.3/full/pyodide.mjs';
celine.cell("mean", async () => {
const pyodide = await loadPyodide();
await pyodide.loadPackage(['numpy']);
return pyodide.runPython(`
import numpy as np
x = np.random.randn(1000)
x.mean()
`);
});
</script>
<h3><a id="webr" href="#webr"></a>WebR</h3>
<p><a href="https://docs.r-wasm.org/webr/latest/">WebR</a> is a version of the statistical language R compiled for the browser using WebAssembly, via Emscripten.</p>
<script type="module" class="echo reflect" id="summary" contenteditable="true">
import { WebR } from 'https://webr.r-wasm.org/latest/webr.mjs';
celine.cell("summary", async () => {
const webR = new WebR();
await webR.init();
const shelter = await new webR.Shelter();
const capture = await shelter.captureR(`
fit <- lm(mpg ~ am, data=mtcars)
print(summary(fit))`);
return capture.output.map(d => d.data).join('\n');
});
</script>
<h3><a id="penrose" href="#penrose"></a>Penrose</h3>
<p><a href="https://penrose.cs.cmu.edu/">Penrose</a>, a system for creating beautiful diagrams just by typing notation in plain text.</p>
<p>Using the <a href="https://penrose.cs.cmu.edu/docs/ref/vanilla-js"><cite>Using Penrose with Vanilla JS</cite></a> instructions:</p>
<details id="sets">
<summary>Show code</summary>
<script type="module" class="echo reflect" contenteditable="true">
import { compile, optimize, showError, toSVG } from "https://ga.jspm.io/npm:@penrose/[email protected]/dist/bundle/index.js"
const trio = {
substance: `
Set A, B, C, D, E, F, G
Subset(B, A)
Subset(C, A)
Subset(D, B)
Subset(E, B)
Subset(F, C)
Subset(G, C)
Disjoint(E, D)
Disjoint(F, G)
Disjoint(B, C)
AutoLabel All
`,
style: `
canvas {
width = 800
height = 300
}
forall Set x {
shape x.icon = Circle { }
shape x.text = Equation {
string : x.label
fontSize : "32px"
}
ensure contains(x.icon, x.text)
encourage norm(x.text.center - x.icon.center) == 0
layer x.text above x.icon
}
forall Set x; Set y
where Subset(x, y) {
ensure disjoint(y.text, x.icon, 10)
ensure contains(y.icon, x.icon, 5)
layer x.icon above y.icon
}
forall Set x; Set y
where Disjoint(x, y) {
ensure disjoint(x.icon, y.icon)
}
forall Set x; Set y
where Intersecting(x, y) {
ensure overlapping(x.icon, y.icon)
ensure disjoint(y.text, x.icon)
ensure disjoint(x.text, y.icon)
}
`,
domain: `
type Set
predicate Disjoint(Set s1, Set s2)
predicate Intersecting(Set s1, Set s2)
predicate Subset(Set s1, Set s2)`,
variation: `test`,
};
celine.cell("sets", async () => {
await celine.library.Promises.delay(1000); // weirdly necessary
const compiled = await compile(trio);
if (compiled.isErr()) {
return showError(compiled.error);
}
const optimized = optimize(compiled.value);
if (optimized.isErr()) {
return showError(optimized.error);
}
return toSVG(optimized.value);
});
</script>
</details>
<p>
<a href="https://penrose.cs.cmu.edu/docs/bloom/tutorial/getting_started">Bloom</a> lets you build optimization-driven interactive diagrams in JavaScript.
</p>
<p>
<i>Try dragging the circles around!</i>
</p>
<details id="bloom">
<summary>Show code</summary>
<script type="module" class="echo reflect" contenteditable="true">
import * as bloom from "https://penrose.cs.cmu.edu/bloom.min.js";
const CANVAS_WIDTH = 400;
const CANVAS_HEIGHT = 200;
const db = new bloom.DiagramBuilder(bloom.canvas(CANVAS_WIDTH, CANVAS_HEIGHT), "abcd", 1);
const { type, predicate, forall, forallWhere, ensure, circle, line } = db;
const pointRad = 30;
const pointMargin = 10;
const Point = type();
const Arrow = type();
const Connects = predicate();
const p1 = Point();
const p2 = Point();
const arrow = Arrow();
Connects(arrow, p1, p2);
forall({ p: Point }, ({ p }) => {
p.icon = circle({
r: pointRad,
drag: true,
});
});
forallWhere(
{ a: Arrow, p: Point, q: Point },
({ a, p, q }) => Connects.test(a, p, q),
({ a, p, q }) => {
const pq = bloom.ops.vsub(q.icon.center, p.icon.center); // vector from p to q
const pqNorm = bloom.ops.vnormalize(pq); // direction from p to q
const pStart = bloom.ops.vmul(pointRad + pointMargin, pqNorm); // vector from p to line start
const start = bloom.ops.vadd(p.icon.center, pStart); // line start
const end = bloom.ops.vsub(q.icon.center, pStart); // line end
a.icon = line({
start: start,
end: end,
endArrowhead: "straight",
});
ensure(
bloom.constraints.greaterThan(
bloom.ops.vdist(p.icon.center, q.icon.center),
2 * (pointRad + pointMargin) + 20,
),
);
},
);
celine.cell("bloom", async () => {
const diagram = await db.build();
return htl.html`
<div style='height: ${CANVAS_HEIGHT};'>
${diagram.getInteractiveElement()}
</div>`
});
</script>
</details>
<h2><a href="#saving" id="saving"></a>Saving modified notebooks</h2>
<p>
If you make changes to <code>contenteditable</code> elements, you are able to save the notebook, with changes, as a new file.
</p>
<script id="viewof save" type="module" class="echo reflect" contenteditable="true">
celine.viewof("save", () => {
return Inputs.button("Save", {
reduce: async () => {
const filename = prompt("Enter a filename", "index.html");
const content = document.documentElement.outerHTML;
const blob = new Blob([content], { type: 'text/html' });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = filename;
a.click();
}
});
});
</script>
<h2><a href="#new-cell" id="new-script"></a>Creating new cells</h2>
<p>
You can allow the user to create new cells:
</p>
<script type="module" class="echo reflect" id="viewof new-cell" contenteditable="true">
celine.viewof("new-cell", () => {
return Inputs.button("New cell", {
value: 0,
reduce: (value) => {
const script = document.createElement('script');
script.id = `new-cell-${value}`;
script.type = 'module';
script.className = 'echo reflect';
script.contentEditable = 'true';
script.textContent = `
celine.cell("new-cell-${value}", () => {
return "Hello from new script!";
});
`;
(document.getElementById(`new-cell-${value - 1}`) || document.getElementById('viewof new-cell'))
.insertAdjacentElement('afterend', script);
return value + 1;
}
});
});
</script>
<!-- <h2><a href="#introspection" id="introspection"></a>Introspection</h2>
<p>
Each instance of a <code>CelineModule</code> exposes a <code>module</code> field, which is an instance of an <a href="https://github.com/observablehq/runtime?tab=readme-ov-file#modules">Observable <code>Runtime.Module</code></a>.
</p>
<script type="module" class="echo reflect" id="module" contenteditable="true">
celine.cell("module", () => {
return celine.module;
});
</script> -->
<h2><a href="#changelog" id="changelog"></a>changelog.xml <a href="celine/changelog.xml"><img src="static/rss.svg" alt="RSS feed icon" style="width: 1ch; height: 1ch; vertical-align: middle; display: inline-block;"></a></h2>
<p>
<cite>@celine/celine</cite> uses <a href="https://semver.org/">Semantic Versioning 2.0.0</a>.
</p>
<p><i>Showing 10 most recent entries.</i></p>
<table>
<thead>
<tr>
<th>Version</th>
<th>Date</th>
<th>Changes</th>
</tr>
</thead>
<tbody id="changelog-table-body">
<!-- rows get inserted here-->
</tbody>
<script type="module">
fetch('celine/changelog.xml')
.then(response => response.text())
.then(data => {
const parser = new DOMParser();
const xmlDoc = parser.parseFromString(data, 'application/xml');
const entries = Array.from(xmlDoc.getElementsByTagName('entry')).slice(0, 10);
let tableRows = '';
for (let entry of entries) {
const version = entry.getElementsByTagName('title')[0].textContent;
const link = entry.getElementsByTagName('link')[0].getAttribute('href');
const changes = entry.getElementsByTagName('summary')[0].textContent;
let date = entry.getElementsByTagName('updated')[0].textContent;
date = new Date(date).toLocaleDateString('en-US', { year: 'numeric', month: 'short', day: 'numeric' });
tableRows += `<tr><td><a href="${link}">${version}</a></td><td>${date}</td><td>${changes}</td></tr>`;
}
document.getElementById('changelog-table-body').innerHTML = tableRows;
})
.catch(error => console.error('Error loading changelog:', error));
</script>
</table>
<h2><a href="#resources" id="resources"></a>Resources</h2>
<ul>
<li><a href="https://github.com/observablehq/runtime">Observable Runtime</a></li>
<li><a href="https://github.com/observablehq/inputs">Observable Inputs</a></li>
<li><a href="https://github.com/observablehq/stdlib">Observable standard library</a></li>
<li><a href="https://observablehq.com/@observablehq/how-observable-runs"><cite>How Observable Runs</cite></a></li>
<li><a href="https://observablehq.com/@observablehq/synchronized-inputs"><cite>Synchronized Inputs</cite></a></li>
<li><a href="https://observablehq.com/@observablehq/module-require-debugger">Module require debugger</a></li>
<li><a href="https://observablehq.com/plot/what-is-plot">Observable Plot</a></li>
<li><a href="https://maxbo.me/a-html-file-is-all-you-need.html"><cite>Reactive HTML Notebooks</cite></a></li>
</ul>
<!-- <div style="text-align: center; margin-top: 4ch;">
<pre style="max-width: 100%; overflow-x: auto;">
_..._
.-'_..._''. .---.
.' .' '.\ __.....__ | |.--. _..._ __.....__
/ .' .-'' '. | ||__| .' '. .-'' '.
. ' / .-''"'-. `. | |.--.. .-. . / .-''"'-. `.
| | / /________\ \| || || ' ' |/ /________\ \
| | | || || || | | || |
. ' \ .-------------'| || || | | |\ .-------------'
\ '. .\ '-.____...---.| || || | | | \ '-.____...---.
'. `._____.-'/ `. .' | ||__|| | | | `. .'
`-.______ / `''-...... -' '---' | | | | `''-...... -'
` | | | |
'--' '--'
</pre>
</div> -->
</main>
</body>
</html>