docspec-blocknote-writer
Converts a DocSpec event stream into BlockNote JSON. Implements the EventSink trait and emits JSON tokens directly to any Write target as events arrive — no intermediate document representation, constant memory regardless of file size.
Supported Features
| Block type | BlockNote type |
|---|---|
| Paragraph | paragraph |
| Heading (levels 1–6) | heading |
| Block quote | quote |
| Preformatted / code block | codeBlock |
| Image | image |
| Table (with header and data cells) | table |
| Thematic break | divider |
| Unordered list item | bulletListItem |
| Ordered list item | numberedListItem |
| Inline link | link inline type with href (title is dropped) |
Inline styles are consumed from StartTextStyle/EndTextStyle spans around Text events. Bold, italic, code, strikethrough, and underline render as BlockNote style flags. Subscript and superscript are accepted but omitted because BlockNote's default schema has no equivalent representation.
Inline links (StartLink/EndLink) emit a link inline type with the href. The optional title field is dropped (BlockNote's default link schema has no title).
List items support arbitrary nesting via BlockNote's native children: Block[] arrays. The start prop on numberedListItem is preserved when the first item in a sequence carries an explicit start number.
Color Styles
Two color-bearing style kinds emit JSON keys in the inline styles object:
textColor— emitted when the reader sendsStartTextStyle { kind: TextColor(Color) }. The RGB value is snapped to the nearest BlockNote palette color and written as a string, e.g."textColor":"red".backgroundColor— emitted when the reader sendsStartTextStyle { kind: Mark(Color) }. Same palette snap, written as e.g."backgroundColor":"blue".
Pure black (0, 0, 0) is treated as BlockNote's default and produces no key for either style. Non-RGB colors are also omitted. Any other RGB color is snapped to one of the 9 named palette entries.
Palette
Each style type has its own 9-entry palette of named colors. The names are the same for both, but the RGB values differ because BlockNote uses distinct pastel shades for backgrounds versus richer tones for text:
| Name |
|---|
gray |
brown |
red |
orange |
yellow |
green |
blue |
purple |
pink |
Palette snap algorithm
The palette snap uses squared Euclidean distance in 8-bit sRGB space. No perceptual weighting, no gamma correction. Ties are broken by iteration order. This mirrors the reference Elixir implementation.
The two palettes use different RGB values, so the same input color can snap to different names depending on whether it's a text color or a background color. For instance, the saturated red (224, 62, 62) snaps to background palette "orange" (not "red") because the background palette uses pastel colors and the Euclidean distance to pastel orange is shorter than to pastel red. This is intentional and matches the reference implementation.
Not Yet Supported
- Footnotes —
StartFootnote/EndFootnote/FootnoteRefare silently ignored; BlockNote's default schema has no equivalent - Definition lists —
StartDefinitionList/StartDefinitionTerm/StartDefinitionDetail(and theirEnd*pairs) are silently ignored; BlockNote's default schema has no equivalent - Captions —
StartCaption/EndCaptionare silently ignored checkListItem— requires upstream DocSpec event support not yet definedtoggleListItem— no DocSpec event equivalent- Custom list style markers — the
style_typefield onStartOrderedListItem/StartUnorderedListItemis always dropped (BlockNote's default schema has no equivalent); list kind is determined entirely by the event variant (ordered vs unordered)
Usage
Wrap BlockNoteWriter in StackTrackingSink before feeding list events. The writer lazily opens the list item's content[] array when the first paragraph or inline content arrives, which lets it preserve non-default first-paragraph alignment on the list item while omitting BlockNote's default props. StackTrackingSink is still required because it normalizes paragraph boundaries (especially from sources that don't always emit an explicit StartParagraph inside list items), which is what lets the writer detect multi-paragraph items and transition emission into children[] for subsequent paragraphs.
use BlockNoteWriter;
use ;
let mut buf = Vec::new;
let mut writer = new;
writer.handle_event?;
// Plain paragraph
writer.handle_event?;
writer.handle_event?;
writer.handle_event?;
// Styled text uses wrapper events.
writer.handle_event?;
writer.handle_event?;
writer.handle_event?;
writer.handle_event?;
writer.handle_event?;
// Bullet list item
writer.handle_event?;
writer.handle_event?;
writer.handle_event?;
// Numbered list item
writer.handle_event?;
writer.handle_event?;
writer.handle_event?;
writer.handle_event?;
writer.finish?;
let json = Stringfrom_utf8?;
# Ok::
Limitations
Table cell content: BlockNote's tableCell.content is InlineContent[] and cannot hold block-level types. Block-level events inside a cell are handled as follows:
StartParagraph/EndParagraphboundaries are absorbed silently (adjacent paragraphs concatenate without separator)TextandLineBreakare preserved- Everything else (images, headings, code blocks, blockquotes, list items, nested tables) is dropped silently
Non-paragraph children inside list items: headings, images, code blocks, and blockquotes that appear as children of a list item are dropped. The first paragraph's inline content populates the item's content[] array; each subsequent paragraph becomes a child paragraph block in children[].
Blockquote + list interaction: a list item encountered while inside a blockquote force-closes the blockquote and emits the list item at the top level as a sibling.
Image-in-link: BlockNote does not allow block-level images inside inline links. The reader closes the link before emitting the image as a sibling block, and the writer serialises that sequence directly. Content preceding the image stays inside the link; the image becomes a sibling block after the link closes; content following the image appears outside the link, losing its link wrapper. The link is empty only when the image is the sole link label, e.g. [](url). All variants are lossy mappings of the original "image-inside-link" structure.
API Documentation
See the module-level docs for the full API surface, including BlockNoteWriter and its EventSink implementation.
License
See the repository LICENSE.