moss-core 0.7.0

Pure-Rust content engine for moss: AST, render, resolve, validate, frontmatter, schema.
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
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
//! The one answer to "which parts of this markdown are NOT live syntax?"
//!
//! moss runs several scanners over raw markdown *before* pulldown-cmark ever
//! sees it — `:::shortcode` extraction ([`crate::ast::shortcode_extract`]),
//! transclusion/folder-embed lowering ([`crate::resolve`]), CriticMarkup
//! accept and `%%comment%%` stripping (src-tauri's `html_post`). Each of them
//! has to know which byte ranges are *inert*: regions where author text that
//! merely looks like syntax must be left completely alone.
//!
//! Every one of those scanners used to carry its own private answer, and each
//! private answer was a different subset of the truth. The one that only knew
//! about fenced code blocks shipped moss#903 bug 2: a `:::gallery` written
//! inside an authored `<!-- TODO … -->` block was extracted as a live
//! shortcode, which spliced a sentinel into the middle of the comment,
//! destroyed the comment's own `-->`, and made every paragraph *after* the
//! comment vanish from the built page. Consolidating on one scanner is what
//! stops that class of bug from being re-derivable.
//!
//! # What counts as inert
//!
//! | Region | Rule |
//! |---|---|
//! | fenced code block | 3+ `` ` ``/`~` run through its closing run (or EOF) |
//! | indented code block | 4-space-indented run that starts a new block |
//! | inline code span | matched backtick runs, on one line |
//! | HTML comment | `<!--` through `-->`, possibly spanning lines (or EOF) |
//!
//! Deliberately NOT inert: raw HTML tags and HTML blocks other than comments
//! (`<div>` wrappers are a documented moss authoring idiom and shortcodes
//! nest inside them), and math spans (`$…$` is handled by the parser, and
//! `:::` inside math is not a shape anyone writes).
//!
//! # UTF-8 safety
//!
//! The scan walks bytes, matching only ASCII (`` ` ``, `<`, `-`, `>`, space,
//! tab, newline). No byte of a multi-byte UTF-8 sequence is ever ASCII, so
//! every offset this module produces lands on a char boundary — the property
//! that hand-rolled byte loops in this codebase have repeatedly failed to
//! preserve (see the `html_prefix_is_balanced` panic on CJK prose in #903
//! bug 1).
//!
//! # Which shape do I want?
//!
//! - Scanning a whole document for a token, and you want the inert
//!   occurrences to simply not be found: [`mask_inert`]. Byte-length- and
//!   line-preserving, so a match offset in the mask is the same offset in the
//!   original.
//! - Walking line by line, and you want to skip inert lines:
//!   [`inert_lines`].
//! - Anything else: [`InertRegions::scan`] plus [`InertRegions::is_inert`] /
//!   [`InertRegions::intersects`].
//!
//! Not yet consolidated onto this module (each still carries a fence-only
//! scan): [`crate::resolve::block_refs`], [`crate::ast::editor_scan`], and
//! src-tauri's `build::scan::scan`. They should move here as they are next
//! touched. [`crate::resolve::md_extract`] moved here 2026-08-03.

use std::ops::Range;

/// Sorted, non-overlapping byte ranges of `markdown` that are inert.
///
/// Ranges that cover a whole line include that line's terminator, so a blank
/// line inside a fence or comment is reported as inert too.
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct InertRegions {
    ranges: Vec<Range<usize>>,
    unterminated_comment: Option<usize>,
}

/// Convenience for the common "find tokens outside inert regions" shape:
/// returns a copy of `markdown` with every inert byte replaced by a space.
///
/// See [`InertRegions::mask`] for the guarantees.
pub fn mask_inert(markdown: &str) -> String {
    InertRegions::scan(markdown).mask(markdown)
}

/// Convenience for line-based scanners: one flag per line of `markdown`,
/// aligned index-for-index with [`str::lines`].
///
/// See [`InertRegions::inert_lines`] for what makes a line inert.
pub fn inert_lines(markdown: &str) -> Vec<bool> {
    InertRegions::scan(markdown).inert_lines(markdown)
}

