yrby 0.5.0 → 0.6.1

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 (32) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +74 -1
  3. data/Cargo.lock +29 -0
  4. data/README.md +286 -30
  5. data/ext/yrby/Cargo.toml +3 -0
  6. data/ext/yrby/crates/html-core/Cargo.toml +15 -0
  7. data/ext/yrby/crates/html-core/src/lib.rs +535 -0
  8. data/ext/yrby/crates/lexical-html/Cargo.toml +16 -0
  9. data/ext/yrby/{src/lexical_html.rs → crates/lexical-html/src/lib.rs} +615 -288
  10. data/ext/yrby/crates/prosemirror-html/Cargo.toml +16 -0
  11. data/ext/yrby/crates/prosemirror-html/src/lib.rs +1369 -0
  12. data/ext/yrby/src/lib.rs +208 -78
  13. data/ext/yrby/src/read.rs +3 -3
  14. data/ext/yrby/target/debug/build/clang-sys-e3ae45bd384f74c3/out/common.rs +355 -0
  15. data/ext/yrby/target/debug/build/clang-sys-e3ae45bd384f74c3/out/dynamic.rs +276 -0
  16. data/ext/yrby/target/debug/build/clang-sys-e3ae45bd384f74c3/out/macros.rs +49 -0
  17. data/ext/yrby/target/debug/build/rb-sys-4407948463231c4f/out/bindings-0.9.128-mri-arm64-darwin23-3.4.7.rs +8934 -0
  18. data/ext/yrby/target/debug/build/rb-sys-dbeea42737529c2d/out/bindings-0.9.128-mri-arm64-darwin23-3.4.7.rs +8934 -0
  19. data/ext/yrby/target/debug/build/serde-58ea0ee887cc2602/out/private.rs +6 -0
  20. data/ext/yrby/target/debug/build/serde_core-41f407c21c1f205e/out/private.rs +5 -0
  21. data/ext/yrby/target/debug/build/thiserror-0f1416a82ff26f22/out/private.rs +5 -0
  22. data/lib/generators/yrby/install/install_generator.rb +44 -0
  23. data/lib/generators/yrby/install/templates/document_channel.rb +36 -0
  24. data/lib/generators/yrby/tables/tables_generator.rb +41 -0
  25. data/lib/generators/yrby/tables/templates/create_y_tables.rb +24 -0
  26. data/lib/y/lexxy.rb +121 -0
  27. data/lib/y/rendering.rb +282 -0
  28. data/lib/y/tiptap.rb +63 -0
  29. data/lib/y/version.rb +1 -1
  30. data/lib/y.rb +4 -0
  31. metadata +23 -4
  32. data/ext/yrby/src/prosemirror_html.rs +0 -896
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 3c53c83185ba046ddbcb6b232953af9aa2f52f883120aff8bb2fed8867f247df
4
- data.tar.gz: 6a09a5541cca754d6ff838feb82838ce881d8fea144fed692031f7c957c9850b
3
+ metadata.gz: ba28bef094ec97908a5ce4b01a61aedbbbcd49585c88a47acfbcfe0dab02c5be
4
+ data.tar.gz: dae39a894e92836681c0aa73dc54201aecf1d3fa68eaac38496e420579d760b3
5
5
  SHA512:
