Skip to main content

common/
format_runs_query.rs

1//! Store-aware readers that synthesize inline content views from
2//! per-block `format_runs` + `block_images`. The canonical entry
3//! point is [`inline_segments_for_block`], which returns the
4//! `Vec<InlineSegment>` view used by export, fragments, cursor, and
5//! tests.
6
7use crate::database::Store;
8use crate::format_runs::{
9    FootnoteRefAnchor, FormatRun, ImageAnchor, InlineSegment, inline_segments_view,
10};
11use crate::types::EntityId;
12
13/// Fetch the format runs for a block. Returns an empty Vec if the block
14/// has no runs (treated the same as a missing entry).
15pub fn get_format_runs(store: &Store, block_id: EntityId) -> Vec<FormatRun> {
16    store
17        .format_runs
18        .read()
19        .get(&block_id)
20        .cloned()
21        .unwrap_or_default()
22}
23
24/// Fetch the footnote references anchored in a block.
25pub fn get_block_footnote_refs(store: &Store, block_id: EntityId) -> Vec<FootnoteRefAnchor> {
26    store
27        .block_footnote_refs
28        .read()
29        .get(&block_id)
30        .cloned()
31        .unwrap_or_default()
32}
33
34/// Fetch the image anchors for a block.
35pub fn get_block_images(store: &Store, block_id: EntityId) -> Vec<ImageAnchor> {
36    store
37        .block_images
38        .read()
39        .get(&block_id)
40        .cloned()
41        .unwrap_or_default()
42}
43
44/// Synthesize the `Vec<InlineSegment>` view for a block from its
45/// format_runs and block_images. Callers must pass the block's
46/// `plain_text` (which they already have in scope from a prior
47/// `get_block` call) — this avoids re-locking the blocks table.
48///
49/// This is the Phase 1.14b-and-forward reader function. Returns segments
50/// in document order.
51pub fn inline_segments_for_block(
52    store: &Store,
53    block_id: EntityId,
54    block_plain_text: &str,
55) -> Vec<InlineSegment> {
56    let runs = get_format_runs(store, block_id);
57    let images = get_block_images(store, block_id);
58    let notes = get_block_footnote_refs(store, block_id);
59    inline_segments_view(block_plain_text, &runs, &images, &notes)
60}