impl InertRegions {
    /// Scan raw markdown source for inert byte ranges.
    ///
    /// Total function: any input, including unterminated fences and
    /// unterminated comments (both of which extend to end-of-input, matching
    /// CommonMark's treatment of an unclosed fenced code block and an
    /// unclosed HTML block).
    pub fn scan(markdown: &str) -> Self {
        let bytes = markdown.as_bytes();
        let mut ranges: Vec<Range<usize>> = Vec::new();

        // Open fence: (fence char, opening run length). CommonMark requires
        // the closing run to be the same char and at least as long.
        let mut fence: Option<(u8, usize)> = None;
        let mut in_comment = false;
        // Where the currently-open `<!--` started. Survives to the end of the
        // scan only when the comment is never closed, which is what
        // `unterminated_comment` reports.
        let mut comment_start: Option<usize> = None;
        let mut in_indented_code = false;
        // Start-of-document reads like "after a blank line": an indented
        // first line is an indented code block.
        let mut prev_blank = true;
        // Sticky list tracking — see `indented code` note in `scan_line`.
        let mut in_list = false;

        let mut line_start = 0usize;
        while line_start < bytes.len() {
            let (content_end, next_start) = line_bounds(bytes, line_start);
            let indent = indent_width(bytes, line_start, content_end);
            let text_start = first_non_ws(bytes, line_start, content_end);
            let is_blank = text_start == content_end;

            // 1. Inside a multi-line HTML comment: inert until `-->`.
            if in_comment {
                match find_bytes(bytes, line_start, content_end, b"-->") {
                    Some(at) => {
                        in_comment = false;
                        comment_start = None;
                        push_range(&mut ranges, line_start..at + 3);
                        // The rest of the line is live again — it can open
                        // another comment or a code span.
                        scan_inline(
                            bytes,
                            at + 3,
                            content_end,
                            next_start,
                            &mut ranges,
                            &mut in_comment,
                            &mut comment_start,
                        );
                    }
                    None => push_range(&mut ranges, line_start..next_start),
                }
                prev_blank = is_blank;
                line_start = next_start;
                continue;
            }

            // 2. Inside a fenced code block: inert until the closing run.
            if let Some((fence_char, open_len)) = fence {
                push_range(&mut ranges, line_start..next_start);
                if closes_fence(bytes, text_start, content_end, fence_char, open_len) {
                    fence = None;
                }
                prev_blank = is_blank;
                line_start = next_start;
                continue;
            }

            // 3. Inside an indented code block: any 4+-indented line
            //    continues it, and so do blank lines (a blank line only ends
            //    it if the next non-blank line dedents).
            if in_indented_code && (is_blank || indent >= 4) {
                push_range(&mut ranges, line_start..next_start);
                prev_blank = is_blank;
                line_start = next_start;
                continue;
            }
            in_indented_code = false;

            if is_blank {
                prev_blank = true;
                line_start = next_start;
                continue;
            }

            // 4. Start of an indented code block. Two guards keep this from
            //    swallowing live content: CommonMark forbids indented code
            //    from interrupting a paragraph (hence `prev_blank`), and
            //    indentation inside a list is list-item content, not code
            //    (hence `in_list`). `in_list` is deliberately sticky —
            //    over-reporting a region as live is a rendering nicety,
            //    under-reporting it silently deletes an author's shortcode.
            if indent >= 4 && prev_blank && !in_list {
                in_indented_code = true;
                push_range(&mut ranges, line_start..next_start);
                prev_blank = false;
                line_start = next_start;
                continue;
            }

            // 5. Opening fence line.
            if let Some((fence_char, run)) = opens_fence(bytes, text_start, content_end) {
                fence = Some((fence_char, run));
                push_range(&mut ranges, line_start..next_start);
                prev_blank = false;
                line_start = next_start;
                continue;
            }

            // 6. Ordinary line: track list context, then look for inline
            //    code spans and HTML comments.
            if is_list_marker(bytes, text_start, content_end) {
                in_list = true;
            } else if indent == 0 && prev_blank {
                in_list = false;
            }
            scan_inline(
                bytes,
                text_start,
                content_end,
                next_start,
                &mut ranges,
                &mut in_comment,
                &mut comment_start,
            );
            prev_blank = false;
            line_start = next_start;
        }

        Self {
            ranges,
            unterminated_comment: if in_comment { comment_start } else { None },
        }
    }

    /// Byte offset of an `<!--` that the document never closes, if there is
    /// one.
    ///
    /// Per CommonMark an unterminated HTML comment runs to end-of-input, so
    /// everything after it is inert — the whole tail of the page stops being
    /// markdown. That is silent content loss, which is the failure this
    /// module exists to stop, so callers with a diagnostics channel report
    /// it (see `ast::shortcode_extract`).
    pub fn unterminated_comment(&self) -> Option<usize> {
        self.unterminated_comment
    }

    /// The inert ranges, sorted by start and non-overlapping.
    pub fn ranges(&self) -> &[Range<usize>] {
        &self.ranges
    }

    /// True if no region is inert.
    pub fn is_empty(&self) -> bool {
        self.ranges.is_empty()
    }

    /// True if the byte at `offset` is inside an inert region.
    pub fn is_inert(&self, offset: usize) -> bool {
        // Ranges are sorted and disjoint: the only candidate is the last
        // range starting at or before `offset`.
        let idx = self.ranges.partition_point(|r| r.start <= offset);
        match idx.checked_sub(1).and_then(|i| self.ranges.get(i)) {
            Some(r) => offset < r.end,
            None => false,
        }
    }

