mdstitch 0.1.0

Streaming markdown preprocessor that closes unterminated syntax token-by-token.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
#![doc(
    html_logo_url = "https://raw.githubusercontent.com/df49b9cd/tahoe-gpui/main/crates/mdstitch/branding/mdstitch-icon.png",
    html_favicon_url = "https://raw.githubusercontent.com/df49b9cd/tahoe-gpui/main/crates/mdstitch/branding/mdstitch-icon.png"
)]
//! Streaming markdown preprocessor that auto-completes incomplete syntax.
//!
//! Runs on raw markdown strings **before** the pulldown-cmark parser, detecting
//! and closing unterminated formatting markers so content renders correctly
//! during token-by-token streaming.

mod bracket;
mod fence;
mod options;
mod ranges;
mod utils;

mod comparison_operators;
mod emphasis;
mod html_tags;
mod inline_code;
mod katex;
mod link_image;
mod setext_heading;
mod single_tilde;
mod strikethrough;

mod detect_direction;
mod incomplete_code;
mod preprocess;

pub use options::{LinkMode, StitchHandler, StitchOptions, priority};
pub use ranges::CodeBlockRanges;

// Re-export public items from internal modules.
pub use detect_direction::{TextDirection, detect_text_direction};
pub use incomplete_code::{has_incomplete_code_fence, has_table};
pub use preprocess::{
    normalize_html_indentation, preprocess_custom_tags, preprocess_literal_tag_content,
};

// Re-export utility functions for use by custom handlers.
// These four are the most commonly needed when implementing `StitchHandler`:
// code block detection, link/image URL detection, math block detection, and
// word character classification.
pub use utils::{
    is_inside_code_block, is_within_link_or_image_url, is_within_math_block, is_word_char,
};

use std::borrow::Cow;

const INCOMPLETE_LINK_MARKER: &str = "](stitch:incomplete-link)";

/// A built-in handler, either plain or taking pre-computed code-block ranges.
enum BuiltInHandler<'a> {
    /// Handler that does not need code-block ranges.
    Plain(Box<dyn Fn(&str) -> Cow<'_, str> + 'a>),
    /// Handler that takes pre-computed `CodeBlockRanges` to avoid redundant O(n) scans.
    ///
    /// The returned `Cow` borrows from the input `&str` (first parameter), not
    /// from the ranges — spelled out via HRTB because two `&` parameters defeat
    /// lifetime elision.
    WithRanges(Box<dyn for<'b> Fn(&'b str, &ranges::CodeBlockRanges) -> Cow<'b, str> + 'a>),
}

/// A handler entry in the priority-sorted pipeline.
enum HandlerEntry<'a> {
    BuiltIn {
        handler: BuiltInHandler<'a>,
        priority: i32,
        early_return: bool,
        /// `true` if the handler may rewrite bytes in the middle of the string,
        /// invalidating any pre-computed `CodeBlockRanges`.
        mutates_mid_text: bool,
    },
    /// A custom handler (trait object).
    Custom(&'a dyn StitchHandler),
}

impl HandlerEntry<'_> {
    fn priority(&self) -> i32 {
        match self {
            HandlerEntry::BuiltIn { priority, .. } => *priority,
            HandlerEntry::Custom(h) => h.priority(),
        }
    }
}

