Media and drawing
Text
Media and drawing
text.md

Text

Examplea whole program
state text = "Ship 2 doors to 90210 by 2026-11-15"
derive numbers = re.all(text, r"\d+").map(m => m.text)
derive date = re.replace(text, r"(\d+)-(\d+)-(\d+)", "$3/$2/$1")
column gap=8 padding=16
input value=text
text `numbers: ${numbers.join(", ")}`
text date color=dim

Mental model#

Patterns#

Examplea whole program
shape Tok
kind: string
text: string
state source = "total = price * 1.0825 + 'tax'"
fn tokens(s: string): Tok[]
let out: Tok[] = []
let rules = [{kind: "space", p: r"\s+"}, {kind: "number", p: r"\d+(\.\d+)?"}, {kind: "word", p: r"[A-Za-z_]\w*"}, {kind: "string", p: r"'[^']*'"}, {kind: "mark", p: r"."}]
let i = 0
while i < s.length
let got = false
for r in rules
if !got
let m = re.at(s, r.p, i)
if m
if r.kind != "space" out.push({kind: r.kind, text: m.text})
i = m.end
got = true
if !got i += 1
return out
column gap=8 padding=16
input value=source
row gap=6 flex-wrap=wrap
for t in tokens(source)
text t.text color=(t.kind == "number" ? "accent" : (t.kind == "string" ? "ok" : "fg"))
Examplea whole program
state text = "Ship 2 doors on 2026-11-27, 3 more on 2026-12-01"
state typed = r"\d+"
derive dates = re.all(text, r"(?<year>\d{4})-(?<month>\d\d)-(?<day>\d\d)").map(m => `${m.named.day}.${m.named.month}.${m.named.year}`)
derive louder = re.replace(text, r"\d+", m => `[${m.text}]`)
derive why = re.check(typed) ?? ""
column gap=8 padding=16
text dates.join(", ")
text louder
input value=typed
text (why == "" ? `${re.all(text, typed).length} matches` : why) color=dim

search#

Examplea whole program
shape Doc
id: string
text: string
state docs: Doc[] = [{id: "a", text: "red oak door"}, {id: "b", text: "white oak cabinet door"}, {id: "c", text: "maple drawer"}]
state query = "oak door"
state index: bytes? = none
state hits: {id: string, score: number}[] = []
action build
index = search.index(docs)?
action find
if index == none return
hits = search.find(index, query, {limit: 5, prefix: true})?
task
build()
column gap=8 padding=16
input value=query input={find()} placeholder="Search"
for h in hits key=h.id
text `${h.id}: ${h.score.toFixed(3)}`
Examplea whole program
state text = "Re: the oak door for March"
state query = "oak doo"
resource marks: {from: number, to: number}[] = search.marks(text, query, {prefix: true}) else []
column gap=4 padding=16
input value=query
text (marks.map(m => text.slice(m.from, m.to)).join(", "))
Examplea whole program
state s: searcher? = none
state hits: {id: string, score: number}[] = []
state query = "invoice march"
// Sealed segments, opened in a worker: the key and the bytes are handed over, and the
// segments come back to be loaded.
off fn unseal(key: bytes, sealed: bytes[]): bytes[]
let out: bytes[] = []
for x in sealed
out.push(crypto.aes.open(key, x.slice(0, 12), x.slice(12))?)
return out
action load(key: bytes, sealed: bytes[])
s = search.open(unseal(key, sealed)?)?
action arrived(mail: {id: string, subject: string, body: string}[])
let open = s
if open == none return
search.add(open, search.index(mail.map(m => ({id: m.id, fields: {subject: m.subject, body: m.body}})))?)?
action find
let open = s
if open == none return
hits = search.find(open, query, {limit: 20, prefix: true, weights: {subject: 2}})?
input value=query input={find()}

syntax#

Code#

Examplea whole program
state code = "function area(w, h) {\n return mul(w, h)\n}\nclass Shop {}\n"
state outline: string[] = []
fn names(n: json, depth: number): string[]
let out: string[] = []
for c in n.children
if c.kind == "declaration"
out.push(`${" ".repeat(depth)}${c.what} ${c.name}`)
for x in names(c, depth + 1)
out.push(x)
else if c.children.length > 0
for x in names(c, depth)
out.push(x)
return out
action read
let tree = syntax.parse(code, "js")?
outline = names(tree, 0)
task
read()
column gap=8 padding=16
for l in outline
text l white-space=pre font-family=monospace