    /// True if any part of `range` is inert. An empty range is tested as a
    /// single point.
    pub fn intersects(&self, range: Range<usize>) -> bool {
        if range.start >= range.end {
            return self.is_inert(range.start);
        }
        let idx = self.ranges.partition_point(|r| r.start < range.end);
        self.ranges
            .get(..idx)
            .unwrap_or_default()
            .iter()
            .rev()
            .take_while(|r| r.end > range.start)
            .any(|r| r.start < range.end)
    }

    /// Return a copy of `markdown` with every inert byte replaced by an ASCII
    /// space, leaving `\n` and `\r` in place.
    ///
    /// The result has the same byte length and the same line structure as the
    /// input, so a byte offset found in the mask indexes the original — the
    /// pattern src-tauri's CriticMarkup pass relies on (match on the mask,
    /// read the capture out of the original).
    pub fn mask(&self, markdown: &str) -> String {
        let mut out = markdown.as_bytes().to_vec();
        for range in &self.ranges {
            let end = range.end.min(out.len());
            if let Some(slice) = out.get_mut(range.start..end) {
                for b in slice {
                    if *b != b'\n' && *b != b'\r' {
                        *b = b' ';
                    }
                }
            }
        }
        // Inert range boundaries are ASCII-aligned (see the module docs), so
        // masking cannot split a multi-byte char. The fallback keeps the
        // function total rather than trusting that argument at runtime.
        String::from_utf8(out).unwrap_or_else(|_| markdown.to_string())
    }

    /// One flag per line of `markdown`, aligned index-for-index with
    /// [`str::lines`].
    ///
    /// A line is inert when its first non-whitespace byte is inert (or, for a
    /// blank line, when its start is). That is the question a line-based
    /// scanner actually asks — "is the token that begins this line live?" — so
    /// a line whose *tail* enters a comment (`text <!-- note`) is NOT inert:
    /// the syntax at its head is still live.
    pub fn inert_lines(&self, markdown: &str) -> Vec<bool> {
        let bytes = markdown.as_bytes();
        let mut flags = Vec::new();
        let mut line_start = 0usize;
        while line_start < bytes.len() {
            let (content_end, next_start) = line_bounds(bytes, line_start);
            let text_start = first_non_ws(bytes, line_start, content_end);
            // Blank line: probe its start (whole-line ranges cover the
            // terminator, so a blank line inside a fence still reads inert).
            let probe = if text_start == content_end {
                line_start
            } else {
                text_start
            };
            flags.push(self.is_inert(probe));
            line_start = next_start;
        }
        flags
    }
}

/// `(end of line content excluding the terminator, start of the next line)`.
fn line_bounds(bytes: &[u8], line_start: usize) -> (usize, usize) {
    let mut i = line_start;
    while i < bytes.len() && bytes[i] != b'\n' {
        i += 1;
    }
    let next_start = if i < bytes.len() { i + 1 } else { bytes.len() };
    let mut content_end = i;
    if content_end > line_start && bytes.get(content_end - 1) == Some(&b'\r') {
        content_end -= 1;
    }
    (content_end, next_start)
}

/// Offset of the first non-space/tab byte in `[from, to)`, or `to`.
fn first_non_ws(bytes: &[u8], from: usize, to: usize) -> usize {
    let mut i = from;
    while i < to && (bytes[i] == b' ' || bytes[i] == b'\t') {
        i += 1;
    }
    i
}

/// CommonMark indent width of the line: tabs advance to the next 4-column
/// stop.
fn indent_width(bytes: &[u8], from: usize, to: usize) -> usize {
    let mut width = 0usize;
    let mut i = from;
    while i < to {
        match bytes[i] {
            b' ' => width += 1,
            b'\t' => width += 4 - (width % 4),
            _ => break,
        }
        i += 1;
    }
    width
}

/// A run of 3+ identical `` ` ``/`~` starting at `text_start`, returned as
/// `(char, run length)`. A backtick fence's info string may not contain a
/// backtick (CommonMark), which is what keeps `` `a` `b` `` from being read
/// as a fence opener.
fn opens_fence(bytes: &[u8], text_start: usize, content_end: usize) -> Option<(u8, usize)> {
    let ch = *bytes.get(text_start)?;
    if ch != b'`' && ch != b'~' {
        return None;
    }
    let mut run = 0usize;
    while bytes.get(text_start + run) == Some(&ch) {
        run += 1;
    }
    if run < 3 {
        return None;
    }
    if ch == b'`' {
        let info = bytes.get(text_start + run..content_end).unwrap_or_default();
        if info.contains(&b'`') {
            return None;
        }
    }
    Some((ch, run))
}