/// Preprocesses streaming markdown text, auto-completing any incomplete syntax.
///
/// Returns `Cow::Borrowed` when no changes are needed (zero-allocation fast path).
pub fn stitch<'a>(text: &'a str, options: &StitchOptions) -> Cow<'a, str> {
    if text.is_empty() {
        return Cow::Borrowed(text);
    }

    // Strip trailing single space (preserve double space for line breaks).
    let mut result: Cow<'a, str> = if text.ends_with(' ') && !text.ends_with("  ") {
        Cow::Borrowed(&text[..text.len() - 1])
    } else {
        Cow::Borrowed(text)
    };

    // If no custom handlers, use the fast fixed-order pipeline.
    if options.handlers.is_empty() {
        return run_builtin_pipeline(result, options);
    }

    // Build and sort handler entries by priority.
    let mut entries: Vec<HandlerEntry<'_>> = Vec::new();

    if options.single_tilde {
        entries.push(HandlerEntry::BuiltIn {
            handler: BuiltInHandler::Plain(Box::new(single_tilde::handle)),
            priority: priority::SINGLE_TILDE,
            early_return: false,
            mutates_mid_text: false,
        });
    }
    if options.comparison_operators {
        entries.push(HandlerEntry::BuiltIn {
            handler: BuiltInHandler::Plain(Box::new(comparison_operators::handle)),
            priority: priority::COMPARISON_OPERATORS,
            early_return: false,
            mutates_mid_text: false,
        });
    }
    if options.html_tags {
        entries.push(HandlerEntry::BuiltIn {
            handler: BuiltInHandler::WithRanges(Box::new(html_tags::handle_with_ranges)),
            priority: priority::HTML_TAGS,
            early_return: false,
            mutates_mid_text: false,
        });
    }
    if options.setext_headings {
        entries.push(HandlerEntry::BuiltIn {
            handler: BuiltInHandler::Plain(Box::new(setext_heading::handle)),
            priority: priority::SETEXT_HEADINGS,
            early_return: false,
            mutates_mid_text: false,
        });
    }
    if options.links || options.images {
        let link_mode = options.link_mode;
        let links_enabled = options.links;
        let images_enabled = options.images;
        // `LinkMode::TextOnly` rewrites mid-text (drops `[` from the opening
        // bracket), shifting byte offsets; `Protocol` either appends at the end
        // or triggers early return on the sentinel marker.
        let mutates_mid_text = link_mode == options::LinkMode::TextOnly;
        let early_return = link_mode == options::LinkMode::Protocol;
        entries.push(HandlerEntry::BuiltIn {
            handler: BuiltInHandler::WithRanges(Box::new(move |text, r| {
                link_image::handle_with_ranges(text, link_mode, links_enabled, images_enabled, r)
            })),
            priority: priority::LINKS,
            early_return,
            mutates_mid_text,
        });
        // Unwrapping a bracket (e.g. `[<a](` → `<a`) can expose an incomplete
        // HTML tag that the first `html_tags` pass rejected as implausible
        // because of the trailing `](`. Re-run `html_tags` on the unwrapped
        // residue so the pipeline reaches its fixed point in one call.
        if options.html_tags {
            entries.push(HandlerEntry::BuiltIn {
                handler: BuiltInHandler::WithRanges(Box::new(html_tags::handle_with_ranges)),
                priority: priority::LINKS + 1,
                early_return: false,
                mutates_mid_text: false,
            });
        }
    }
    if options.bold_italic {
        entries.push(HandlerEntry::BuiltIn {
            handler: BuiltInHandler::WithRanges(Box::new(emphasis::handle_bold_italic_with_ranges)),
            priority: priority::BOLD_ITALIC,
            early_return: false,
            mutates_mid_text: false,
        });
    }
    if options.bold {
        entries.push(HandlerEntry::BuiltIn {
            handler: BuiltInHandler::WithRanges(Box::new(emphasis::handle_bold_with_ranges)),
            priority: priority::BOLD,
            early_return: false,
            mutates_mid_text: false,
        });
    }
    if options.italic {
        entries.push(HandlerEntry::BuiltIn {
            handler: BuiltInHandler::WithRanges(Box::new(
                emphasis::handle_double_underscore_with_ranges,
            )),
            priority: priority::ITALIC_DOUBLE_UNDERSCORE,
            early_return: false,
            mutates_mid_text: false,
        });
        entries.push(HandlerEntry::BuiltIn {
            handler: BuiltInHandler::WithRanges(Box::new(
                emphasis::handle_italic_asterisk_with_ranges,
            )),
            priority: priority::ITALIC_SINGLE_ASTERISK,
            early_return: false,
            mutates_mid_text: false,
        });
        entries.push(HandlerEntry::BuiltIn {
            handler: BuiltInHandler::WithRanges(Box::new(
                emphasis::handle_italic_underscore_with_ranges,
            )),
            priority: priority::ITALIC_SINGLE_UNDERSCORE,
            early_return: false,
            mutates_mid_text: false,
        });
    }
    if options.inline_code {
        entries.push(HandlerEntry::BuiltIn {
            handler: BuiltInHandler::Plain(Box::new(inline_code::handle)),
            priority: priority::INLINE_CODE,
            early_return: false,
            mutates_mid_text: false,
        });
    }
    if options.strikethrough {
        entries.push(HandlerEntry::BuiltIn {
            handler: BuiltInHandler::WithRanges(Box::new(strikethrough::handle_with_ranges)),
            priority: priority::STRIKETHROUGH,
            early_return: false,
            mutates_mid_text: false,
        });
    }
    if options.katex {
        entries.push(HandlerEntry::BuiltIn {
            handler: BuiltInHandler::WithRanges(Box::new(katex::handle_block_with_ranges)),
            priority: priority::KATEX,
            early_return: false,
            mutates_mid_text: false,
        });
    }
    if options.inline_katex {
        entries.push(HandlerEntry::BuiltIn {
            handler: BuiltInHandler::WithRanges(Box::new(katex::handle_inline_with_ranges)),
            priority: priority::INLINE_KATEX,
            early_return: false,
            mutates_mid_text: false,
        });
    }

    // Add custom handlers.
    for handler in &options.handlers {
        entries.push(HandlerEntry::Custom(handler.as_ref()));
    }

    // Sort by priority (stable sort preserves insertion order for equal priorities).
    entries.sort_by_key(|e| e.priority());

    // Share a single `CodeBlockRanges` across all range-using handlers, built
    // lazily on first use and invalidated after any handler that may rewrite
    // bytes in the middle of the string (custom handlers are opaque mutators).
    let mut shared_ranges: Option<ranges::CodeBlockRanges> = None;

    for entry in &entries {
        match entry {
            HandlerEntry::BuiltIn {
                handler,
                early_return,
                mutates_mid_text,
                ..
            } => {
                let before_ptr = result.as_ref().as_ptr();
                match handler {
                    BuiltInHandler::Plain(f) => {
                        result = apply_with(result, |text| f(text));
                    }
                    BuiltInHandler::WithRanges(f) => {
                        if shared_ranges.is_none() {
                            shared_ranges = Some(ranges::CodeBlockRanges::new(&result));
                        }
                        let r = shared_ranges.as_ref().expect("ranges just initialized");
                        result = apply_with(result, |text| f(text, r));
                    }
                }
                if *early_return && result.ends_with(INCOMPLETE_LINK_MARKER) {
                    return result;
                }
                if *mutates_mid_text && !std::ptr::eq(result.as_ref().as_ptr(), before_ptr) {
                    shared_ranges = None;
                }
            }
            HandlerEntry::Custom(h) => {
                let before_ptr = result.as_ref().as_ptr();
                result = apply_with(result, |text| h.handle(text));
                if !std::ptr::eq(result.as_ref().as_ptr(), before_ptr) {
                    shared_ranges = None;
                }
            }
        }
    }

    result
}