6
- metadata.gz: d44fb5bb2a723b6a0dcf7ac6fd3bc248bd58f1401fe2e62009a214148f4a7b1ef1586d0d25585b39d291667eff04d6c66e911c7cdb5dec4968a0f3192b70a85c
7
- data.tar.gz: 74da78ebc395e44cbdc9f32dcc51a239b7ea312de24f5ac64ec47db41bc784b47a53425afc83b948d36c9bf1b11922a3f9f3aa6615dcbac72a77759abe046ccc
6
+ metadata.gz: de4dedd7b4eb4e14e343b83b20007587cd129cbc0f42ff03f21118043ebced3853b9a2e2c51e77688ba1b8665e058422e24d1cc18d7acfd85fd6c99e435b43b9
7
+ data.tar.gz: 93291be948f34edc50c98266e554bd41a2beb7f8e74b1aeab87b19dc41efed425603f0634cefd117f24077b7d4fecf967b3ff4e40614b2276f2ccb40e85b5dc6
data/CHANGELOG.md CHANGED
@@ -4,7 +4,80 @@ All notable changes to this project are documented here. The format is based on
4
4
  [Keep a Changelog](https://keepachangelog.com/en/1.1.0/), and this project aims
5
5
  to follow [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
6
6
 
7
- ## [Unreleased]
7
+ ## [0.6.1] - 2026-08-04
8
+
9
+ ### Fixed
10
+
11
+ - `Y::Lexxy` emits the attachment tag the node was created with instead of
12
+ a hardcoded `<action-text-attachment>`. Lexxy makes the tag configurable
13
+ (`Lexxy.configure`'s `attachmentTagName`, paired with
14
+ `ActionText::Attachment.tag_name` in Rails), and each attachment node
15
+ stores its tag, so a custom-tag app's rendered HTML now matches its
16
+ editor. A stored value that doesn't look like a tag name falls back to
17
+ the default; documents from before the tag was stored render unchanged.
18
+
19
+ ## [0.6.0] - 2026-07-11
20
+
21
+ ### Added
22
+
23
+ - **Custom render rules for `Y::Lexical` and `Y::ProseMirror`.** Both
24
+ renderers now take a block registering rules per node type —
25
+ `rules.node "callout", tag: "aside"` for markup-as-data, a Ruby block for
26
+ logic — plus `nodes:`/`marks:` keywords as the equivalent data form, to
27
+ render node types the pinned schemas don't know or to override how a
28
+ built-in renders.
29
+ Declarative rules (`tag`/`attrs`/`text`/`contains`, with templates mixing
30
+ literals and attribute references) render natively at full speed. Callback
31
+ rules run a Ruby block per node, receiving its type, attributes,
32
+ already-rendered children, and `child_types` (its element/block children by
33
+ type — the structural facts behind gallery counts and nested-list classes).
34
+ The block runs after the document read has finished, never while the doc is
35
+ locked, so it can safely read or write the same doc. Blocks are proven
36
+ sufficient for whole schemas: the gem's own editor schemas (`Y::Lexxy`,
37
+ `Y::Tiptap`) ship through this API, and the fixture tests hold their
38
+ output byte-identical to a live editor's. With no callback rules the
39
+ render path is unchanged, byte for byte. See "Custom nodes and marks" in
40
+ the README.
41
+ - **`Y::Lexical#node_types` / `Y::ProseMirror#node_types` — schema
42
+ discovery.** Ask a real document which node types it holds and what they
43
+ look like: counts, attribute names as stored, child types, whether text
44
+ runs appear, and whether a builtin or one of your rules already handles
45
+ each ("handled" nil marks what still needs a rule). Editors store names
46
+ you'd never guess; this is how you find them.
47
+ - `Y::RenderRules.escape_text` / `escape_attr` — the exact escaping the
48
+ native renderers use, for blocks that build markup from stored values
49
+ (ERB's `html_escape` also rewrites apostrophes, which breaks byte parity
50
+ with editor output).
51
+
52
+ ### Changed
53
+
54
+ - **Lexical rendering is now two classes: `Y::Lexical` (core Lexical) and
55
+ `Y::Lexxy` (core plus the Lexxy schema as render rules).** Stock Lexical
56
+ has no canonical serializer, so the editor-specific class carries the
57
+ editor's name — `Y::Lexxy.new(doc).to_html` is the byte-parity call for
58
+ Lexxy/Rails apps, and `Y::Lexical` is the base any other Lexical editor
59
+ extends with its own rules. The
60
+ native side renders core structure — paragraphs, headings, quotes, code,
61
+ lists, tables, links, the full text-format model. Lexxy's own node types
62
+ (attachments, galleries, `early_escape_code`, `horizontal_divider`) and its
63
+ decorations of core nodes (the table figure wrapper, header-cell styling,
64
+ the nested-list-item class) are rules applied beneath the app's, on the
65
+ same extension API — the gem's Lexxy support is the API's first consumer.
66
+ Output is unchanged: the fixture tests still hold `to_html` byte-identical
67
+ to a live editor's serialized value, now through the extension path. An
68
+ unknown Lexical container also degrades better: its block children render
69
+ without an invented wrapper instead of being dropped.
70
+ - **ProseMirror rendering gets the same split: `Y::ProseMirror` (core
71
+ ProseMirror) and `Y::Tiptap` (core plus Tiptap's extension nodes as render
72
+ rules).** `Y::Tiptap.new(doc).to_html` is the byte-parity call for Tiptap
73
+ apps. The native side renders prosemirror-schema-basic plus the
74
+ prosemirror-tables family; Tiptap's extension nodes — task lists, mentions,
75
+ the details family — are `Y::Tiptap::NODES` rules. Marks stay native in the
76
+ base class: mark rendering (nesting order, `textStyle` CSS, `code`
77
+ exclusivity) runs through text-run machinery node rules don't reach, so
78
+ `Y::ProseMirror` still renders Tiptap's full mark set and `rules.mark`
79
+ overrides individual marks. Output through `Y::Tiptap` is unchanged, held
80
+ byte-identical to a live editor's `getHTML()` by the fixture tests.
8
81
 
9
82
  ## [0.5.0] - 2026-07-08
10
83
 
data/Cargo.lock CHANGED
@@ -617,6 +617,9 @@ dependencies = [
617
617
  "rb-sys-env 0.1.2",
618
618
  "serde_json",
619
619
  "yrs",
620
+ "yrs-html-core",
621
+ "yrs-lexical-html",
622
+ "yrs-prosemirror-html",
620
623
  ]
621
624
 
622
625
  [[package]]
@@ -637,6 +640,32 @@ dependencies = [
637
640
  "thiserror",
638
641
  ]
639
642
 
643
+ [[package]]
644
+ name = "yrs-html-core"
645
+ version = "0.1.0"
646
+ dependencies = [
647
+ "serde_json",
648
+ "yrs",
649
+ ]
650
+
651
+ [[package]]
652
+ name = "yrs-lexical-html"
653
+ version = "0.1.0"
654
+ dependencies = [
655
+ "serde_json",
656
+ "yrs",
657
+ "yrs-html-core",
658
+ ]
659
+
660
+ [[package]]
661
+ name = "yrs-prosemirror-html"
662
+ version = "0.1.0"
663
+ dependencies = [
664
+ "serde_json",
665
+ "yrs",
666
+ "yrs-html-core",
667
+ ]
668
+
640
669
  [[package]]
641
670
  name = "zmij"
642
671
  version = "1.0.14"
data/README.md CHANGED
@@ -5,14 +5,16 @@
5
5
  Collaborative editing for Rails, backed by [y-crdt](https://github.com/y-crdt/y-crdt)
6
6
  (the Rust library behind Y.js). Your Rails server speaks the y-websocket sync
7
7
  protocol directly, so there's no separate Node process hosting the Y.js
8
- documents.
8
+ documents. Pronounced "yer-bee".
9
+
10
+ ![Two people typing on separate lines of the same document, each keystroke synced through a Rails server, seen from a third browser with labeled carets](docs/images/collab.gif)
9
11
 
10
12
  ```ruby
11
13
  class DocumentChannel < ApplicationCable::Channel
12
- include Y::ActionCable::Sync
14
+ include Y::ActionCable
13
15
 
14
- on_load { |key| MyStore.load(key) }
15
- on_change { |key, update| MyStore.append(key, update) }
16
+ on_load { |key| Y::Document.load_state(key) }
17
+ on_change { |key, update| Y::Document.append(key, update) }
16
18
 
17
19
  def subscribed = sync_subscribed(params[:id])
18
20
  def receive(data) = sync_receive(data, params[:id])
@@ -59,7 +61,7 @@ and `Doc#read_map` reconstruct it server-side, in Ruby.
59
61
  The surface is intentionally small, but the focus is durability, resiliency, delivery
60
62
  guarantees, correctness, and thread safety.
61
63
 
62
- Towards that goal, `yrby` adds capabilities that stand out even in the Yjs ecosystem:
64
+ Towards that goal, `yrby` adds opinionated defaults on top of normal Yjs syncing:
63
65
 
64
66
  - Built-in update acknowledgement: the `ActionCableProvider` in `yrby-client` will continue to
65
67
  send updates until an ack is received from the server. [`yrby-actioncable`](https://rubygems.org/gems/yrby-actioncable)
@@ -101,8 +103,9 @@ Issues and PRs are welcome.
101
103
  # Core CRDT + protocol primitives:
102
104
  gem "yrby"
103
105
 
104
- # For the Rails/ActionCable server concern (Y::ActionCable::Sync):
105
- gem "yrby-actioncable"
106
+ # For the Rails side (the sync channel, document models, the generator).
107
+ # Formerly yrby-actioncable; that name stops at 0.3.1.
108
+ gem "yrby-rails"
106
109
  ```
107
110
 
108
111
  Requires Ruby 3.4 or newer. The release workflow builds precompiled gems for
@@ -129,6 +132,27 @@ The rest of the dev setup, plus the demo, is in [CONTRIBUTING.md](CONTRIBUTING.m
129
132
  and the test/load suites.
130
133
  - [CHANGELOG.md](CHANGELOG.md) and [CONTRIBUTING.md](CONTRIBUTING.md).
131
134
 
135
+ ## Editors
136
+
137
+ yrby syncs opaque Yjs updates, so it works with any editor that has a Yjs
138
+ binding. The demo app runs four, and CI drives each one in real Chrome:
139
+ concurrent typing with every keystroke accounted for, remote cursors,
140
+ local-only undo, and byte parity between the server-side renderers and the
141
+ editor's own serializer. Each page is a working integration to copy from:
142
+
143
+ | Editor | Yjs binding | Demo code |
144
+ |---|---|---|
145
+ | [Tiptap](https://tiptap.dev) (v2) | `@tiptap/extension-collaboration` | [`app.js`](examples/actioncable-demo/frontend/src/app.js) |
146
+ | [Lexxy](https://github.com/basecamp/lexxy) (Lexical) | [`lexxy-realtime`](https://www.npmjs.com/package/lexxy-realtime) | [`lexxy.js`](examples/actioncable-demo/frontend/src/lexxy.js) |
147
+ | [Rhino Editor](https://github.com/KonnorRogers/rhino-editor) (Tiptap 3) | `@tiptap/extension-collaboration` + `-caret` | [`rhino.js`](examples/actioncable-demo/frontend/src/rhino.js) |
148
+ | [CodeMirror 6](https://codemirror.net) | `y-codemirror.next` | [`codemirror.js`](examples/actioncable-demo/frontend/src/codemirror.js) |
149
+
150
+ The demo also syncs plain Yjs shapes with no editor at all — a whiteboard
151
+ on a `Y.Map`, a kanban board on a `Y.Array`, a co-filled form — over the
152
+ same channel. The demo README's "Using this in your own app" section has
153
+ the integration recipe, and its `NoteMaterializer` shows how to render a
154
+ document to ActionText server-side with `Y::Tiptap` or `Y::Lexxy`.
155
+
132
156
  ## Usage
133
157
 
134
158
  ### Doc (Low-Level Document Sync)
@@ -188,18 +212,20 @@ guarantees keep serving safe:
188
212
 
189
213
  ### Rendering to HTML
190
214
 
191
- Two schema-pinned renderers turn a collaborative document into HTML on the
192
- server, with no Node process or headless editor: `Y::ProseMirror` for
193
- ProseMirror/Tiptap documents and `Y::Lexical` for
194
- [Lexxy](https://github.com/basecamp/lexxy) (Lexical) documents. Each returns
195
- `nil` for a root that belongs to the other schema.
215
+ Schema-pinned renderers turn a collaborative document into HTML on the
216
+ server, with no Node process or headless editor. Each is an editor-specific
217
+ class (byte-for-byte with that editor's own serializer) built on a core base
218
+ any other editor extends with rules: `Y::Tiptap` on `Y::ProseMirror` for
219
+ ProseMirror documents, and `Y::Lexxy` (the
220
+ [Lexxy](https://github.com/basecamp/lexxy) editor) on `Y::Lexical`. Each
221
+ returns `nil` for a root that belongs to the other schema.
196
222
 
197
- #### `Y::ProseMirror`
223
+ #### `Y::Tiptap` (and `Y::ProseMirror`, its base)
198
224
 
199
225
  ```ruby
200
- prosemirror = Y::ProseMirror.new(doc)
201
- prosemirror.to_html # the "default" fragment (Tiptap's default root)
202
- prosemirror.to_html("content") # or another XML root
226
+ tiptap = Y::Tiptap.new(doc)
227
+ tiptap.to_html # the "default" fragment (Tiptap's default root)
228
+ tiptap.to_html("content") # or another XML root
203
229
  ```
204
230
 
205
231
  The output matches Tiptap's own `getHTML()`, checked byte-for-byte in the tests
@@ -214,17 +240,30 @@ tables, text styles (color, font family), and every text mark. A table renders
214
240
  as semantic `<table><tbody>`, without the column-width styling Tiptap's editor
215
241
  view adds.
216
242
 
217
- #### `Y::Lexical`
243
+ The support is layered like the Lexical side: `Y::ProseMirror` covers core
244
+ ProseMirror natively — prosemirror-schema-basic plus the prosemirror-tables
245
+ family — and Tiptap's extension nodes (task lists, mentions, the details
246
+ family) are `Y::Tiptap`'s rule set (`Y::Tiptap::NODES`), built on the
247
+ extension API below. Marks stay in the base: mark rendering (nesting order,
248
+ `textStyle` CSS, `code` exclusivity) runs through native text-run machinery
249
+ that node rules don't reach, so `Y::ProseMirror` renders Tiptap's mark set
250
+ as-is and `rules.mark` overrides individual marks.
251
+
252
+ #### `Y::Lexxy` (and `Y::Lexical`, its base)
218
253
 
219
254
  ```ruby
220
- lexical = Y::Lexical.new(doc)
221
- lexical.to_html # the "root" fragment (Lexical's default root name)
222
- lexical.to_html("notepad") # or another XML root
255
+ lexxy = Y::Lexxy.new(doc)
256
+ lexxy.to_html # the "root" fragment (Lexical's default root name)
257
+ lexxy.to_html("notepad") # or another XML root
223
258
  ```
224
259
 
225
260
  The HTML is identical to what a `lexxy-editor` submits to Rails (its `value`).
226
261
  The tests check this byte-for-byte against a document captured from a real
227
- editor.
262
+ editor. Stock Lexical has no canonical serializer — every editor configures
263
+ its own — so the editor-specific class carries the editor's name, and
264
+ `Y::Lexical` is the core-Lexical base: paragraphs, headings, quotes, code,
265
+ lists, tables, links, and the full text-format model, for any other Lexical
266
+ editor to extend with rules.
228
267
 
229
268
  It handles the whole Lexxy 0.9.x node set: paragraphs, headings, every text
230
269
  format and their combinations, links, the four list types and nesting,
@@ -233,8 +272,178 @@ header cells, image galleries, and ActionText attachments (uploads and
233
272
  mentions both emit `<action-text-attachment>` elements that ActionText can
234
273
  re-render).
235
274
 
236
- In both renderers an unknown node keeps its content — text falls back to a
237
- plain paragraph rather than disappearing.
275
+ Internally that support is layered: `Y::Lexical` covers core Lexical
276
+ structure natively, and everything Lexxy adds — its node types (attachments,
277
+ galleries) and its decorations of core nodes (the table wrapper, header-cell
278
+ styling, nested-list classes) — is `Y::Lexxy`'s rule set
279
+ (`Y::Lexxy::NODES`), built on the extension API below. The gem's own Lexxy
280
+ support is the API's first consumer: an app rule for one of those types
281
+ simply replaces it.
282
+
283
+ In both renderers an unknown node keeps its content — text and nested blocks
284
+ fall back to readable markup rather than disappearing.
285
+
286
+ #### Custom nodes and marks
287
+
288
+ The built-in schemas are pinned to what Tiptap and Lexxy ship, but apps add
289
+ their own node types. Both renderers take rules for them. A rule is checked
290
+ before the built-in schema, so it can add a node type or replace how a
291
+ built-in renders.
292
+
293
+ Rules register in a block — one `rules.node` call per type. A declarative
294
+ rule is markup as data, rendered natively:
295
+
296
+ ```ruby
297
+ tiptap = Y::Tiptap.new(doc) do |rules|
298
+ rules.node "callout", tag: "aside",
299
+ attrs: { "class" => ["callout callout--", :kind] },
300
+ contains: :blocks
301
+ end
302
+ ```
303
+
304
+ `tag` names the element. `attrs` values are templates: a string is a literal,
305
+ a symbol reads that attribute off the node, an array concatenates both kinds;
306
+ an attribute that resolves empty is left out. `text` (same template form)
307
+ emits literal text content. `contains` declares what lives inside the node — `:inline` (formatted text,
308
+ the default), `:blocks` (child block nodes — a container), or `:none` (a
309
+ leaf). `void: true` skips the closing tag.
310
+
311
+ You don't have to guess any of those names or shapes. Editors store types
312
+ and attributes under names you'd never predict (Rhino's strike mark is
313
+ `rhino-strike`; Lexical prefixes its own props `__`), so ask a real
314
+ document instead — make one in your editor using your custom node, then:
315
+
316
+ ```ruby
317
+ Y::Tiptap.new(doc).node_types
318
+ # => { "callout" => { "count" => 2, "attrs" => ["kind"],
319
+ # "children" => ["paragraph"], "text" => false,
320
+ # "handled" => nil },
321
+ # "paragraph" => { ..., "handled" => "builtin" } }
322
+ ```
323
+
324
+ `handled` nil marks the types that still need a rule; `attrs` are the stored
325
+ names your templates and blocks will read; `children` plus `text` is how you
326
+ pick `contains:` (child block types → `:blocks`; text → `:inline`).
327
+
328
+ When markup-as-data isn't enough, give the node a block:
329
+
330
+ ```ruby
331
+ lexical = Y::Lexical.new(doc) do |rules|
332
+ rules.node "video_embed" do |node|
333
+ src = ERB::Util.html_escape(node.attrs["__src"])
334
+ %(<video controls src="#{src}"></video>)
335
+ end
336
+ end
337
+ ```
338
+
339
+ The block gets the node's type, its stored attributes, `node.content` — the
340
+ children, already rendered to HTML — and `node.child_types`, the node's
341
+ element/block children by type, in document order. `child_types` answers the
342
+ structural questions attributes can't: how many images a gallery holds, or
343
+ whether a list item carries a nested list. Whatever the block returns is
344
+ spliced into the output as-is: it's trusted HTML, so escape any values you
345
+ interpolate. To set the content mode for a callback, give the node both —
346
+ `rules.node "embed", contains: :blocks do |node| ... end`.
347
+
348
+ Callbacks never run while the document is locked. The render finishes first
349
+ (inside one read transaction, GVL released), then the blocks run and their
350
+ output is spliced in — so a callback can safely read or even write the same
351
+ doc. With no callback rules, `to_html` skips the splicing entirely.
352
+
353
+ Blocks are the escape hatch for everything the declarative form can't say,
354
+ and they're proven sufficient: `Y::Lexxy` and `Y::Tiptap` are themselves
355
+ built on this API (`lib/y/lexxy.rb`, `lib/y/tiptap.rb`) — simple nodes as
356
+ declarative hashes, everything with logic as plain methods mapped by node
357
+ type (a `Method` responds to `call` like any lambda) — and the fixture tests
358
+ hold their output byte-identical to a live editor's.
359
+
360
+ The ProseMirror side also takes custom marks:
361
+
362
+ ```ruby
363
+ tiptap = Y::Tiptap.new(doc) do |rules|
364
+ rules.mark "comment", tag: "span", attrs: { "data-comment-id" => :id }
365
+ end
366
+ ```
367
+
368
+ Symbol refs resolve against the mark's own attributes. A custom mark wraps
369
+ outside every built-in mark; several on one run nest alphabetically. A rule
370
+ for a built-in mark name (`"bold"`) replaces its built-in tag.
371
+
372
+ ##### Worked examples
373
+
374
+ A video-embed node from an app's Tiptap extension — a type the pinned schema
375
+ has never heard of:
376
+
377
+ ```ruby
378
+ tiptap = Y::Tiptap.new(doc) do |rules|
379
+ rules.node "videoEmbed" do |node|
380
+ src = ERB::Util.html_escape(node.attrs["src"])
381
+ title = ERB::Util.html_escape(node.attrs["title"] || "Video")
382
+ %(<figure class="video"><iframe src="#{src}" title="#{title}" allowfullscreen></iframe></figure>)
383
+ end
384
+ end
385
+ ```
386
+
387
+ Resolving mentions against the database. Blocks run after the document read
388
+ has finished, so hitting ActiveRecord (or the doc itself) inside one is safe:
389
+
390
+ ```ruby
391
+ tiptap = Y::Tiptap.new(doc) do |rules|
392
+ rules.node "mention" do |node|
393
+ user = User.find_by(id: node.attrs["id"])
394
+ next "<span>@unknown</span>" unless user
395
+
396
+ %(<a class="mention" href="/users/#{user.id}">@#{ERB::Util.html_escape(user.handle)}</a>)
397
+ end
398
+ end
399
+ ```
400
+
401
+ Overriding a shipped rule — rendering Lexxy uploads as real image markup
402
+ instead of the `<action-text-attachment>` elements ActionText re-renders:
403
+
404
+ ```ruby
405
+ lexxy = Y::Lexxy.new(doc) do |rules|
406
+ rules.node "action_text_attachment" do |node|
407
+ src = ERB::Util.html_escape(node.attrs["src"])
408
+ alt = ERB::Util.html_escape(node.attrs["altText"].to_s)
409
+ caption = node.attrs["caption"].to_s
410
+ html = %(<img src="#{src}" alt="#{alt}" loading="lazy">)
411
+ html += "<figcaption>#{ERB::Util.html_escape(caption)}</figcaption>" unless caption.empty?
412
+ "<figure>#{html}</figure>"
413
+ end
414
+ end
415
+ ```
416
+
417
+ Markup that depends on structure — `node.child_types` lists the node's
418
+ element/block children in document order, so a layout container can size
419
+ itself by its column count while the columns themselves stay declarative:
420
+
421
+ ```ruby
422
+ tiptap = Y::Tiptap.new(doc) do |rules|
423
+ rules.node "columns", contains: :blocks do |node|
424
+ %(<div class="columns columns--#{node.child_types.length}">#{node.content}</div>)
425
+ end
426
+ rules.node "column", tag: "div", attrs: { "class" => "column" }, contains: :blocks
427
+ end
428
+ ```
429
+
430
+ Content-aware overrides — dropping the empty paragraphs an editor keeps
431
+ around the cursor, since `node.content` arrives already rendered:
432
+
433
+ ```ruby
434
+ lexical = Y::Lexical.new(doc) do |rules|
435
+ rules.node "paragraph" do |node|
436
+ node.content.empty? ? "" : "<p>#{node.content}</p>"
437
+ end
438
+ end
439
+ ```
440
+
441
+ For a larger reference, the gem's own editor schemas ship this way — see
442
+ `Y::Lexxy::NODES` in `lib/y/lexxy.rb` (declarative hashes for the simple
443
+ nodes, a plain method per node that needs logic — galleries, list items,
444
+ header cells, both attachment types — mapped with `method(:name)`) and
445
+ `Y::Tiptap::NODES` in `lib/y/tiptap.rb` (task lists, mentions, the details
446
+ family).
238
447
 
239
448
  ### Protocol codec (module functions)
240
449
 
@@ -251,25 +460,72 @@ Y.wrap_update(update_bytes) # => wrap a raw doc update as a sync Update frame
251
460
 
252
461
  ### ActionCable Integration
253
462
 
254
- `Y::ActionCable::Sync` (from the `yrby-actioncable` gem) is a channel
255
- concern that implements the full y-websocket protocol (document sync +
256
- awareness/presence) over ActionCable:
463
+ In a Rails app, one generator creates the channel and the migration:
464
+
465
+ ```bash
466
+ bin/rails generate yrby:install
467
+ bin/rails db:migrate
468
+ ```
469
+
470
+ The models ship in the gem, the way Action Text owns
471
+ `ActionText::RichText`:
472
+
473
+ - **`Y::Document`** — one row per document, addressed two ways: by `key`
474
+ (what a channel addresses — one opaque, unique string, sometimes
475
+ app-supplied, never parsed) and, optionally, by polymorphic `record` +
476
+ `name` (which model attribute it backs; `name` is the attribute name,
477
+ `"body"` — one document per attribute per record, the
478
+ ActionText::RichText scheme). Key-only documents leave the binding nil.
479
+ Either side can arrive first: `Y::Document.for(record, name)` finds or
480
+ creates the binding, derives a readable key (`post/1/body`), and adopts
481
+ a key-only row already holding that key, so a channel writing first and
482
+ a binding created later converge on one document. The row also holds
483
+ the merged `state` snapshot — CRDT state only; derived data (rendered
484
+ HTML, search text) is the application's job, typically in the channel's
485
+ on_change. `.load_state(key)` / `.append(key, update)` are the store
486
+ calls the generated channel uses.
487
+ - **`Y::DocumentUpdate`** — the uncompacted tail: one delta per row,
488
+ compacted into `state` and deleted once the tail reaches `compact_every`
489
+ (default 64). Loading reads the snapshot plus the current tail; an
490
+ empty tail returns `state` directly. Compaction serializes on a
491
+ per-document row lock and skips causally-gapped rows — they're
492
+ quarantined until they heal rather than compacted into state or
493
+ deleted. Destroying a document deletes its updates with it.
494
+
495
+ The migration creates `y_documents` and `y_document_updates`. To rename
496
+ them, edit the generated migration and point `Y::Document.table_name` /
497
+ `Y::DocumentUpdate.table_name` at the new names in an initializer.
498
+
499
+ Storage is swappable: the channel only needs `on_load` and `on_change`
500
+ answered, and they can point at anything.
501
+
502
+ `include Y::ActionCable` (from the `yrby-rails` gem) is the channel
503
+ integration: the y-websocket protocol (document sync +
504
+ awareness/presence) over ActionCable. (`include Y::ActionCable::Sync`
505
+ keeps working and has the same effect.)
257
506
 
258
507
  ```ruby
259
508
  # app/channels/document_channel.rb
260
509
  class DocumentChannel < ApplicationCable::Channel
261
- include Y::ActionCable::Sync
510
+ include Y::ActionCable
262
511
 
263
- on_load { |key| MyStore.load(key) } # source of truth
264
- on_change { |key, update| MyStore.append(key, update) } # durable record
512
+ on_load { |key| Y::Document.load_state(key) } # rebuild from storage
513
+ on_change { |key, update| Y::Document.append(key, update) } # record, then broadcast
265
514
 
266
515
  def subscribed
516
+ return reject unless authorized?(params[:id])
517
+
267
518
  sync_subscribed params[:id]
268
519
  end
269
520
 
270
521
  def receive(data)
271
522
  sync_receive(data, params[:id])
272
523
  end
524
+
525
+ private
526
+
527
+ # Everyone is denied until you wire this to your app's auth.
528
+ def authorized?(_document_key) = false
273
529
  end
274
530
  ```
275
531
 
@@ -365,7 +621,7 @@ It is up to you to durably record it:
365
621
 
366
622
  ```ruby
367
623
  class DocumentChannel < ApplicationCable::Channel
368
- include Y::ActionCable::Sync
624
+ include Y::ActionCable
369
625
 
370
626
  # ...
371
627
 
data/ext/yrby/Cargo.toml CHANGED
@@ -14,6 +14,9 @@ magnus = "0.8"
14
14
  rb-sys = "0.9"
15
15
  yrs = { version = "0.27", features = ["sync"] }
16
16
  serde_json = "1.0"
17
+ yrs-html-core = { path = "crates/html-core" }
18
+ yrs-lexical-html = { path = "crates/lexical-html" }
19
+ yrs-prosemirror-html = { path = "crates/prosemirror-html" }
17
20
 
18
21
  [dev-dependencies]
19
22
  rb-sys-env = "0.1"
@@ -0,0 +1,15 @@
1
+ [package]
2
+ name = "yrs-html-core"
3
+ description = "Internal core for the yrs-*-html renderer crates; depend on those instead"
4
+ repository = "https://github.com/jpcamara/yrby"
5
+ readme = "README.md"
6
+ version = "0.1.0"
7
+ edition = "2024"
8
+ rust-version = "1.85"
9
+ authors = ["JP Camara <johnpcamara@gmail.com>"]
10
+ license = "MIT"
11
+ publish = false
12
+
13
+ [dependencies]
14
+ yrs = { version = "0.27", features = ["sync"] }
15
+ serde_json = "1.0"