A language of your own#

Examplea whole program
state source = "1 + 2 * (3 - 4)"
state out = ""
fn calc(): string = "expr = left:expr op:(\"+\" | \"-\") right:term | term\nterm = left:term op:(\"*\" | \"/\") right:atom | atom\natom = number | \"(\" expr \")\"\ntoken number @number = [0-9]+ (\".\" [0-9]+)?\nskip = [ \\t\\n]+\n"
fn worth(n: json): number
if n.kind == "number"
return Number(n.text)
if n.kind == "atom"
return worth(n.children.length == 1 ? n.children[0] : n.children[1])
if n.children.length == 1
return worth(n.children[0])
let a = worth(n.children[0])
let b = worth(n.children[2])
let op = n.children[1].text
return op == "+" ? a + b : op == "-" ? a - b : op == "*" ? a * b : a / b
action run
let g = syntax.grammar(calc())?
let tree = syntax.parse(source, g)?
out = `${worth(tree.children[0])}`
column gap=8 padding=16
input value=source
button "Work it out" press=run
text out

Markdown#

HTML#

Examplea whole program
state letter = "<p style='color:#246'>Hi <b>Ada</b>, <a href='https://example.com/'>the plans</a> are ready.<img src='https://pixel.example/o.gif' width=1 height=1></p>"
state report = ""
state opened = ""
state snippet = ""
action cleaned(r: {html: string, images: string[], links: string[], removed: string[], trackers: number})
report = `${r.images.length} remote pictures left out, ${r.trackers} to learn it was read`
action asText
snippet = html.text(letter) else ""
column gap=8 padding=16
htmlframe html=letter images="block" link={opened = it} cleaned=cleaned
text report color=dim
text opened
button "As text" press=asText
text snippet

html.clean#

Examplea whole program
state raw = "<p onclick='x()'>Hello <img src='cid:logo@shop'></p>"
state safe = ""
action go
let c = html.clean(raw, {images: "proxy", proxy: "/img?u={url}", cid: {"logo@shop": "data:image/png;base64,iVBORw0KGgo="}})?
safe = c.html
column gap=8 padding=16
button "Clean" press=go
text safe

html.text#

htmlframe#

In a worker and on the back end#

font#

Examplea whole program
state face: font? = none
state words = "Waffle"
state shapes: number[][][] = []
state wide = 0
state said = ""
action open
let file = fs.pick(".ttf,.otf,.woff")?
if file == none return
let f = font.open(file.data)?
face = f
said = `${f.family} ${f.style}: ${f.glyphs} glyphs`
font.use(f, "Picked")?
set()
action set
let f = face
if f == none return
let line = font.layout(f, words, {size: 96})?
wide = line.width
let glyphs = font.outline(f, words, {size: 96, x: 16, y: 120})?
shapes = glyphs.flatMap(g => g.contours)
watch words
set()
fn show(g: pen, size: {width: number, height: number, time: number})
draw.clear(g, "card")
draw.line(g, 16, 120, 16 + wide, 120, {color: "line"})
draw.path(g, shapes, {fill: "fg"})
column gap=8 padding=16
row gap=8
button "Open a font" press=open
input value=words
surface draw=show width=100% height=160
text said font-family="Picked" font-size=24
text (failure(open) ?? failure(set) ?? "") color=danger

Highlight, CodeBlock and Markdown#

Examplea whole program
state code = "const n: number = 1\nconst m = n + 1\n"
state answer = "# Hello\n\nSome **bold** text and a [link](https://example.com).\n"
state busy = false
column gap=16 padding=16
Highlight text=code lang="ts" lines start=12 marks=[{line: 13, kind: "added"}]
CodeBlock code=code language="ts" title="a.ts"
Markdown text=answer streaming=busy

The editor#

Pitfalls#

Where a new call goes#

In the source#

esc
1072 sections of 111 pages, ranked as you type.A heading's words count for more than what stands under it.
↑↓move
↵open
ranked by BM25, in the page