@tempoxyz/ds 0.3.16 → 0.3.18

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 (47) hide show
  1. package/dist/docs/AGENTS.md +4 -2
  2. package/dist/docs/marketing.md +699 -0
  3. package/dist/docs/product/documents.md +248 -0
  4. package/dist/docs/product/foundations.md +1 -1
  5. package/dist/document/Document.d.ts +46 -0
  6. package/dist/document/Document.d.ts.map +1 -0
  7. package/dist/document/Document.js +190 -0
  8. package/dist/document/Document.js.map +1 -0
  9. package/dist/document/Enhance.d.ts +13 -0
  10. package/dist/document/Enhance.d.ts.map +1 -0
  11. package/dist/document/Enhance.js +511 -0
  12. package/dist/document/Enhance.js.map +1 -0
  13. package/dist/document/bin.d.ts +3 -0
  14. package/dist/document/bin.d.ts.map +1 -0
  15. package/dist/document/bin.js +38 -0
  16. package/dist/document/bin.js.map +1 -0
  17. package/dist/document/document.css +388 -0
  18. package/dist/document/index.d.ts +3 -0
  19. package/dist/document/index.d.ts.map +1 -0
  20. package/dist/document/index.js +3 -0
  21. package/dist/document/index.js.map +1 -0
  22. package/dist/document/internal/Cli.d.ts +43 -0
  23. package/dist/document/internal/Cli.d.ts.map +1 -0
  24. package/dist/document/internal/Cli.js +87 -0
  25. package/dist/document/internal/Cli.js.map +1 -0
  26. package/dist/document/internal/Template.d.ts +21 -0
  27. package/dist/document/internal/Template.d.ts.map +1 -0
  28. package/dist/document/internal/Template.js +15 -0
  29. package/dist/document/internal/Template.js.map +1 -0
  30. package/dist/document/script.d.ts +2 -0
  31. package/dist/document/script.d.ts.map +1 -0
  32. package/dist/document/script.js +452 -0
  33. package/dist/document/templates/brief.html +96 -0
  34. package/dist/document/templates/essay.html +70 -0
  35. package/dist/document/templates/post.html +69 -0
  36. package/package.json +19 -1
  37. package/src/document/Document.ts +277 -0
  38. package/src/document/Enhance.ts +532 -0
  39. package/src/document/bin.ts +38 -0
  40. package/src/document/document.css +388 -0
  41. package/src/document/index.ts +2 -0
  42. package/src/document/internal/Cli.ts +132 -0
  43. package/src/document/internal/Template.ts +25 -0
  44. package/src/document/script.ts +8 -0
  45. package/src/document/templates/brief.html +96 -0
  46. package/src/document/templates/essay.html +70 -0
  47. package/src/document/templates/post.html +69 -0