/// Fast path: fixed-order pipeline with no dynamic dispatch (used when no custom handlers).
fn run_builtin_pipeline<'a>(mut result: Cow<'a, str>, options: &StitchOptions) -> Cow<'a, str> {
    if options.single_tilde {
        result = apply(result, single_tilde::handle);
    }
    if options.comparison_operators {
        result = apply(result, comparison_operators::handle);
    }

    // Compute CodeBlockRanges once for all handlers that need code-block detection,
    // converting ~15 O(n) scans per streaming delta into 1 O(n) scan + O(log n) queries.
    //
    // Must be `mut` because `link_image` in `LinkMode::TextOnly` can remove a `[`
    // byte from the middle of the string, shifting every subsequent byte offset.
    // Stale ranges would then mis-report code regions to downstream handlers.
    let needs_ranges = options.html_tags
        || options.links
        || options.images
        || options.bold_italic
        || options.bold
        || options.italic
        || options.strikethrough
        || options.katex
        || options.inline_katex;
    let mut ranges = needs_ranges.then(|| ranges::CodeBlockRanges::new(&result));

    if options.html_tags
        && let Some(ref r) = ranges
    {
        result = apply_with(result, |text| html_tags::handle_with_ranges(text, r));
    }
    if options.setext_headings {
        result = apply(result, setext_heading::handle);
    }
    if (options.links || options.images)
        && let Some(ref r_guard) = ranges
    {
        let link_mode = options.link_mode;
        let links_enabled = options.links;
        let images_enabled = options.images;
        let before_ptr = result.as_ref().as_ptr();
        result = apply_with(result, move |text| {
            link_image::handle_with_ranges(text, link_mode, links_enabled, images_enabled, r_guard)
        });
        if result.ends_with(INCOMPLETE_LINK_MARKER) {
            return result;
        }
        // Mid-text mutation (e.g. TextOnly mode removing `[`) shifts every
        // later byte; rebuild ranges so downstream handlers see correct offsets.
        if !std::ptr::eq(result.as_ref().as_ptr(), before_ptr) {
            ranges = Some(ranges::CodeBlockRanges::new(&result));
            // Unwrapping a bracket (e.g. `[<a](` → `<a`) can expose an incomplete
            // HTML tag that the first `html_tags` pass rejected as implausible
            // because of the trailing `](`. Re-run `html_tags` on the unwrapped
            // residue so the pipeline reaches its fixed point in one call.
            if options.html_tags
                && let Some(ref r) = ranges
            {
                result = apply_with(result, |text| html_tags::handle_with_ranges(text, r));
            }
        }
    }
    if let Some(ref r) = ranges {
        if options.bold_italic {
            result = apply_with(result, |text| {
                emphasis::handle_bold_italic_with_ranges(text, r)
            });
        }
        if options.bold {
            result = apply_with(result, |text| emphasis::handle_bold_with_ranges(text, r));
        }
        if options.italic {
            result = apply_with(result, |text| {
                emphasis::handle_double_underscore_with_ranges(text, r)
            });
            result = apply_with(result, |text| {
                emphasis::handle_italic_asterisk_with_ranges(text, r)
            });
            result = apply_with(result, |text| {
                emphasis::handle_italic_underscore_with_ranges(text, r)
            });
        }
        if options.inline_code {
            result = apply(result, inline_code::handle);
        }
        if options.strikethrough {
            result = apply_with(result, |text| strikethrough::handle_with_ranges(text, r));
        }
        if options.katex {
            result = apply_with(result, |text| katex::handle_block_with_ranges(text, r));
        }
        if options.inline_katex {
            result = apply_with(result, |text| katex::handle_inline_with_ranges(text, r));
        }
    } else if options.inline_code {
        // Only inline_code can reach here: when `needs_ranges` is false, every
        // other option in this block is also disabled (they gate `needs_ranges`).
        result = apply(result, inline_code::handle);
    }

    result
}

/// Applies a handler to a `Cow<str>`, threading ownership efficiently.
fn apply<'a>(input: Cow<'a, str>, handler: fn(&str) -> Cow<'_, str>) -> Cow<'a, str> {
    apply_with(input, handler)
}

/// Applies a closure handler to a `Cow<str>`, threading ownership efficiently.
fn apply_with<'a>(input: Cow<'a, str>, handler: impl FnOnce(&str) -> Cow<'_, str>) -> Cow<'a, str> {
    match handler(&input) {
        Cow::Borrowed(b) if std::ptr::eq(b, input.as_ref() as &str) => {
            // Handler returned its input unchanged — preserve the original Cow.
            input
        }
        Cow::Borrowed(b) => {
            // Handler returned a borrowed sub-slice (e.g. trimmed) — must own it.
            Cow::Owned(b.to_owned())
        }
        Cow::Owned(s) => Cow::Owned(s),
    }
}

#[cfg(test)]
mod tests;