@estiva-app/protocol 0.32.0 → 0.33.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/src/index.ts CHANGED
@@ -47,7 +47,7 @@
47
47
  /** Bumped by hand with the version in package.json — `test/version.test.ts`
48
48
  * pins the two together. It exists so "the upgrade reached the app" can be
49
49
  * checked by grepping a built bundle rather than by trusting a lockfile. */
50
- export const PROTOCOL_VERSION = '0.32.0'
50
+ export const PROTOCOL_VERSION = '0.33.0'
51
51
 
52
52
  export {
53
53
  // types
@@ -225,6 +225,9 @@ export {
225
225
  renderTreeText,
226
226
  standaloneReference,
227
227
  referencesIn,
228
+ type BlockCard,
229
+ blockCards,
230
+ cardLayout,
228
231
  } from './render.js'
229
232
 
230
233
  export {
@@ -259,7 +262,7 @@ export {
259
262
  resolveBlockAnchor,
260
263
  resolvePart,
261
264
  } from './anchors.js'
262
- export { type Embed, type EmbedLayout, type LinkResolver, EMBED_TAG, cardsIn, embedTag, embedsOf, linksAsReferences } from './embeds.js'
265
+ export { type Embed, type EmbedLayout, type LinkResolver, EMBED_TAG, cardsIn, embedTag, embedsOf, linksAsReferences, referenceOfLink, urlRunsIn } from './embeds.js'
263
266
  export {
264
267
  type BlossomVerb,
265
268
  type Imeta,
package/src/render.ts CHANGED
@@ -35,9 +35,11 @@ import {
35
35
  type InlineTextNode,
36
36
  parseBodySegments,
37
37
  parseInlineMarks,
38
+ targetMark,
38
39
  } from './content.js'
39
40
  import { type Block, type BlockDocument, type CalloutTone, parseBlockDocument, inlineTextOf, listItemDepths, listItemChecked, calloutTone } from './blocks.js'
40
41
  import type { SignedEvent } from './events.js'
42
+ import { type LinkResolver, referenceOfLink } from './embeds.js'
41
43
 
42
44
  /** A run of text and the marks on it. Nothing here needs escaping; it is text. */
43
45
  export interface RenderInline {
@@ -51,6 +53,18 @@ export interface RenderInline {
51
53
  * nothing still shows the token rather than a blank.
52
54
  */
53
55
  reference?: string
56
+ /**
57
+ * The card its writer chose for this reference in a block document (§13.3,
58
+ * "A reference's card"): absent for none. A message's cards are its `embed`
59
+ * tags instead (§13.6.2), so a marker body never has one.
60
+ */
61
+ embed?: 'compact' | 'detailed'
62
+ /**
63
+ * The URL a block document's `link` mark points this run at (§13.3). On a run
64
+ * whose text is the URL, `linksAsReferences` turns it into the reference the
65
+ * URL names, `embed` and all; a link on words keeps it.
66
+ */
67
+ href?: string
54
68
  }
55
69
 
56
70
  /**
@@ -158,6 +172,8 @@ const KNOWN: ReadonlySet<string> = new Set([
158
172
  'blockquote', 'codeBlock', 'table', 'horizontalRule', 'attachment', 'widget',
159
173
  ])
160
174
 
175
+ const INLINE_MARKS: ReadonlySet<string> = new Set<InlineMark>(['bold', 'italic', 'underline', 'code'])
176
+
161
177
  const marksOf = (span: { bold?: boolean; italic?: boolean; underline?: boolean; code?: boolean }): InlineMark[] => {
162
178
  const out: InlineMark[] = []
163
179
  if (span.bold) out.push('bold')
@@ -232,11 +248,16 @@ function blockFromDocument(block: Block): RenderBlock {
232
248
  if (isInlineRun(block.content)) {
233
249
  out.inline = block.content.map((node) => {
234
250
  const marks = node.marks ?? []
235
- const reference = marks.find((m) => m.type === 'reference')
251
+ const reference = targetMark(marks, 'reference')
252
+ const link = reference ? undefined : targetMark(marks, 'link')
253
+ const embed = cardLayout((reference ?? link)?.embed)
236
254
  return {
237
255
  text: node.text,
238
- marks: marks.filter((m) => m.type !== 'reference').map((m) => m.type) as InlineMark[],
239
- ...(reference && 'attrs' in reference ? { reference: reference.attrs.uri } : {}),
256
+ // Only the four the vocabulary has: a mark this reader does not know is not one to draw (§13.3).
257
+ marks: marks.map((m) => m.type).filter((type): type is InlineMark => INLINE_MARKS.has(type)),
258
+ ...(reference ? { reference: reference.uri } : {}),
259
+ ...(link ? { href: link.href } : {}),
260
+ ...(embed ? { embed } : {}),
240
261
  }
241
262
  })
242
263
  } else {
@@ -328,10 +349,11 @@ export function renderTreeText(blocks: readonly RenderBlock[]): string {
328
349
  * on its line is alone on its line whether or not somebody left a space after
329
350
  * it.
330
351
  *
331
- * **For a block document (a description) only, since OPT-18.** In a message a
332
- * reference reads as its chip wherever it sits, and a card comes only from the
333
- * author's `embed` (§13.6.2). A description has no `embed` yet, so its readers
334
- * still draw a reference alone on its line as a card until it can choose one.
352
+ * @deprecated Since 0.33.0 a reference reads as its chip wherever it sits, in a
353
+ * message (§13.6.2) and in a block document (§13.3, "A reference's card"); a
354
+ * card comes only from what its writer chose. Use {@link blockCards} for a
355
+ * document and `cardsIn` for a message. Kept for one release so a reader on it
356
+ * still builds.
335
357
  */
336
358
  export function standaloneReference(block: RenderBlock): string | undefined {
337
359
  if (block.type !== 'paragraph' || block.children?.length) return undefined
@@ -341,6 +363,58 @@ export function standaloneReference(block: RenderBlock): string | undefined {
341
363
  return only.reference && only.text.trim() === only.reference ? only.reference : undefined
342
364
  }
343
365
 
366
+ // ── A reference's card in a block document — SPEC §13.3 ─────────────────────
367
+
368
+ /**
369
+ * `attrs.embed` on a reference mark, read: `compact`, `detailed`, or absent for
370
+ * no card. A string this reader does not know is `compact` — the writer asked
371
+ * for a card, and the smallest is the safe reading, as for a message's `embed`
372
+ * tag (§13.6.2). `none`, a non-string and absence are all no card.
373
+ */
374
+ export function cardLayout(raw: unknown): 'compact' | 'detailed' | undefined {
375
+ // An empty string names no layout: no card, as an editor saves it.
376
+ if (typeof raw !== 'string' || raw === '' || raw === 'none') return undefined
377
+ return raw === 'detailed' ? 'detailed' : 'compact'
378
+ }
379
+
380
+ /** One card a block document asks for: the reference it hangs from, and how much to show. */
381
+ export interface BlockCard {
382
+ /** The `nostr:` URI, as the reference mark carries it. */
383
+ reference: string
384
+ layout: 'compact' | 'detailed'
385
+ }
386
+
387
+ /**
388
+ * The cards to draw after this block, in the order their references appear
389
+ * (§13.3, "A reference's card"). Only the block's own runs: a list's cards hang
390
+ * from each item, so a reader asks each item.
391
+ *
392
+ * Each occurrence is its own card, so the same object twice may draw twice. A
393
+ * reference split across runs (part of it bold) is one occurrence, and takes
394
+ * its card from its first run.
395
+ *
396
+ * A link carries its card the same way. Read the block through
397
+ * `linksAsReferences` first and a URL is its reference already; a link on
398
+ * other words draws its card only when `resolveLink` says what its `href`
399
+ * names (§7.7), and without one names nothing.
400
+ *
401
+ * Inside a card's body or a part a reader asks nothing: there every reference
402
+ * is its chip (§13.3), or two objects that card each other nest forever.
403
+ */
404
+ export function blockCards(block: RenderBlock, resolveLink?: LinkResolver): BlockCard[] {
405
+ const out: BlockCard[] = []
406
+ let previous: string | undefined
407
+ for (const run of block.inline ?? []) {
408
+ const target = run.reference ?? (run.href !== undefined ? `href:${run.href}` : undefined)
409
+ if (target && target === previous) continue
410
+ previous = target
411
+ if (!run.embed) continue
412
+ const reference = run.reference ?? (run.href !== undefined && resolveLink ? referenceOfLink(run.href, resolveLink) : undefined)
413
+ if (reference) out.push({ reference, layout: run.embed })
414
+ }
415
+ return out
416
+ }
417
+
344
418
  /**
345
419
  * Every `nostr:` reference in a resolved tree, in the order they are written.
346
420
  *