@@ -0,0 +1,532 @@
1
+ /**
2
+ * Progressive enhancement for document mode: footnote popovers and the list of notes, heading
3
+ * copy-link anchors, copy buttons on code blocks, Essay's reading rail, Brief's contents highlight,
4
+ * and Axis placement. A document reads in full without it. @module
5
+ */
6
+
7
+ // The Link icon from src/web/icons/svg/Link.svg, filled with currentColor as the React icon is. Inlined
8
+ // so the script has no imports to resolve.
9
+ const link =
10
+ '<svg viewBox="0 0 16 16" fill="none" aria-hidden="true">' +
11
+ '<path d="M3.81309 6.81315C4.00834 6.61819 4.32494 6.61809 4.52012 6.81315C4.71529 7.00833 4.71513 7.32491 4.52012 7.52018L2.77501 9.26628C2.63738 9.40389 2.55452 9.48662 2.49864 9.55241C2.44691 9.61342 2.44701 9.62653 2.45079 9.61491C2.43994 9.64829 2.43997 9.68504 2.45079 9.71843C2.45504 9.72574 2.46795 9.74473 2.49864 9.78093C2.55453 9.84677 2.63719 9.92925 2.77501 10.0671L5.93321 13.2253C6.07102 13.3631 6.15351 13.4448 6.21934 13.5007C6.25519 13.5311 6.27422 13.5441 6.28184 13.5485C6.31522 13.5593 6.35199 13.5603 6.38536 13.5495C6.39329 13.5448 6.41267 13.5305 6.44786 13.5007C6.51365 13.4448 6.5964 13.3629 6.73399 13.2253L8.48008 11.4801C8.67537 11.2851 8.99194 11.285 9.18712 11.4801C9.38206 11.6753 9.38205 11.992 9.18712 12.1872L7.44102 13.9323C7.31499 14.0583 7.19946 14.1749 7.09532 14.2633C7.01381 14.3325 6.92065 14.4014 6.81016 14.4538L6.69395 14.4997C6.45972 14.5758 6.2075 14.5757 5.97325 14.4997C5.80931 14.4465 5.68052 14.3556 5.57188 14.2633C5.4677 14.1749 5.35229 14.0584 5.22618 13.9323L2.06798 10.7741C1.94181 10.6479 1.82539 10.5326 1.73692 10.4284C1.64466 10.3197 1.55286 10.1909 1.49962 10.027C1.42365 9.79294 1.42362 9.54039 1.49962 9.30632C1.55287 9.14262 1.64472 9.01358 1.73692 8.90495C1.82537 8.8008 1.94192 8.6853 2.06798 8.55925L3.81309 6.81315Z" fill="currentColor"/>' +
12
+ '<path d="M9.6461 5.64616C9.84126 5.451 10.1578 5.4512 10.3531 5.64616C10.5484 5.84142 10.5484 6.15793 10.3531 6.35319L6.35313 10.3532C6.15787 10.5485 5.84136 10.5485 5.6461 10.3532C5.45114 10.1579 5.45094 9.84132 5.6461 9.64616L9.6461 5.64616Z" fill="currentColor"/>' +
13
+ '<path d="M9.30626 1.49968C9.54033 1.42368 9.79288 1.42371 10.027 1.49968C10.1908 1.55292 10.3196 1.64472 10.4283 1.73698C10.5325 1.82546 10.6479 1.94187 10.774 2.06804L13.9322 5.22624C14.0583 5.35235 14.1748 5.46776 14.2633 5.57194C14.3555 5.68058 14.4464 5.80937 14.4996 5.97331C14.5566 6.14891 14.5711 6.33463 14.5426 6.5153L14.4996 6.69401C14.4464 6.85774 14.3554 6.98682 14.2633 7.09538C14.1748 7.19952 14.0583 7.31506 13.9322 7.44108L12.1871 9.18718C11.9919 9.38211 11.6753 9.38213 11.4801 9.18718C11.2849 8.992 11.2851 8.67543 11.4801 8.48015L13.2252 6.73405C13.3628 6.59646 13.4447 6.51371 13.5006 6.44792C13.5305 6.41273 13.5438 6.39335 13.5484 6.38542C13.5593 6.35205 13.5602 6.31528 13.5494 6.2819C13.5451 6.27428 13.531 6.25525 13.5006 6.2194C13.4447 6.15357 13.363 6.07108 13.2252 5.93327L10.067 2.77507C9.92919 2.63725 9.84671 2.55459 9.78087 2.4987C9.74467 2.46801 9.72568 2.45511 9.71837 2.45085C9.68498 2.44003 9.64823 2.44 9.61485 2.45085C9.62646 2.44707 9.61336 2.44697 9.55235 2.4987C9.48656 2.55458 9.40383 2.63744 9.26622 2.77507L7.52012 4.52018C7.32485 4.71519 7.00827 4.71535 6.81309 4.52018C6.61803 4.325 6.61813 4.0084 6.81309 3.81315L8.55919 2.06804C8.68524 1.94198 8.80074 1.82543 8.90489 1.73698C9.01352 1.64478 9.14256 1.55293 9.30626 1.49968Z" fill="currentColor"/>' +
14
+ '</svg>'
15
+
16
+ /** How long a copied state shows before it reverts, in milliseconds. */
17
+ const settle = 1500
18
+
19
+ /**
20
+ * Enhances every document on a page. Safe to call once per page, after the document is parsed.
21
+ *
22
+ * @example
23
+ * Enhance.run(document)
24
+ */
25
+ export function run(page: Document): void {
26
+ const view = page.defaultView
27
+ if (!view) return
28
+
29
+ page.documentElement.classList.add('js')
30
+ const live = page.createElement('div')
31
+ live.className = 'sr-only'
32
+ live.setAttribute('aria-live', 'polite')
33
+ page.body.append(live)
34
+ const announce = (message: string) => {
35
+ live.textContent = message
36
+ }
37
+
38
+ for (const doc of page.querySelectorAll<HTMLElement>('.doc')) {
39
+ // The summary of a Brief is a sibling of its article, and its notes join the article's notes.
40
+ const summary = doc.closest('.brief')?.querySelector<HTMLElement>(':scope > .summary')
41
+ notes(doc, view, summary ?? undefined)
42
+ anchors(doc, view, announce)
43
+ code(doc, view, announce)
44
+ if (doc.hasAttribute('data-rail')) rail(doc, view)
45
+ }
46
+ for (const nav of page.querySelectorAll<HTMLElement>('.summary nav')) contents(nav, view)
47
+ for (const axis of page.querySelectorAll<HTMLOListElement>('ol.axis')) place(axis, view)
48
+ }
49
+
50
+ /** Adds a copy-link anchor to each H2 and H3 that has an id, or gets one from its text. */
51
+ function anchors(doc: HTMLElement, view: Window, announce: (message: string) => void) {
52
+ const page = doc.ownerDocument
53
+
54
+ for (const heading of doc.querySelectorAll<HTMLHeadingElement>(':scope > h2, :scope > h3')) {
55
+ if (heading.querySelector('.h-anchor')) continue
56
+ const name = (heading.textContent ?? '').trim()
57
+ heading.dataset['name'] = name
58
+ if (!heading.id) {
59
+ const base = slug(name) || 'section'
60
+ let id = base
61
+ for (let n = 2; page.getElementById(id); n++) id = `${base}-${n}`
62
+ heading.id = id
63
+ }
64
+
65
+ const anchor = page.createElement('a')
66
+ anchor.className = 'h-anchor'
67
+ anchor.href = '#' + heading.id
68
+ anchor.setAttribute('aria-label', 'Copy link to ' + name)
69
+ anchor.innerHTML = link + '<span class="tip" aria-hidden="true">Copy link</span>'
70
+
71
+ // The last word and the anchor never wrap apart.
72
+ const last = heading.lastChild
73
+ const match = last?.nodeType === 3 ? /^([\s\S]*?)(\S+)\s*$/.exec(last.textContent ?? '') : null
74
+ if (last && match) {
75
+ last.textContent = match[1] ?? ''
76
+ const tail = page.createElement('span')
77
+ tail.className = 'h-tail'
78
+ tail.append(match[2] ?? '', anchor)
79
+ heading.append(tail)
80
+ } else heading.append(anchor)
81
+
82
+ let timer: ReturnType<typeof setTimeout> | undefined
83
+ const tip = anchor.querySelector<HTMLElement>('.tip')
84
+ anchor.addEventListener('click', (event) => {
85
+ const clipboard = view.navigator.clipboard
86
+ if (!clipboard) return
87
+ event.preventDefault()
88
+ const url = view.location.href.split('#')[0] + '#' + heading.id
89
+ clipboard.writeText(url).then(
90
+ () => {
91
+ view.history.replaceState(null, '', '#' + heading.id)
92
+ anchor.dataset['copied'] = ''
93
+ if (tip) tip.textContent = 'Link copied'
94
+ announce('Link to ' + name + ' copied')
95
+ clearTimeout(timer)
96
+ timer = setTimeout(() => {
97
+ delete anchor.dataset['copied']
98
+ if (tip) tip.textContent = 'Copy link'
99
+ announce('')
100
+ }, settle)
101
+ },
102
+ () => {
103
+ view.location.hash = heading.id
104
+ },
105
+ )
106
+ })
107
+ }
108
+ }
109
+
110
+ /** Wraps each code block with a copy button. */
111
+ function code(doc: HTMLElement, view: Window, announce: (message: string) => void) {
112
+ const page = doc.ownerDocument
113
+
114
+ for (const pre of doc.querySelectorAll<HTMLPreElement>(':scope > pre')) {
115
+ const wrap = page.createElement('div')
116
+ wrap.className = [
117
+ 'code-block',
118
+ ...[...pre.classList].filter((name) => name.startsWith('l-')),
119
+ ].join(' ')
120
+ pre.replaceWith(wrap)
121
+ wrap.append(pre)
122
+
123
+ const button = page.createElement('button')
124
+ button.type = 'button'
125
+ button.className = 'copy'
126
+ button.textContent = 'Copy'
127
+ button.setAttribute('aria-label', 'Copy code')
128
+ wrap.append(button)
129
+
130
+ let timer: ReturnType<typeof setTimeout> | undefined
131
+ button.addEventListener('click', async () => {
132
+ const text = (pre.querySelector('code') ?? pre).textContent ?? ''
133
+ try {
134
+ await view.navigator.clipboard.writeText(text)
135
+ } catch {
136
+ return
137
+ }
138
+ button.textContent = 'Copied'
139
+ button.dataset['copied'] = ''
140
+ announce('Copied')
141
+ clearTimeout(timer)
142
+ timer = setTimeout(() => {
143
+ button.textContent = 'Copy'
144
+ delete button.dataset['copied']
145
+ announce('')
146
+ }, settle)
147
+ })
148
+ }
149
+ }
150
+
151
+ /**
152
+ * Marks the Brief contents link of the section in view: the last one whose heading reached the
153
+ * line where a followed link puts it. A followed link marks its own section while that heading
154
+ * stays on screen, so a section too short to reach the line is marked as well.
155
+ */
156
+ function contents(nav: HTMLElement, view: Window) {
157
+ const page = nav.ownerDocument
158
+ const links = [...nav.querySelectorAll<HTMLAnchorElement>('a[href^="#"]')]
159
+ const targets = links.flatMap((anchor) => {
160
+ const target = page.getElementById((anchor.getAttribute('href') ?? '').slice(1))
161
+ return target ? [target] : []
162
+ })
163
+ if (targets.length === 0) return
164
+
165
+ const root = page.documentElement
166
+ // The address holds a fragment percent-encoded, and an id holds it as written.
167
+ const fragment = () =>
168
+ (() => {
169
+ const hash = view.location.hash.slice(1)
170
+ try {
171
+ return decodeURIComponent(hash)
172
+ } catch {
173
+ return hash
174
+ }
175
+ })()
176
+
177
+ type Choice = { arrived: boolean; index: number }
178
+ let chosen: Choice | undefined
179
+
180
+ const mark = () => {
181
+ // A followed link stops its heading below the scroll padding of the page and the scroll
182
+ // margin of the heading. The 4px covers fractional scroll positions.
183
+ const padding = Number.parseFloat(view.getComputedStyle(root).scrollPaddingTop) || 0
184
+ let reading = 0
185
+ targets.forEach((target, index) => {
186
+ const margin = Number.parseFloat(view.getComputedStyle(target).scrollMarginTop) || 0
187
+ if (target.getBoundingClientRect().top <= padding + margin + 4) reading = index
188
+ })
189
+ // At the end of the page the last heading may never reach the line.
190
+ const end = view.scrollY > 0 && view.scrollY + view.innerHeight >= root.scrollHeight - 2
191
+
192
+ if (chosen) {
193
+ const top = targets[chosen.index]?.getBoundingClientRect().top ?? -1
194
+ const visible = top >= 0 && top < view.innerHeight
195
+ chosen.arrived ||= visible
196
+
197
+ // The choice ends when the reading position agrees with it, or when its heading leaves the
198
+ // screen or a later heading reaches the line. The heading starts off screen for a far link.
199
+ const agrees = reading === chosen.index && !end
200
+ const left = chosen.arrived && (!visible || reading > chosen.index)
201
+ if (agrees || left) chosen = undefined
202
+ }
203
+
204
+ const current = targets[chosen?.index ?? (end ? targets.length - 1 : reading)]
205
+ for (const anchor of links)
206
+ anchor.setAttribute('aria-current', String(anchor.getAttribute('href') === '#' + current?.id))
207
+ }
208
+ const choose = (id: string) => {
209
+ const index = targets.findIndex((target) => target.id === id)
210
+ chosen = index < 0 ? undefined : { arrived: false, index }
211
+ mark()
212
+ }
213
+ // A choice whose heading is not on screen once the page is at rest went somewhere else.
214
+ const settle = () => {
215
+ mark()
216
+ if (chosen && !chosen.arrived) chosen = undefined
217
+ mark()
218
+ }
219
+
220
+ // Only a plain click scrolls this page. A click with a modifier opens the link somewhere else.
221
+ for (const anchor of links)
222
+ anchor.addEventListener('click', (event) => {
223
+ const modified = event.altKey || event.ctrlKey || event.metaKey || event.shiftKey
224
+ if (event.button !== 0 || modified || event.defaultPrevented) return
225
+
226
+ choose((anchor.getAttribute('href') ?? '').slice(1))
227
+ })
228
+ // The address also changes on Back and Forward, after the page scrolls. The event of a followed
229
+ // link is late, and its choice may have ended by then.
230
+ view.addEventListener('hashchange', () => {
231
+ const id = fragment()
232
+ if (targets[chosen?.index ?? -1]?.id === id) return
233
+
234
+ choose(id)
235
+ settle()
236
+ })
237
+ view.addEventListener('scroll', () => view.requestAnimationFrame(mark), { passive: true })
238
+ view.addEventListener('scrollend', settle)
239
+
240
+ // The browser may scroll to the fragment of the address after this script runs.
241
+ choose(fragment())
242
+ if (page.readyState === 'complete') settle()
243
+ else view.addEventListener('load', settle, { once: true })
244
+ }
245
+
246
+ /**
247
+ * Turns each `span.sn > small` into a numbered button with a popover, and lists the notes at the
248
+ * end. Escape closes the open note and returns focus to its button. The notes of a Brief summary
249
+ * take the first numbers, since the summary is at the top of the page.
250
+ */
251
+ function notes(doc: HTMLElement, view: Window, summary?: HTMLElement) {
252
+ const page = doc.ownerDocument
253
+ const roots = summary ? [summary, doc] : [doc]
254
+ const items = roots
255
+ .flatMap((root) => [...root.querySelectorAll<HTMLElement>('.sn')])
256
+ .flatMap((note) => {
257
+ const small = note.querySelector<HTMLElement>(':scope > small')
258
+ return small ? [{ note, small }] : []
259
+ })
260
+ if (items.length === 0) return
261
+
262
+ let open: { button: HTMLButtonElement; note: HTMLElement; small: HTMLElement } | undefined
263
+ const close = () => {
264
+ if (!open) return
265
+ open.note.classList.remove('open')
266
+ open.button.setAttribute('aria-expanded', 'false')
267
+ open = undefined
268
+ }
269
+ const place = (note: HTMLElement, button: HTMLButtonElement, small: HTMLElement) => {
270
+ // The open note is positioned against its containing block: the nearest positioned ancestor
271
+ // (the article.doc), or the window for a note in the sticky summary. That note is fixed, so
272
+ // the scroll box of the summary does not clip it.
273
+ const fixed = view.getComputedStyle(small).position === 'fixed'
274
+ const frame = fixed ? { left: 0, top: 0 } : (small.offsetParent ?? doc).getBoundingClientRect()
275
+ const anchor = button.getBoundingClientRect()
276
+ const width = Math.min(320, view.innerWidth - 40)
277
+ const x = Math.max(
278
+ 20 - frame.left,
279
+ Math.min(anchor.left - frame.left - 16, view.innerWidth - 20 - width - frame.left),
280
+ )
281
+ // A fixed note does not scroll into view, so it opens above its number when the window has
282
+ // no room below.
283
+ const below = anchor.bottom + 8
284
+ const above = Math.max(8, anchor.top - 8 - small.offsetHeight)
285
+ const y = fixed && below + small.offsetHeight > view.innerHeight - 8 ? above : below
286
+ note.style.setProperty('--pop-x', x + 'px')
287
+ note.style.setProperty('--pop-y', y - frame.top + 'px')
288
+ }
289
+ const show = (note: HTMLElement, button: HTMLButtonElement, small: HTMLElement) => {
290
+ if (open && open.note !== note) close()
291
+ note.classList.add('open')
292
+ button.setAttribute('aria-expanded', 'true')
293
+ open = { button, note, small }
294
+ place(note, button, small)
295
+ }
296
+
297
+ const list = page.createElement('ol')
298
+ items.forEach((item, index) => {
299
+ const { note, small } = item
300
+ const number = String(index + 1)
301
+ small.id ||= 'note-' + number
302
+ small.setAttribute('role', 'note')
303
+ small.dataset['note'] = number
304
+
305
+ const button = page.createElement('button')
306
+ button.type = 'button'
307
+ button.className = 'sn-ref'
308
+ button.textContent = number
309
+ button.setAttribute('aria-label', 'Note ' + number)
310
+ button.setAttribute('aria-controls', small.id)
311
+ button.setAttribute('aria-expanded', 'false')
312
+ note.insertBefore(button, small)
313
+ button.addEventListener('click', (event) => {
314
+ event.stopPropagation()
315
+ if (note.classList.contains('open')) close()
316
+ else show(note, button, small)
317
+ })
318
+
319
+ let timer: ReturnType<typeof setTimeout> | undefined
320
+ note.addEventListener('pointerenter', (event) => {
321
+ if (event.pointerType !== 'mouse') return
322
+ clearTimeout(timer)
323
+ timer = setTimeout(() => show(note, button, small), 300)
324
+ })
325
+ note.addEventListener('pointerleave', (event) => {
326
+ if (event.pointerType !== 'mouse') return
327
+ clearTimeout(timer)
328
+ timer = setTimeout(() => open?.note === note && close(), 160)
329
+ })
330
+
331
+ const listed = page.createElement('li')
332
+ listed.innerHTML = small.innerHTML
333
+ list.append(listed)
334
+ })
335
+
336
+ page.addEventListener('click', (event) => {
337
+ if (open && !open.note.contains(event.target as Node)) close()
338
+ })
339
+ // A fixed note does not move with its number, so place it again when anything scrolls.
340
+ if (summary)
341
+ view.addEventListener(
342
+ 'scroll',
343
+ () => view.requestAnimationFrame(() => open && place(open.note, open.button, open.small)),
344
+ { capture: true, passive: true },
345
+ )
346
+ page.addEventListener('keydown', (event) => {
347
+ if (event.key !== 'Escape' || !open) return
348
+ const { button, note } = open
349
+ const inside = note.contains(page.activeElement)
350
+ close()
351
+ if (inside) button.focus()
352
+ })
353
+
354
+ const section = page.createElement('section')
355
+ section.className = 'footnotes'
356
+ section.setAttribute('aria-label', 'Notes')
357
+ const heading = page.createElement('h2')
358
+ heading.textContent = 'Notes'
359
+ section.append(heading, list)
360
+ doc.append(section)
361
+ }
362
+
363
+ /**
364
+ * Lays an Axis timeline out horizontally at 768px and wider, in lanes so labels never overlap.
365
+ * Narrower, it stays a vertical list in date order.
366
+ */
367
+ function place(axis: HTMLOListElement, view: Window) {
368
+ const page = axis.ownerDocument
369
+ const items = [...axis.children] as HTMLElement[]
370
+ const day = 864e5
371
+ const start = Date.parse(axis.dataset['start'] ?? '')
372
+ const end = Date.parse(axis.dataset['end'] ?? '')
373
+ if (!Number.isFinite(start) || !Number.isFinite(end) || end <= start) return
374
+ const at = (time: number) => (time - start) / (end - start)
375
+
376
+ for (const item of items) {
377
+ const times = [...item.querySelectorAll('time')].map((time) =>
378
+ Date.parse(time.getAttribute('datetime') ?? ''),
379
+ )
380
+ const first = times[0] ?? start
381
+ item.style.setProperty('--x', at(first).toFixed(4))
382
+ if (item.classList.contains('phase'))
383
+ item.style.setProperty('--w', (at(times[1] ?? first) - at(first)).toFixed(4))
384
+ }
385
+ // As a list, read in date order with each phase ahead of the events inside it.
386
+ items
387
+ .map((item, index) => ({
388
+ index,
389
+ item,
390
+ kind: item.classList.contains('phase') ? 0 : 1,
391
+ x: Number(item.style.getPropertyValue('--x')),
392
+ }))
393
+ .sort((a, b) => a.x - b.x || a.kind - b.kind || a.index - b.index)
394
+ .forEach((entry, order) => {
395
+ entry.item.style.order = String(order)
396
+ })
397
+ axis.style.display = 'flex'
398
+ axis.style.flexDirection = 'column'
399
+
400
+ const scale = page.createElement('div')
401
+ scale.className = 'axis-scale'
402
+ scale.setAttribute('aria-hidden', 'true')
403
+ const marks = [start]
404
+ for (let time = start + 7 * day; time < end - 3 * day; time += 7 * day) marks.push(time)
405
+ marks.push(end)
406
+ for (const mark of marks) {
407
+ const span = page.createElement('span')
408
+ span.style.left = (at(mark) * 100).toFixed(2) + '%'
409
+ span.textContent = new Date(mark).toLocaleDateString('en-US', {
410
+ day: 'numeric',
411
+ month: 'short',
412
+ timeZone: 'UTC',
413
+ })
414
+ scale.append(span)
415
+ }
416
+ axis.after(scale)
417
+
418
+ const wide = view.matchMedia('(min-width: 768px)')
419
+ const lanes = () => {
420
+ if (!wide.matches) {
421
+ delete axis.dataset['placed']
422
+ axis.style.display = 'flex'
423
+ return
424
+ }
425
+ axis.dataset['placed'] = ''
426
+ axis.style.removeProperty('display')
427
+ const width = axis.clientWidth
428
+ const fill = (list: HTMLElement[], phase: boolean) => {
429
+ const ends: number[] = []
430
+ const placed: { lane: number; x: number }[] = []
431
+ for (const item of list) {
432
+ delete item.dataset['flip']
433
+ item.style.setProperty('--lane', '0')
434
+ const x = Number(item.style.getPropertyValue('--x')) * width
435
+ const size = phase
436
+ ? Math.max(item.scrollWidth, Number(item.style.getPropertyValue('--w')) * width)
437
+ : item.scrollWidth
438
+ let left = x
439
+ let right = x + size
440
+ if (!phase && right > width) {
441
+ item.dataset['flip'] = ''
442
+ left = x - size
443
+ right = x
444
+ }
445
+ let lane: number
446
+ if (phase) {
447
+ lane = ends.findIndex((edge) => edge < left - 12)
448
+ if (lane < 0) lane = ends.push(0) - 1
449
+ ends[lane] = right
450
+ } else {
451
+ const crossed = (depth: number) =>
452
+ placed.some(
453
+ (other) => other.lane > depth && other.x >= left - 4 && other.x <= right + 4,
454
+ )
455
+ lane = ends.findIndex((edge, depth) => edge > right + 12 && !crossed(depth))
456
+ if (lane < 0) lane = ends.push(0) - 1
457
+ ends[lane] = left
458
+ placed.push({ lane, x: 'flip' in item.dataset ? right : left })
459
+ }
460
+ item.style.setProperty('--lane', String(lane))
461
+ }
462
+ return Math.max(1, ends.length)
463
+ }
464
+ const phases = items.filter((item) => item.classList.contains('phase'))
465
+ // Events right to left, so a leader line never crosses a label in a shallower lane.
466
+ const events = items
467
+ .filter((item) => !item.classList.contains('phase') && !item.classList.contains('minor'))
468
+ .reverse()
469
+ axis.style.setProperty('--phase-lanes', String(fill(phases, true)))
470
+ axis.style.setProperty('--event-lanes', String(fill(events, false)))
471
+ }
472
+ lanes()
473
+ wide.addEventListener('change', lanes)
474
+ view.addEventListener('resize', lanes)
475
+ void page.fonts?.ready.then(lanes)
476
+ }
477
+
478
+ /** Adds Essay's reading rail: progress through the page, with a tick for each section. */
479
+ function rail(doc: HTMLElement, view: Window) {
480
+ const page = doc.ownerDocument
481
+ const headings = [...doc.querySelectorAll<HTMLHeadingElement>(':scope > h2[id]')]
482
+ const nav = page.createElement('nav')
483
+ nav.className = 'rail'
484
+ nav.setAttribute('aria-label', 'Reading progress')
485
+ nav.innerHTML = '<div class="rail-track"></div><div class="rail-fill"></div>'
486
+ const ticks = headings.map((heading) => {
487
+ const anchor = page.createElement('a')
488
+ const name = heading.dataset['name'] ?? heading.textContent ?? ''
489
+ anchor.href = '#' + heading.id
490
+ anchor.setAttribute('aria-label', 'Go to ' + name)
491
+ const label = page.createElement('span')
492
+ label.textContent = name
493
+ anchor.append(label)
494
+ nav.append(anchor)
495
+ return anchor
496
+ })
497
+ page.body.append(nav)
498
+
499
+ const header = doc.querySelector('header')
500
+ let span = 1
501
+ const measure = () => {
502
+ span = Math.max(1, page.documentElement.scrollHeight - view.innerHeight)
503
+ headings.forEach((heading, index) => {
504
+ const y = heading.getBoundingClientRect().top + view.scrollY - view.innerHeight * 0.25
505
+ ticks[index]?.style.setProperty('--at', Math.min(1, Math.max(0, y / span)).toFixed(4))
506
+ })
507
+ }
508
+ const update = () => {
509
+ nav.style.setProperty('--p', Math.min(1, Math.max(0, view.scrollY / span)).toFixed(4))
510
+ const started = header ? header.getBoundingClientRect().bottom < 80 : view.scrollY > 200
511
+ nav.toggleAttribute('data-on', started)
512
+ }
513
+ measure()
514
+ update()
515
+ view.addEventListener('scroll', () => view.requestAnimationFrame(update), { passive: true })
516
+ view.addEventListener('resize', () => {
517
+ measure()
518
+ update()
519
+ })
520
+ void page.fonts?.ready.then(() => {
521
+ measure()
522
+ update()
523
+ })
524
+ }
525
+
526
+ /** Returns a URL fragment for a heading's text. */
527
+ function slug(text: string) {
528
+ return text
529
+ .toLowerCase()
530
+ .replace(/[^a-z0-9]+/g, '-')
531
+ .replace(/^-|-$/g, '')
532
+ }
@@ -0,0 +1,38 @@
1
+ #!/usr/bin/env node
2
+ /** The `tempo-document` executable, which runs the document command line on the filesystem. @module */
3
+ import * as Fs from 'node:fs/promises'
4
+ import * as Cli from './internal/Cli.ts'
5
+
6
+ // The bin sits at `dist/document` when installed and at `src/document` when linked for development.
7
+ const manifest = JSON.parse(
8
+ await Fs.readFile(new URL('../../package.json', import.meta.url), 'utf8'),
9
+ ) as { version: string }
10
+
11
+ const result = await Cli.run({
12
+ args: process.argv.slice(2),
13
+ files: {
14
+ async create(path, text) {
15
+ try {
16
+ await Fs.writeFile(path, text, { flag: 'wx' })
17
+ return true
18
+ } catch (error) {
19
+ if ((error as NodeJS.ErrnoException).code === 'EEXIST') return false
20
+ throw error
21
+ }
22
+ },
23
+ async read(path) {
24
+ try {
25
+ return await Fs.readFile(path, 'utf8')
26
+ } catch (error) {
27
+ if ((error as NodeJS.ErrnoException).code === 'ENOENT') return undefined
28
+ throw error
29
+ }
30
+ },
31
+ template: (name) => Fs.readFile(new URL(`./templates/${name}.html`, import.meta.url), 'utf8'),
32
+ },
33
+ version: manifest.version,
34
+ })
35
+
36
+ process.stdout.write(result.stdout)
37
+ process.stderr.write(result.stderr)
38
+ process.exitCode = result.code