/// True if the line is a closing fence for `(fence_char, open_len)`: a run of
/// at least `open_len` of the same char, then nothing but whitespace.
fn closes_fence(
    bytes: &[u8],
    text_start: usize,
    content_end: usize,
    fence_char: u8,
    open_len: usize,
) -> bool {
    let mut run = 0usize;
    while text_start + run < content_end && bytes.get(text_start + run) == Some(&fence_char) {
        run += 1;
    }
    if run < open_len {
        return false;
    }
    bytes
        .get(text_start + run..content_end)
        .unwrap_or_default()
        .iter()
        .all(|b| *b == b' ' || *b == b'\t')
}

/// True if the line starts a list item (`-`/`*`/`+` or `N.`/`N)` followed by
/// whitespace or end-of-line).
fn is_list_marker(bytes: &[u8], text_start: usize, content_end: usize) -> bool {
    let after_marker = match bytes.get(text_start) {
        Some(b'-') | Some(b'*') | Some(b'+') => text_start + 1,
        Some(d) if d.is_ascii_digit() => {
            let mut i = text_start;
            while i < content_end && bytes.get(i).is_some_and(u8::is_ascii_digit) {
                i += 1;
            }
            match bytes.get(i) {
                Some(b'.') | Some(b')') => i + 1,
                _ => return false,
            }
        }
        _ => return false,
    };
    match bytes.get(after_marker) {
        None => true,
        Some(b' ') | Some(b'\t') | Some(b'\r') | Some(b'\n') => true,
        Some(_) => after_marker >= content_end,
    }
}

/// Scan `[from, content_end)` of one line for inline code spans and HTML
/// comments, pushing the inert ranges it finds.
///
/// Sets `in_comment` (and records `comment_start`) when the line ends inside
/// an unterminated `<!--`; the range pushed in that case runs to `next_start`
/// so the line's terminator is covered.
#[allow(clippy::too_many_arguments)]
fn scan_inline(
    bytes: &[u8],
    from: usize,
    content_end: usize,
    next_start: usize,
    ranges: &mut Vec<Range<usize>>,
    in_comment: &mut bool,
    comment_start: &mut Option<usize>,
) {
    let mut i = from;
    while i < content_end {
        match bytes[i] {
            b'`' => {
                let mut run = 0usize;
                while i + run < content_end && bytes[i + run] == b'`' {
                    run += 1;
                }
                match closing_backtick_run(bytes, i + run, content_end, run) {
                    Some(close) => {
                        push_range(ranges, i..close + run);
                        i = close + run;
                    }
                    // No matching run on this line: not inline code per
                    // CommonMark (a span may not contain a blank line, and
                    // moss's line-at-a-time scanners never look further).
                    None => i += run,
                }
            }
            b'<' if bytes.get(i..i + 4) == Some(b"<!--".as_slice()) => {
                // Search from `i + 2` so the degenerate empty comments
                // `<!-->` and `<!--->` terminate on their own `-->`
                // instead of swallowing the rest of the document.
                match find_bytes(bytes, i + 2, content_end, b"-->") {
                    Some(at) => {
                        push_range(ranges, i..at + 3);
                        i = at + 3;
                    }
                    None => {
                        push_range(ranges, i..next_start);
                        *in_comment = true;
                        *comment_start = Some(i);
                        return;
                    }
                }
            }
            _ => i += 1,
        }
    }
}

/// Offset of the start of a backtick run of exactly `run` length in
/// `[from, to)`, per CommonMark's "same number of backticks" rule.
fn closing_backtick_run(bytes: &[u8], from: usize, to: usize, run: usize) -> Option<usize> {
    let mut i = from;
    while i < to {
        if bytes[i] != b'`' {
            i += 1;
            continue;
        }
        let mut len = 0usize;
        while i + len < to && bytes[i + len] == b'`' {
            len += 1;
        }
        if len == run {
            return Some(i);
        }
        i += len;
    }
    None
}

/// First offset of `needle` within `[from, to)` of `bytes`.
fn find_bytes(bytes: &[u8], from: usize, to: usize, needle: &[u8]) -> Option<usize> {
    if needle.is_empty() || to < from {
        return None;
    }
    let hay = bytes.get(from..to)?;
    hay.windows(needle.len())
        .position(|w| w == needle)
        .map(|p| from + p)
}

/// Append `range`, coalescing with the previous range when they touch or
/// overlap, so [`InertRegions::ranges`] stays sorted and disjoint.
fn push_range(ranges: &mut Vec<Range<usize>>, range: Range<usize>) {
    if range.start >= range.end {
        return;
    }
    if let Some(last) = ranges.last_mut() {
        if range.start <= last.end {
            last.end = last.end.max(range.end);
            return;
        }
    }
    ranges.push(range);
}

#[cfg(test)]
#[path = "inert_regions_tests.rs"]
mod tests;