@estiva-app/protocol 0.5.0 → 0.7.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/CHANGELOG.md +93 -0
- package/dist/blocks.d.ts +87 -0
- package/dist/blocks.d.ts.map +1 -0
- package/dist/blocks.js +257 -0
- package/dist/blocks.js.map +1 -0
- package/dist/content.d.ts +107 -0
- package/dist/content.d.ts.map +1 -0
- package/dist/content.js +311 -0
- package/dist/content.js.map +1 -0
- package/dist/index.d.ts +3 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -1
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/src/blocks.ts +276 -0
- package/src/content.ts +350 -0
- package/src/index.ts +33 -1
package/CHANGELOG.md
CHANGED
|
@@ -4,6 +4,99 @@ Every entry answers the wire question explicitly, including when the answer is
|
|
|
4
4
|
nothing (ADR 0002 §4b). A change to the bytes an app publishes is a MAJOR — in
|
|
5
5
|
`0.x`, a MINOR — even when no TypeScript signature moved.
|
|
6
6
|
|
|
7
|
+
## 0.7.0 — 2026-09-02
|
|
8
|
+
|
|
9
|
+
**Wire behaviour: unchanged.** No builder, tag layout, id computation or
|
|
10
|
+
signature input moved. This release adds a second content model — the one SPEC
|
|
11
|
+
§13.3 specifies for a rich text field — and nothing yet publishes one.
|
|
12
|
+
|
|
13
|
+
- **New: `blocks.ts` — the rich text field, SPEC §13.3.** `parseBlockDocument`,
|
|
14
|
+
`serializeBlockDocument`, `validateBlockDocument`, `newBlockId`,
|
|
15
|
+
`assignMissingBlockIds`, `blockIds`, `findBlock`, `inlineTextOf`,
|
|
16
|
+
`documentText`.
|
|
17
|
+
|
|
18
|
+
**`assignMissingBlockIds` mints only what is missing**, and that is the whole
|
|
19
|
+
point rather than an optimisation. An editor rebuilds its document on every
|
|
20
|
+
keystroke; a rebuild that re-mints ids detaches every anchored comment on the
|
|
21
|
+
next edit, and nothing reports it. A block id is what RFC 0.4 §6 anchoring
|
|
22
|
+
binds to, so the function makes the safe path the easy one. There is a test
|
|
23
|
+
that fails if it is changed to mint unconditionally.
|
|
24
|
+
|
|
25
|
+
- **New: the inline vocabulary's second serialisation** (§13.1) —
|
|
26
|
+
`markersToInlineNodes`, `inlineNodesToMarkers`, `inlineNodesToText`. Held back
|
|
27
|
+
from 0.6.0 on purpose: it had no consumer until the block model existed.
|
|
28
|
+
|
|
29
|
+
Mark order in the JSON encoding is **deterministic**, because two encoders
|
|
30
|
+
disagreeing about array order produce documents that differ byte-for-byte
|
|
31
|
+
while meaning the same thing, and every equality check downstream — a diff, a
|
|
32
|
+
dedupe, a cache key — is then wrong about it.
|
|
33
|
+
|
|
34
|
+
- **An unknown block type is not an error anywhere in this module.** §13.3
|
|
35
|
+
requires a reader to draw its inline text and forbids dropping it silently,
|
|
36
|
+
the same discipline §7.5 applies to widgets and for the same reason:
|
|
37
|
+
consumers upgrade at different times. `validateBlockDocument` passes it,
|
|
38
|
+
round-tripping preserves it whole, and `inlineTextOf` reads its text by shape
|
|
39
|
+
rather than by name.
|
|
40
|
+
|
|
41
|
+
- **The `content-format` tag is deliberately NOT defined here.** §13.4's read
|
|
42
|
+
rule is `contentFormatOf` in `@estiva-app/interop`, added in #26, because it
|
|
43
|
+
is a question about an event and its slot rather than about a document.
|
|
44
|
+
A second copy of `'estiva-blocks-1'` in this package is precisely the
|
|
45
|
+
divergence this package exists to prevent, so `blocks.ts` never names it.
|
|
46
|
+
|
|
47
|
+
- **Tests: the claim §13 makes, checked rather than asserted.** The 152 real
|
|
48
|
+
published bodies from 0.6.0 now go through both encodings, and no character
|
|
49
|
+
changes its marks. Two controls were run against it — dropping `code` from
|
|
50
|
+
the JSON encoding fails 4 tests, re-minting ids unconditionally fails 2.
|
|
51
|
+
|
|
52
|
+
## 0.6.0 — 2026-09-02
|
|
53
|
+
|
|
54
|
+
**Wire behaviour: unchanged.** No builder, tag layout, id computation or
|
|
55
|
+
signature input moved. This release adds a parser, and a parser reads bytes
|
|
56
|
+
somebody else already published.
|
|
57
|
+
|
|
58
|
+
**What it does change is what readers draw**, which is not the wire and is not
|
|
59
|
+
nothing: an app that upgrades starts rendering backtick spans in messages that
|
|
60
|
+
have been on the relay for weeks. Measured 2026-09-02, that is **200 of 548
|
|
61
|
+
published message bodies** — the most common construct in the corpus, more
|
|
62
|
+
common than bold, and until now rendered by nobody. Upgrading is visible to
|
|
63
|
+
users on history, not only on new messages.
|
|
64
|
+
|
|
65
|
+
- **New: `content.ts` — the message marker dialect, SPEC §13.2.** `parseInlineMarks`,
|
|
66
|
+
`wrapInlineMarks`, `parseBodySegments` and `stripInlineFormatting`, moved out
|
|
67
|
+
of Peek's `src/lib/textParsing.ts` rather than written fresh, so the behaviour
|
|
68
|
+
that was already shipping is preserved by construction. Peek's own tests for
|
|
69
|
+
them are ported verbatim into `test/content.test.ts` as the proof.
|
|
70
|
+
|
|
71
|
+
Only the **mark** layer moved. Peek's mentions and bracket references resolve
|
|
72
|
+
against its own directory and fixtures, and they stay there — a mention is
|
|
73
|
+
RIC-2's, and dragging its regexes along would have imported one app's mock
|
|
74
|
+
data into the shared package.
|
|
75
|
+
|
|
76
|
+
- **New in the dialect: `code` and fenced code** (SPEC §13.1, §13.2). A code
|
|
77
|
+
span carries `code` and never combines with another mark, and its content is
|
|
78
|
+
never parsed for further marks — so `` `**x**` `` is four literal characters
|
|
79
|
+
and a name.
|
|
80
|
+
|
|
81
|
+
- **SPEC §13.2's rule 2 does not fence a backtick**, and the corpus is why.
|
|
82
|
+
Rule 2 keeps `2*3*4` literal, which an asymmetric prose marker needs and a
|
|
83
|
+
backtick does not — applying it anyway left `` `main`s `` unparsed, a code
|
|
84
|
+
span followed by a plural or possessive. Eight such spans in six published
|
|
85
|
+
messages. SPEC was amended to say so (estiva-docs#56) rather than the parser
|
|
86
|
+
quietly disagreeing with it.
|
|
87
|
+
|
|
88
|
+
- **Fixed while extracting: `stripInlineFormatting` was not idempotent.** It
|
|
89
|
+
removed the heading prefix before the quote prefix, so `> # Heading` — the
|
|
90
|
+
shape every project brief in this workspace is written in — came back as
|
|
91
|
+
`# Heading` and previews showed a stray `#`. Prefixes are now stripped until
|
|
92
|
+
the line stops changing. **This bug was invisible to the tests it shipped
|
|
93
|
+
with** and was found by running the function over 152 real published bodies.
|
|
94
|
+
|
|
95
|
+
- **New: `test/corpus-bodies.json`** — 152 real message bodies from production.
|
|
96
|
+
A parser checked only against fixtures written beside it is checked against
|
|
97
|
+
its own assumptions; both defects above came from this file and neither came
|
|
98
|
+
from the unit tests.
|
|
99
|
+
|
|
7
100
|
## 0.5.0 — 2026-09-01
|
|
8
101
|
|
|
9
102
|
**Wire behaviour: unchanged.** No builder, tag layout, id computation or
|
package/dist/blocks.d.ts
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import { type InlineTextNode } from './content.js';
|
|
2
|
+
/**
|
|
3
|
+
* The block types §13.3 names.
|
|
4
|
+
*
|
|
5
|
+
* **Open, not closed.** An unknown type is not an error at any point in this
|
|
6
|
+
* module — §13.3 requires a reader to render its inline text and forbids
|
|
7
|
+
* dropping it silently, which is the same fallback discipline §7.5 applies to
|
|
8
|
+
* widgets and for the same reason: consumers upgrade at different times, and a
|
|
9
|
+
* block that vanishes is indistinguishable from one you may not read.
|
|
10
|
+
*/
|
|
11
|
+
export declare const KNOWN_BLOCK_TYPES: readonly ["paragraph", "heading", "bulletList", "orderedList", "listItem", "blockquote", "codeBlock", "table", "horizontalRule", "attachment", "widget"];
|
|
12
|
+
export type KnownBlockType = (typeof KNOWN_BLOCK_TYPES)[number];
|
|
13
|
+
export interface Block {
|
|
14
|
+
/** A {@link KnownBlockType}, or anything a later version publishes. */
|
|
15
|
+
type: string;
|
|
16
|
+
/** Unique within the document, and stable across edits that do not replace it. */
|
|
17
|
+
id: string;
|
|
18
|
+
attrs?: Record<string, unknown>;
|
|
19
|
+
/** Inline runs, or child blocks. A leaf like `horizontalRule` has neither. */
|
|
20
|
+
content?: InlineTextNode[] | Block[];
|
|
21
|
+
}
|
|
22
|
+
export interface BlockDocument {
|
|
23
|
+
type: 'doc';
|
|
24
|
+
content: Block[];
|
|
25
|
+
}
|
|
26
|
+
/** Thrown by {@link parseBlockDocument}; `problems` is {@link validateBlockDocument}'s list. */
|
|
27
|
+
export declare class BlockDocumentError extends Error {
|
|
28
|
+
readonly problems: string[];
|
|
29
|
+
constructor(problems: string[]);
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Every way a value fails to be a block document, rather than the first.
|
|
33
|
+
*
|
|
34
|
+
* A validator that stops at the first problem makes fixing a document a
|
|
35
|
+
* sequence of round trips, and this one is read by authors of *other* apps.
|
|
36
|
+
*/
|
|
37
|
+
export declare function validateBlockDocument(value: unknown): string[];
|
|
38
|
+
/**
|
|
39
|
+
* A block document from JSON text, or from an already-parsed value.
|
|
40
|
+
*
|
|
41
|
+
* **Throws rather than returning a partial document.** A caller that cannot
|
|
42
|
+
* tell "this is not a block document" from "this is an empty one" would render
|
|
43
|
+
* a blank field for a body that is really there — and §13.4 is explicit that a
|
|
44
|
+
* body's model is decided by its event's tag, so arriving here with something
|
|
45
|
+
* that is not a document means the tag was wrong, which is worth an error and
|
|
46
|
+
* not a shrug.
|
|
47
|
+
*/
|
|
48
|
+
export declare function parseBlockDocument(input: string | unknown): BlockDocument;
|
|
49
|
+
/** JSON text for the wire. §12.3's 256 KiB ingest cap applies to this string. */
|
|
50
|
+
export declare function serializeBlockDocument(doc: BlockDocument): string;
|
|
51
|
+
/**
|
|
52
|
+
* A fresh block id.
|
|
53
|
+
*
|
|
54
|
+
* Short on purpose: ids repeat in every anchor and every serialisation, the
|
|
55
|
+
* document has a 256 KiB ceiling, and uniqueness is only required *within one
|
|
56
|
+
* document* (§13.3) rather than globally. 12 hex characters is far more than
|
|
57
|
+
* that needs, and {@link assignMissingBlockIds} checks for a collision anyway
|
|
58
|
+
* rather than trusting the arithmetic.
|
|
59
|
+
*/
|
|
60
|
+
export declare function newBlockId(): string;
|
|
61
|
+
/**
|
|
62
|
+
* Ids for blocks that have none, **leaving every existing id alone**.
|
|
63
|
+
*
|
|
64
|
+
* This is the id-stability rule (§13.3) as a function. An editor rebuilds a
|
|
65
|
+
* document on every keystroke; if that rebuild re-mints ids, every anchored
|
|
66
|
+
* comment detaches on the next edit and the anchoring feature is silently
|
|
67
|
+
* worthless. Assigning only what is missing makes the safe path the easy one.
|
|
68
|
+
*
|
|
69
|
+
* Returns a new document; the input is not mutated.
|
|
70
|
+
*/
|
|
71
|
+
export declare function assignMissingBlockIds(doc: BlockDocument): BlockDocument;
|
|
72
|
+
/** Every block id in document order, nested blocks included. */
|
|
73
|
+
export declare function blockIds(doc: BlockDocument): string[];
|
|
74
|
+
/** The block an anchor points at, or `undefined` if it is gone. */
|
|
75
|
+
export declare function findBlock(doc: BlockDocument, id: string): Block | undefined;
|
|
76
|
+
/**
|
|
77
|
+
* A block's text, including its children's.
|
|
78
|
+
*
|
|
79
|
+
* This is what §13.3 requires a reader to draw for a block type it does not
|
|
80
|
+
* know — *"render that block's inline text rather than dropping it"*. It works
|
|
81
|
+
* on an unknown type for the same reason it works on a known one: it reads the
|
|
82
|
+
* shape, never the name.
|
|
83
|
+
*/
|
|
84
|
+
export declare function inlineTextOf(block: Block): string;
|
|
85
|
+
/** The whole document as plain text — previews, search, and the last-resort render. */
|
|
86
|
+
export declare function documentText(doc: BlockDocument): string;
|
|
87
|
+
//# sourceMappingURL=blocks.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"blocks.d.ts","sourceRoot":"","sources":["../src/blocks.ts"],"names":[],"mappings":"AAyBA,OAAO,EAAE,KAAK,cAAc,EAAqB,MAAM,cAAc,CAAA;AAErE;;;;;;;;GAQG;AACH,eAAO,MAAM,iBAAiB,0JAYpB,CAAA;AAEV,MAAM,MAAM,cAAc,GAAG,CAAC,OAAO,iBAAiB,CAAC,CAAC,MAAM,CAAC,CAAA;AAE/D,MAAM,WAAW,KAAK;IACpB,uEAAuE;IACvE,IAAI,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
@@ -0,0 +1,257 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The rich text field — SPEC §13.3.
|
|
3
|
+
*
|
|
4
|
+
* A message is marker text (`content.ts`); a rich text field is this: a tree of
|
|
5
|
+
* blocks, each with an id, serialised as JSON. **The id is the entire reason
|
|
6
|
+
* this model is JSON rather than text.** A comment anchors to `<object address>
|
|
7
|
+
* + <block id>`, which is what RFC 0.4 §6 was waiting for, and an implementation
|
|
8
|
+
* that mints ids at render time has built a tree that is not addressable — the
|
|
9
|
+
* anchor has nothing to bind to and nobody finds out until it silently drifts.
|
|
10
|
+
*
|
|
11
|
+
* ## What is deliberately not here
|
|
12
|
+
*
|
|
13
|
+
* **The `content-format` tag.** SPEC §13.4's read rule already lives in
|
|
14
|
+
* `@estiva-app/interop` as `contentFormatOf`, alongside `CONTENT_FORMAT_TAG`
|
|
15
|
+
* and `BLOCK_DOCUMENT_FORMAT`, because it is a question about an *event and its
|
|
16
|
+
* slot* rather than about a document. Defining the same wire string here would
|
|
17
|
+
* be a second copy of exactly the kind this package exists to prevent, so this
|
|
18
|
+
* module never names it: a document does not know its own tag, and the reader
|
|
19
|
+
* that does is one layer up.
|
|
20
|
+
*
|
|
21
|
+
* **Rendering.** §13.5's safe renderer is RIC-6's, where Ship is the consumer
|
|
22
|
+
* that needs it. What this module owes a renderer is {@link inlineTextOf}, so
|
|
23
|
+
* an unknown block type can be drawn as its own text rather than dropped.
|
|
24
|
+
*/
|
|
25
|
+
import { bytesToHex, randomBytes } from '@noble/hashes/utils';
|
|
26
|
+
import { inlineNodesToText } from './content.js';
|
|
27
|
+
/**
|
|
28
|
+
* The block types §13.3 names.
|
|
29
|
+
*
|
|
30
|
+
* **Open, not closed.** An unknown type is not an error at any point in this
|
|
31
|
+
* module — §13.3 requires a reader to render its inline text and forbids
|
|
32
|
+
* dropping it silently, which is the same fallback discipline §7.5 applies to
|
|
33
|
+
* widgets and for the same reason: consumers upgrade at different times, and a
|
|
34
|
+
* block that vanishes is indistinguishable from one you may not read.
|
|
35
|
+
*/
|
|
36
|
+
export const KNOWN_BLOCK_TYPES = [
|
|
37
|
+
'paragraph',
|
|
38
|
+
'heading',
|
|
39
|
+
'bulletList',
|
|
40
|
+
'orderedList',
|
|
41
|
+
'listItem',
|
|
42
|
+
'blockquote',
|
|
43
|
+
'codeBlock',
|
|
44
|
+
'table',
|
|
45
|
+
'horizontalRule',
|
|
46
|
+
'attachment',
|
|
47
|
+
'widget',
|
|
48
|
+
];
|
|
49
|
+
/** Thrown by {@link parseBlockDocument}; `problems` is {@link validateBlockDocument}'s list. */
|
|
50
|
+
export class BlockDocumentError extends Error {
|
|
51
|
+
problems;
|
|
52
|
+
constructor(problems) {
|
|
53
|
+
super(`not a block document: ${problems.join('; ')}`);
|
|
54
|
+
this.name = 'BlockDocumentError';
|
|
55
|
+
this.problems = problems;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
const isObject = (v) => typeof v === 'object' && v !== null && !Array.isArray(v);
|
|
59
|
+
const isInlineRun = (content) => Array.isArray(content) && content.every((n) => isObject(n) && n.type === 'text');
|
|
60
|
+
/**
|
|
61
|
+
* Every way a value fails to be a block document, rather than the first.
|
|
62
|
+
*
|
|
63
|
+
* A validator that stops at the first problem makes fixing a document a
|
|
64
|
+
* sequence of round trips, and this one is read by authors of *other* apps.
|
|
65
|
+
*/
|
|
66
|
+
export function validateBlockDocument(value) {
|
|
67
|
+
const problems = [];
|
|
68
|
+
if (!isObject(value))
|
|
69
|
+
return ['root is not an object'];
|
|
70
|
+
if (value.type !== 'doc')
|
|
71
|
+
problems.push(`root type is ${JSON.stringify(value.type)}, expected "doc"`);
|
|
72
|
+
if (!Array.isArray(value.content)) {
|
|
73
|
+
problems.push('root has no content array');
|
|
74
|
+
return problems;
|
|
75
|
+
}
|
|
76
|
+
const seen = new Set();
|
|
77
|
+
const walk = (blocks, path) => {
|
|
78
|
+
blocks.forEach((block, i) => {
|
|
79
|
+
const at = `${path}[${i}]`;
|
|
80
|
+
if (!isObject(block)) {
|
|
81
|
+
problems.push(`${at} is not an object`);
|
|
82
|
+
return;
|
|
83
|
+
}
|
|
84
|
+
if (typeof block.type !== 'string' || block.type === '') {
|
|
85
|
+
problems.push(`${at} has no type`);
|
|
86
|
+
}
|
|
87
|
+
// §13.3: every block, not only the ones an app happens to know.
|
|
88
|
+
if (typeof block.id !== 'string' || block.id === '') {
|
|
89
|
+
problems.push(`${at} has no id`);
|
|
90
|
+
}
|
|
91
|
+
else if (seen.has(block.id)) {
|
|
92
|
+
problems.push(`${at} repeats the id ${JSON.stringify(block.id)}`);
|
|
93
|
+
}
|
|
94
|
+
else {
|
|
95
|
+
seen.add(block.id);
|
|
96
|
+
}
|
|
97
|
+
if (block.type === 'heading') {
|
|
98
|
+
const level = isObject(block.attrs) ? block.attrs.level : undefined;
|
|
99
|
+
if (typeof level !== 'number' || !Number.isInteger(level) || level < 1 || level > 3) {
|
|
100
|
+
problems.push(`${at} is a heading with level ${JSON.stringify(level)}, expected 1–3`);
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
if (block.content === undefined)
|
|
104
|
+
return;
|
|
105
|
+
if (!Array.isArray(block.content)) {
|
|
106
|
+
problems.push(`${at} has a content that is not an array`);
|
|
107
|
+
return;
|
|
108
|
+
}
|
|
109
|
+
if (isInlineRun(block.content)) {
|
|
110
|
+
block.content.forEach((node, j) => {
|
|
111
|
+
const nodeAt = `${at}.content[${j}]`;
|
|
112
|
+
if (typeof node.text !== 'string') {
|
|
113
|
+
problems.push(`${nodeAt} has no text`);
|
|
114
|
+
}
|
|
115
|
+
const marks = node.marks;
|
|
116
|
+
if (marks === undefined)
|
|
117
|
+
return;
|
|
118
|
+
if (!Array.isArray(marks)) {
|
|
119
|
+
problems.push(`${nodeAt} has marks that are not an array`);
|
|
120
|
+
return;
|
|
121
|
+
}
|
|
122
|
+
for (const mark of marks) {
|
|
123
|
+
if (!isObject(mark) || typeof mark.type !== 'string') {
|
|
124
|
+
problems.push(`${nodeAt} has a mark with no type`);
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
});
|
|
128
|
+
return;
|
|
129
|
+
}
|
|
130
|
+
walk(block.content, `${at}.content`);
|
|
131
|
+
});
|
|
132
|
+
};
|
|
133
|
+
walk(value.content, 'content');
|
|
134
|
+
return problems;
|
|
135
|
+
}
|
|
136
|
+
/**
|
|
137
|
+
* A block document from JSON text, or from an already-parsed value.
|
|
138
|
+
*
|
|
139
|
+
* **Throws rather than returning a partial document.** A caller that cannot
|
|
140
|
+
* tell "this is not a block document" from "this is an empty one" would render
|
|
141
|
+
* a blank field for a body that is really there — and §13.4 is explicit that a
|
|
142
|
+
* body's model is decided by its event's tag, so arriving here with something
|
|
143
|
+
* that is not a document means the tag was wrong, which is worth an error and
|
|
144
|
+
* not a shrug.
|
|
145
|
+
*/
|
|
146
|
+
export function parseBlockDocument(input) {
|
|
147
|
+
let value = input;
|
|
148
|
+
if (typeof input === 'string') {
|
|
149
|
+
try {
|
|
150
|
+
value = JSON.parse(input);
|
|
151
|
+
}
|
|
152
|
+
catch (e) {
|
|
153
|
+
throw new BlockDocumentError([`content is not JSON: ${e.message}`]);
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
const problems = validateBlockDocument(value);
|
|
157
|
+
if (problems.length)
|
|
158
|
+
throw new BlockDocumentError(problems);
|
|
159
|
+
return value;
|
|
160
|
+
}
|
|
161
|
+
/** JSON text for the wire. §12.3's 256 KiB ingest cap applies to this string. */
|
|
162
|
+
export function serializeBlockDocument(doc) {
|
|
163
|
+
return JSON.stringify(doc);
|
|
164
|
+
}
|
|
165
|
+
/**
|
|
166
|
+
* A fresh block id.
|
|
167
|
+
*
|
|
168
|
+
* Short on purpose: ids repeat in every anchor and every serialisation, the
|
|
169
|
+
* document has a 256 KiB ceiling, and uniqueness is only required *within one
|
|
170
|
+
* document* (§13.3) rather than globally. 12 hex characters is far more than
|
|
171
|
+
* that needs, and {@link assignMissingBlockIds} checks for a collision anyway
|
|
172
|
+
* rather than trusting the arithmetic.
|
|
173
|
+
*/
|
|
174
|
+
export function newBlockId() {
|
|
175
|
+
// `@noble/hashes`' randomBytes, the same source `nip98.ts` draws its nonce
|
|
176
|
+
// from — one CSPRNG seam for the package rather than a direct `globalThis`
|
|
177
|
+
// reach that types differently under DOM and node libs.
|
|
178
|
+
return bytesToHex(randomBytes(6));
|
|
179
|
+
}
|
|
180
|
+
/**
|
|
181
|
+
* Ids for blocks that have none, **leaving every existing id alone**.
|
|
182
|
+
*
|
|
183
|
+
* This is the id-stability rule (§13.3) as a function. An editor rebuilds a
|
|
184
|
+
* document on every keystroke; if that rebuild re-mints ids, every anchored
|
|
185
|
+
* comment detaches on the next edit and the anchoring feature is silently
|
|
186
|
+
* worthless. Assigning only what is missing makes the safe path the easy one.
|
|
187
|
+
*
|
|
188
|
+
* Returns a new document; the input is not mutated.
|
|
189
|
+
*/
|
|
190
|
+
export function assignMissingBlockIds(doc) {
|
|
191
|
+
const taken = new Set(blockIds(doc));
|
|
192
|
+
const fresh = () => {
|
|
193
|
+
let id = newBlockId();
|
|
194
|
+
while (taken.has(id))
|
|
195
|
+
id = newBlockId();
|
|
196
|
+
taken.add(id);
|
|
197
|
+
return id;
|
|
198
|
+
};
|
|
199
|
+
const walk = (blocks) => blocks.map((block) => {
|
|
200
|
+
const id = typeof block.id === 'string' && block.id !== '' ? block.id : fresh();
|
|
201
|
+
const content = Array.isArray(block.content) && !isInlineRun(block.content)
|
|
202
|
+
? walk(block.content)
|
|
203
|
+
: block.content;
|
|
204
|
+
return { ...block, id, ...(content === undefined ? {} : { content }) };
|
|
205
|
+
});
|
|
206
|
+
return { ...doc, content: walk(doc.content) };
|
|
207
|
+
}
|
|
208
|
+
/** Every block id in document order, nested blocks included. */
|
|
209
|
+
export function blockIds(doc) {
|
|
210
|
+
const out = [];
|
|
211
|
+
const walk = (blocks) => {
|
|
212
|
+
for (const block of blocks) {
|
|
213
|
+
if (typeof block.id === 'string' && block.id !== '')
|
|
214
|
+
out.push(block.id);
|
|
215
|
+
if (Array.isArray(block.content) && !isInlineRun(block.content))
|
|
216
|
+
walk(block.content);
|
|
217
|
+
}
|
|
218
|
+
};
|
|
219
|
+
walk(doc.content);
|
|
220
|
+
return out;
|
|
221
|
+
}
|
|
222
|
+
/** The block an anchor points at, or `undefined` if it is gone. */
|
|
223
|
+
export function findBlock(doc, id) {
|
|
224
|
+
const walk = (blocks) => {
|
|
225
|
+
for (const block of blocks) {
|
|
226
|
+
if (block.id === id)
|
|
227
|
+
return block;
|
|
228
|
+
if (Array.isArray(block.content) && !isInlineRun(block.content)) {
|
|
229
|
+
const hit = walk(block.content);
|
|
230
|
+
if (hit)
|
|
231
|
+
return hit;
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
return undefined;
|
|
235
|
+
};
|
|
236
|
+
return walk(doc.content);
|
|
237
|
+
}
|
|
238
|
+
/**
|
|
239
|
+
* A block's text, including its children's.
|
|
240
|
+
*
|
|
241
|
+
* This is what §13.3 requires a reader to draw for a block type it does not
|
|
242
|
+
* know — *"render that block's inline text rather than dropping it"*. It works
|
|
243
|
+
* on an unknown type for the same reason it works on a known one: it reads the
|
|
244
|
+
* shape, never the name.
|
|
245
|
+
*/
|
|
246
|
+
export function inlineTextOf(block) {
|
|
247
|
+
if (!Array.isArray(block.content))
|
|
248
|
+
return '';
|
|
249
|
+
if (isInlineRun(block.content))
|
|
250
|
+
return inlineNodesToText(block.content);
|
|
251
|
+
return block.content.map(inlineTextOf).join('\n');
|
|
252
|
+
}
|
|
253
|
+
/** The whole document as plain text — previews, search, and the last-resort render. */
|
|
254
|
+
export function documentText(doc) {
|
|
255
|
+
return doc.content.map(inlineTextOf).filter((t) => t !== '').join('\n');
|
|
256
|
+
}
|
|
257
|
+
//# sourceMappingURL=blocks.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"blocks.js","sourceRoot":"","sources":["../src/blocks.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;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C;QACH,CAAC;QACD,OAAO,SAAS,CAAA;IAClB,CAAC,CAAA;IACD,OAAO,IAAI,CAAC,GAAG,CAAC,OAAO,CAAC,CAAA;AAC1B,CAAC;AAED;;;;;;;GAOG;AACH,MAAM,UAAU,YAAY,CAAC,KAAY;IACvC,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,OAAO,CAAC;QAAE,OAAO,EAAE,CAAA;IAC5C,IAAI,WAAW,CAAC,KAAK,CAAC,OAAO,CAAC;QAAE,OAAO,iBAAiB,CAAC,KAAK,CAAC,OAAO,CAAC,CAAA;IACvE,OAAQ,KAAK,CAAC,OAAmB,CAAC,GAAG,CAAC,YAAY,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,CAAA;AAChE,CAAC;AAED,uFAAuF;AACvF,MAAM,UAAU,YAAY,CAAC,GAAkB;IAC7C,OAAO,GAAG,CAAC,OAAO,CAAC,GAAG,CAAC,YAAY,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,KAAK,EAAE,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,CAAA;AACzE,CAAC"}
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The message marker dialect — SPEC §13.1 and §13.2.
|
|
3
|
+
*
|
|
4
|
+
* ## Why content is in a package that "interprets nothing"
|
|
5
|
+
*
|
|
6
|
+
* `index.ts` draws the line at *interpretation*: the fold stays in the apps,
|
|
7
|
+
* because how an app turns events into current truth is where apps are supposed
|
|
8
|
+
* to differ. A body's formatting is on the other side of that line for the same
|
|
9
|
+
* reason the event id is — **it is wire-visible, and two apps disagreeing about
|
|
10
|
+
* it produces a divergence each copy is self-consistent about and nobody
|
|
11
|
+
* notices.** That is not a hypothetical: measured on production 2026-09-02,
|
|
12
|
+
* 261 of 548 published messages carry structure, Peek parses it, Ship renders
|
|
13
|
+
* it literally, and neither is wrong about its own behaviour.
|
|
14
|
+
*
|
|
15
|
+
* Being honest about the usual test: the relay would *not* notice if two apps
|
|
16
|
+
* disagreed here, so §10's question is a poor fit. What makes this belong is the
|
|
17
|
+
* failure shape rather than the arbiter — see RFC 0.4 §14.3, which put the
|
|
18
|
+
* inline layer here, and SPEC §13.1, which made it normative.
|
|
19
|
+
*
|
|
20
|
+
* **What is deliberately not here.** Mentions resolved against a directory,
|
|
21
|
+
* bracket references to an app's own objects, and anything that needs to know
|
|
22
|
+
* what a topic or a file is. Peek's `textParsing.ts` mixes those with the marks;
|
|
23
|
+
* only the marks moved. A `nostr:` reference is a mark and its *token* is in
|
|
24
|
+
* scope, but what a mention does is RIC-2's.
|
|
25
|
+
*/
|
|
26
|
+
/** Marks a run of text can carry. `code` is exclusive — see {@link parseInlineMarks}. */
|
|
27
|
+
export type InlineMark = 'bold' | 'italic' | 'underline' | 'code';
|
|
28
|
+
/** A run of text and the marks on it. */
|
|
29
|
+
export interface InlineMarkSpan {
|
|
30
|
+
text: string;
|
|
31
|
+
bold?: boolean;
|
|
32
|
+
italic?: boolean;
|
|
33
|
+
underline?: boolean;
|
|
34
|
+
code?: boolean;
|
|
35
|
+
}
|
|
36
|
+
/** A block-level run, parsed from line prefixes. */
|
|
37
|
+
export type BodySegment = {
|
|
38
|
+
type: 'text';
|
|
39
|
+
lines: string[];
|
|
40
|
+
} | {
|
|
41
|
+
type: 'bullet';
|
|
42
|
+
items: string[];
|
|
43
|
+
} | {
|
|
44
|
+
type: 'numbered';
|
|
45
|
+
items: string[];
|
|
46
|
+
} | {
|
|
47
|
+
type: 'heading';
|
|
48
|
+
level: 1 | 2;
|
|
49
|
+
text: string;
|
|
50
|
+
} | {
|
|
51
|
+
type: 'quote';
|
|
52
|
+
lines: string[];
|
|
53
|
+
} | {
|
|
54
|
+
type: 'code';
|
|
55
|
+
language?: string;
|
|
56
|
+
lines: string[];
|
|
57
|
+
};
|
|
58
|
+
/**
|
|
59
|
+
* Parse one plain-text run into styled spans.
|
|
60
|
+
*
|
|
61
|
+
* Split mentions and app-specific references out first — this function knows
|
|
62
|
+
* about marks and nothing else.
|
|
63
|
+
*/
|
|
64
|
+
export declare function parseInlineMarks(text: string): InlineMarkSpan[];
|
|
65
|
+
/**
|
|
66
|
+
* Wrap a run of text in the markers for the marks it carries.
|
|
67
|
+
*
|
|
68
|
+
* Edge whitespace is hoisted outside the markers, because `**word **` would not
|
|
69
|
+
* parse back under rule 1 — so bolding "word " round-trips as `**word** `.
|
|
70
|
+
*
|
|
71
|
+
* `code` wins alone, for the same reason the parser never emits it combined.
|
|
72
|
+
*/
|
|
73
|
+
export declare function wrapInlineMarks(text: string, markNames: ReadonlySet<string>): string;
|
|
74
|
+
/**
|
|
75
|
+
* Split a body into block segments.
|
|
76
|
+
*
|
|
77
|
+
* Blank lines split runs of plain text into separate `text` segments. `# ` and
|
|
78
|
+
* `## ` become headings; three or more `#`s, and a `#123` reference with no
|
|
79
|
+
* space, stay plain text.
|
|
80
|
+
*/
|
|
81
|
+
export declare function parseBodySegments(body: string): BodySegment[];
|
|
82
|
+
export declare function stripInlineFormatting(text: string): string;
|
|
83
|
+
/** A mark on an inline run, in the JSON encoding. */
|
|
84
|
+
export type InlineMarkNode = {
|
|
85
|
+
type: InlineMark;
|
|
86
|
+
};
|
|
87
|
+
/** One run of text and its marks — §13.3's `{"type":"text","text":…,"marks":[…]}`. */
|
|
88
|
+
export interface InlineTextNode {
|
|
89
|
+
type: 'text';
|
|
90
|
+
text: string;
|
|
91
|
+
marks?: InlineMarkNode[];
|
|
92
|
+
}
|
|
93
|
+
/** Marker text → inline JSON nodes. */
|
|
94
|
+
export declare function markersToInlineNodes(text: string): InlineTextNode[];
|
|
95
|
+
/**
|
|
96
|
+
* Inline JSON nodes → marker text.
|
|
97
|
+
*
|
|
98
|
+
* The inverse of {@link markersToInlineNodes} for every mark the dialect can
|
|
99
|
+
* spell, which is all four of them. Whitespace at a run's edges is hoisted
|
|
100
|
+
* outside the markers by `wrapInlineMarks`, because §13.2 rule 1 would not
|
|
101
|
+
* parse it back otherwise — so this round-trips the *marks on each character*
|
|
102
|
+
* rather than the span boundaries. See the corpus tests.
|
|
103
|
+
*/
|
|
104
|
+
export declare function inlineNodesToMarkers(nodes: readonly InlineTextNode[]): string;
|
|
105
|
+
/** The plain text of a run of inline nodes, marks discarded. */
|
|
106
|
+
export declare function inlineNodesToText(nodes: readonly InlineTextNode[]): string;
|
|
107
|
+
//# sourceMappingURL=content.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"content.d.ts","sourceRoot":"","sources":["../src/content.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AAEH,yFAAyF;AACzF,MAAM,MAAM,UAAU,GAAG,MAAM,GAAG,QAAQ,GAAG,WAAW,GAAG,MAAM,CAAA;AAEjE,yCAAyC;AACzC,MAAM,WAAW,cAAc;IAC7B,IAAI,EAAE,MAAM,CAAA;IACZ,IAAI,CAAC,EAAE,OAAO,CAAA;IACd,MAAM,CAAC,EAAE,OAAO,CAAA;IAChB,SAAS,CAAC,EAAE,OAAO,CAAA;IACnB,IAAI,CAAC,EAAE,OAAO,CAAA;CACf;AAED,oDAAoD;AACpD,MAAM,MAAM,WAAW,GACnB;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,KAAK,EAAE,MAAM,EAAE,CAAA;CAAE,GACjC;IAAE,IAAI,EAAE,QAAQ,CAAC;IAAC,KAAK,EAAE,MAAM,EAAE,CAAA;CAAE,GACnC;IAAE,IAAI,EAAE,UAAU,CAAC;IAAC,KAAK,EAAE,MAAM,EAAE,CAAA;CAAE,GACrC;IAAE,IAAI,EAAE,SAAS,CAAC;IAAC,KAAK,EAAE,CAAC,GAAG,CAAC,CAAC;IAAC,IAAI,EAAE,MAAM,CAAA;CAAE,GAC/C;IAAE,IAAI,EAAE,OAAO,CAAC;IAAC,KAAK,EAAE,MAAM,EAAE,CAAA;CAAE,GAClC;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,QAAQ,CAA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