peisar 1.1.0

A practical Markdown parser written in Rust
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
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
use crate::markdown::ast::tokens::{
    Attributes,
    span::Span,
    token::{Block, Inline},
};
#[cfg(feature = "npm")]
use napi_derive::napi;

/// Unified plugin factory for the AST engine.
///
/// One trait covers both extension phases — parsing hooks (`try_parse_block` /
/// `try_parse_inline`) and visitor callbacks (`visit_block` / `visit_inline`).
/// It replaces the pre-1.3.0 [`AstParser`] + [`AstVisitor`] pair (both are
/// still available, deprecated).
///
/// All methods have default no-op / declining implementations — implement
/// only the callbacks a plugin needs.
pub trait PluginFactory {
    fn try_parse_block(&self, _ctx: &BlockParserContext) -> Option<(Block, usize)> {
        None
    }
    fn try_parse_inline(&self, _ctx: &InlineParserContext) -> Option<(Inline, usize)> {
        None
    }
    fn visit_block(&mut self, _block: &mut Block) -> VisitControl {
        VisitControl::default()
    }
    fn visit_inline(&mut self, _inline: &mut Inline) -> InlineVisitControl {
        InlineVisitControl::default()
    }
    /// Whether this factory carries parse hooks (i.e. the host must
    /// re-parse the raw source for its effects to show).  Default `false`
    /// — the JS adapter overrides this when `parseBlock` / `parseInline`
    /// callbacks are registered.
    fn has_parse_hooks(&self) -> bool {
        false
    }
}

/// Visitor trait for AST nodes. Implement this trait to receive callbacks
/// for every block- and inline-level node. The visitor methods receive a
/// mutable reference and return a control value that can request the
/// node be replaced, removed, or have nodes inserted around it.
///
/// Deprecated since 1.3.0 — implement [`PluginFactory`] instead; its
/// `visit_block` / `visit_inline` methods have the same shape.
#[deprecated(since = "1.3.0", note = "Use `PluginFactory` instead")]
pub trait AstVisitor {
    /// Called for every block-level node (pre-order). Return a
    /// `VisitControl` to indicate modifications. The callback may also
    /// mutate the node in-place.
    fn visit_block(&mut self, _block: &mut Block) -> VisitControl {
        VisitControl::default()
    }

    /// Called for every inline-level node (pre-order). Return an
    /// `InlineVisitControl` to indicate modifications. The callback may
    /// also mutate the inline node in-place.
    fn visit_inline(&mut self, _inline: &mut Inline) -> InlineVisitControl {
        InlineVisitControl::default()
    }
}

/// Custom parser hook trait.  Implement this to parse extension syntax
/// during parsing.
///
/// Both methods are optional — implement only the phase you need.  Return
/// `None` to decline and let the next hook (or the built-in parser) handle
/// the position.
///
/// Deprecated since 1.3.0 — implement [`PluginFactory`] instead; its
/// `try_parse_block` / `try_parse_inline` methods have the same shape.
#[deprecated(since = "1.3.0", note = "Use `PluginFactory` instead")]
pub trait AstParser {
    /// Try to parse a block at the current line.
    ///
    /// Returns the parsed [`Block`] and the number of source lines it
    /// consumed (must be at least 1; `0` is treated as `1`), or `None` to
    /// decline.  The returned block's `pos` span is overwritten with an
    /// accurate source span, and a Kramdown `{:...}` attribute block on a
    /// following line is applied automatically.
    fn try_parse_block(&self, _ctx: &BlockParserContext) -> Option<(Block, usize)> {
        None
    }

    /// Try to parse inline content starting at the current character.
    ///
    /// Returns the parsed [`Inline`] node and the number of characters it
    /// consumed (must be at least 1; `0` makes the result be ignored), or
    /// `None` to decline.  The returned node's `pos` span is overwritten
    /// with an accurate source span.
    fn try_parse_inline(&self, _ctx: &InlineParserContext) -> Option<(Inline, usize)> {
        None
    }
}

#[cfg_attr(feature = "npm", napi(object))]
#[derive(Debug, Clone)]
pub struct BlockParserContext {
    /// The current (unmodified) source line.
    pub line: String,
    /// 0-based index of the current line in the (sub-)document.
    pub line_index: u32,
    /// Remaining lines starting at the current line (`lines[0]` is `line`).
    pub lines: Vec<String>,
}

/// Context passed to inline parser hooks.
///
/// On the JavaScript side the fields are exposed as `rest` and `index`.
#[cfg_attr(feature = "npm", napi(object))]
#[derive(Debug, Clone)]
pub struct InlineParserContext {
    /// Remaining inline text starting at the current character position.
    pub rest: String,
    /// 0-based character index of the current position in the inline text.
    pub index: u32,
}
#[allow(rustdoc::private_intra_doc_links)]
/// Deprecated pre-1.3.0 name for [`BlockParserContext`].
#[deprecated(since = "1.3.0", note = "Use `BlockParserContext` instead")]
pub type BlockParseContext = BlockParserContext;
#[allow(rustdoc::private_intra_doc_links)]
/// Deprecated pre-1.3.0 name for [`InlineParserContext`].
#[deprecated(since = "1.3.0", note = "Use `InlineParserContext` instead")]
pub type InlineParseContext = InlineParserContext;
/// Control returned from `visit_block` describing edits to perform.
///
/// Returned by [`PluginFactory::visit_block`].  Mutually exclusive fields are
/// resolved in the order: `remove` → `replace_with` → keep.  `insert_before`
/// and `insert_after` apply regardless.
#[derive(Debug, Default)]
pub struct VisitControl {
    /// Nodes to insert before the current node's position.
    pub insert_before: Vec<Block>,
    /// Nodes to insert after the current node's position.
    pub insert_after: Vec<Block>,
    /// Replace the current node with these nodes.
    pub replace_with: Option<Vec<Block>>,
    /// Remove the current node entirely.
    pub remove: bool,
    /// Whether to recurse into this node's child nodes (when applicable).
    /// Default `false`.
    pub recurse: bool,
}

impl VisitControl {
    /// Convenience: keep the (possibly mutated) node and recurse into children.
    pub fn keep_and_recurse() -> Self {
        VisitControl {
            recurse: true,
            ..Default::default()
        }
    }

    /// Convenience: remove the node.
    pub fn remove() -> Self {
        VisitControl {
            remove: true,
            ..Default::default()
        }
    }

    /// Convenience: replace current node with given nodes.
    ///
    /// The replacements are not themselves visited.
    pub fn replace_with(nodes: Vec<Block>) -> Self {
        VisitControl {
            replace_with: Some(nodes),
            ..Default::default()
        }
    }
}

/// Control returned from `visit_inline` describing edits to perform on inline
/// nodes. Mirrors `VisitControl` but for `Inline` nodes.
#[derive(Debug, Default)]
pub struct InlineVisitControl {
    /// Nodes to insert before the current node's position.
    pub insert_before: Vec<Inline>,
    /// Nodes to insert after the current node's position.
    pub insert_after: Vec<Inline>,
    /// Replace the current node with these nodes.
    pub replace_with: Option<Vec<Inline>>,
    /// Remove the current node entirely.
    pub remove: bool,
    /// Whether to recurse into this node's child nodes (when applicable).
    pub recurse: bool,
}

impl InlineVisitControl {
    /// Convenience: keep the (possibly mutated) node and recurse into children.
    pub fn keep_and_recurse() -> Self {
        InlineVisitControl {
            recurse: true,
            ..Default::default()
        }
    }
    /// Convenience: remove the node.
    pub fn remove() -> Self {
        InlineVisitControl {
            remove: true,
            ..Default::default()
        }
    }
    /// Convenience: replace current node with given nodes.
    pub fn replace_with(nodes: Vec<Inline>) -> Self {
        InlineVisitControl {
            replace_with: Some(nodes),
            ..Default::default()
        }
    }
}

// 1 ============
/// Visit and possibly mutate a whole document. Traversal order is pre-order
/// (visitor is called before recursing into children). The visitor may:
/// - mutate the node in-place and return a default/keep control
/// - return insert_before / insert_after vectors
/// - return replace_with to swap the node for one or more nodes
/// - return remove to drop the node entirely
pub fn visit_document_mut<V: PluginFactory + ?Sized>(doc: &mut super::Document, visitor: &mut V) {
    visit_blocks_vec(&mut doc.children, visitor)
}

fn visit_blocks_vec<V: PluginFactory + ?Sized>(children: &mut Vec<Block>, visitor: &mut V) {
    let mut i = 0usize;
    while i < children.len() {
        // take ownership of the node so the visitor can freely replace/remove it
        let mut node = children.remove(i);

        // call visitor
        let ctrl = visitor.visit_block(&mut node);
        // destructure control so we can move fields
        let VisitControl {
            insert_before,
            insert_after,
            replace_with,
            remove,
            recurse,
        } = ctrl;

        // insert_before: put these at the original position
        if !insert_before.is_empty() {
            let before_len = insert_before.len();
            children.splice(i..i, insert_before);
            i += before_len;
        }

        // removal requested
        if remove {
            if !insert_after.is_empty() {
                let after_len = insert_after.len();
                children.splice(i..i, insert_after);
                i += after_len;
            }
            // original removed, continue (don't increment i)
            continue;
        }

        // replacement requested
        if let Some(reps) = replace_with {
            let replacement_len = reps.len();
            children.splice(i..i, reps);
            i += replacement_len;
            continue;
        }

        // keep the (possibly mutated) node; recurse into children if requested
        if recurse {
            match &mut node {
                Block::Heading {
                    children: inline_children,
                    ..
                }
                | Block::Paragraph {
                    children: inline_children,
                    ..
                } => {
                    visit_inlines_vec(inline_children, visitor);
                }
                Block::BlockQuote {
                    children: block_children,
                    ..
                } => {
                    visit_blocks_vec(block_children, visitor);
                }
                Block::List { items, .. } => {
                    for item in items.iter_mut() {
                        visit_blocks_vec(&mut item.children, visitor);
                    }
                }
                Block::Table { table, .. } => {
                    visit_table(table, visitor);
                }
                // CodeBlock, ThematicBreak, HtmlBlock, LinkReferenceDefinition,
                // and Comment have no nested AST children
                _ => {}
            }
        }

        // insert the original (possibly mutated) node back into the vector
        children.insert(i, node);
        i += 1;

        // insert_after
        if !insert_after.is_empty() {
            let after_len = insert_after.len();
            children.splice(i..i, insert_after);
            i += after_len;
        }
    }
}

fn visit_table<V: PluginFactory + ?Sized>(
    table: &mut crate::markdown::ast::tokens::token::Table,
    visitor: &mut V,
) {
    for cell in table.header.cells.iter_mut() {
        visit_inlines_vec(&mut cell.children, visitor);
    }
    for row in table.rows.iter_mut() {
        for cell in row.cells.iter_mut() {
            visit_inlines_vec(&mut cell.children, visitor);
        }
    }
}

fn visit_inlines_vec<V: PluginFactory + ?Sized>(inlines: &mut Vec<Inline>, visitor: &mut V) {
    let mut i = 0usize;
    while i < inlines.len() {
        let mut node = inlines.remove(i);
        let ctrl = visitor.visit_inline(&mut node);
        let InlineVisitControl {
            insert_before,
            insert_after,
            replace_with,
            remove,
            recurse,
        } = ctrl;

        if !insert_before.is_empty() {
            let before_len = insert_before.len();
            inlines.splice(i..i, insert_before);
            i += before_len;
        }

        if remove {
            if !insert_after.is_empty() {
                let after_len = insert_after.len();
                inlines.splice(i..i, insert_after);
                i += after_len;
            }
            continue;
        }
        #[allow(unused_mut)]
        if let Some(reps) = replace_with {
            inlines.splice(i..i, reps);
            continue;
        }

        if recurse {
            match &mut node {
                Inline::Emphasis { children, .. } | Inline::Strikethrough { children, .. } => {
                    visit_inlines_vec(children, visitor);
                }
                Inline::Link { text, .. } | Inline::LinkReference { text, .. } => {
                    visit_inlines_vec(text, visitor);
                }
                // Text, Code, Image, HtmlInline, HardBreak, SoftBreak have no nested inlines
                _ => {}
            }
        }

        inlines.insert(i, node);
        i += 1;

        if !insert_after.is_empty() {
            let after_len = insert_after.len();
            inlines.splice(i..i, insert_after);
            i += after_len;
        }
    }
}
// ---------------------------------------------------------------------------
// Hook registry
// ---------------------------------------------------------------------------

/// Ordered registry of [`PluginFactory`] hooks, threaded through the block and
/// inline parsers.
///
/// Hooks are tried in registration order; the first hook to claim a
/// position wins.
#[derive(Clone, Default)]
pub struct ParserHooks<'a> {
    parsers: Vec<&'a dyn PluginFactory>,
}

impl<'a> ParserHooks<'a> {
    /// An empty registry — no hooks run (the default when none are
    /// registered).
    pub fn empty() -> Self {
        Self {
            parsers: Vec::new(),
        }
    }

    /// Register a hook (appended after any existing hooks).
    pub fn push(&mut self, parser: &'a dyn PluginFactory) {
        self.parsers.push(parser);
    }

    /// Chainable [`push`](Self::push) — returns the registry so calls can
    /// be chained: `ParseHooks::empty().with(&a).with(&b)`.
    pub fn with(mut self, parser: &'a dyn PluginFactory) -> Self {
        self.parsers.push(parser);
        self
    }

    /// `true` when no hooks are registered.
    pub fn is_empty(&self) -> bool {
        self.parsers.is_empty()
    }

    /// Try every registered block hook in order.
    pub fn try_parse_block(&self, ctx: &BlockParserContext) -> Option<(Block, usize)> {
        for parser in &self.parsers {
            if let Some(result) = parser.try_parse_block(ctx) {
                return Some(result);
            }
        }
        None
    }

    /// Try every registered inline hook in order.
    pub fn try_parse_inline(&self, ctx: &InlineParserContext) -> Option<(Inline, usize)> {
        for parser in &self.parsers {
            if let Some(result) = parser.try_parse_inline(ctx) {
                return Some(result);
            }
        }
        None
    }
}

/// Deprecated pre-1.3.0 name for [`ParserHooks`].
#[deprecated(since = "1.3.0", note = "Use `ParserHooks` instead")]
pub type ParseHooks<'a> = ParserHooks<'a>;

// ---------------------------------------------------------------------------
// Back-compat: run pre-1.3.0 `AstParser` / `AstVisitor` implementations
// through the unified `PluginFactory` engine without duplicating any
// traversal or registry logic.  A blanket impl for every deprecated-trait
// implementor would conflict with the new one, so the shims below wrap a
// single hook and expose it as a `PluginFactory`.
// ---------------------------------------------------------------------------

/// Adapter that exposes a deprecated [`AstParser`] as a [`PluginFactory`]
/// (parse hooks only).
///
/// ```ignore
/// let adapter = AstParserAdapter::new(&my_old_hook);
/// let hooks = ParserHooks::empty().with(&adapter);
/// let doc = md_to_ast_with_hooks(md, &opts, None, &hooks);
/// ```
#[allow(deprecated)]
#[deprecated(since = "1.3.0", note = "Use `PluginFactory` instead")]
pub struct AstParserAdapter<'a> {
    pub(crate) inner: &'a dyn AstParser,
}

#[allow(deprecated)]
impl<'a> AstParserAdapter<'a> {
    /// Wrap a deprecated [`AstParser`] hook.
    #[deprecated(since = "1.3.0", note = "implement `PluginFactory` instead")]
    pub fn new(parser: &'a dyn AstParser) -> Self {
        Self { inner: parser }
    }
}

#[allow(deprecated)]
impl PluginFactory for AstParserAdapter<'_> {
    fn try_parse_block(&self, ctx: &BlockParserContext) -> Option<(Block, usize)> {
        self.inner.try_parse_block(ctx)
    }
    fn try_parse_inline(&self, ctx: &InlineParserContext) -> Option<(Inline, usize)> {
        self.inner.try_parse_inline(ctx)
    }
}

/// Adapter that exposes a deprecated [`AstVisitor`] as a [`PluginFactory`]
/// (visit callbacks only).
///
/// ```ignore
/// let mut adapter = AstVisitorAdapter::new(&mut my_old_visitor);
/// visit_document_mut(&mut doc, &mut adapter);
/// ```
#[allow(deprecated)]
#[deprecated(since = "1.3.0", note = "Use `PluginFactory` instead")]
pub struct AstVisitorAdapter<'a> {
    pub(crate) inner: &'a mut dyn AstVisitor,
}

#[allow(deprecated)]
impl<'a> AstVisitorAdapter<'a> {
    /// Wrap a deprecated [`AstVisitor`].
    #[deprecated(since = "1.3.0", note = "implement `PluginFactory` instead")]
    pub fn new(visitor: &'a mut dyn AstVisitor) -> Self {
        Self { inner: visitor }
    }
}

#[allow(deprecated)]
impl PluginFactory for AstVisitorAdapter<'_> {
    fn visit_block(&mut self, block: &mut Block) -> VisitControl {
        self.inner.visit_block(block)
    }
    fn visit_inline(&mut self, inline: &mut Inline) -> InlineVisitControl {
        self.inner.visit_inline(inline)
    }
}

// ---------------------------------------------------------------------------
// Node finalisation helpers
// ---------------------------------------------------------------------------

/// Overwrite a hook-returned block's source span (computed from the number
/// of consumed lines) and apply a Kramdown `{:...}` attribute block if one
/// follows the block in the source.
pub(crate) fn finalize_hook_block(
    block: Block,
    pos: Span,
    trailing_attrs: Option<Attributes>,
) -> Block {
    match block {
        Block::Heading {
            level,
            children,
            attrs,
            ..
        } => Block::Heading {
            level,
            children,
            pos,
            attrs: trailing_attrs.or(attrs),
        },
        Block::Paragraph {
            children, attrs, ..
        } => Block::Paragraph {
            children,
            pos,
            attrs: trailing_attrs.or(attrs),
        },
        Block::CodeBlock {
            lang, code, attrs, ..
        } => Block::CodeBlock {
            lang,
            code,
            pos,
            attrs: trailing_attrs.or(attrs),
        },
        Block::BlockQuote {
            children, attrs, ..
        } => Block::BlockQuote {
            children,
            pos,
            attrs: trailing_attrs.or(attrs),
        },
        Block::List {
            ordered,
            items,
            attrs,
            ..
        } => Block::List {
            ordered,
            items,
            pos,
            attrs: trailing_attrs.or(attrs),
        },
        Block::ThematicBreak { .. } => Block::ThematicBreak { pos },
        Block::HtmlBlock { html, attrs, .. } => Block::HtmlBlock {
            html,
            pos,
            attrs: trailing_attrs.or(attrs),
        },
        Block::Table { table, attrs, .. } => Block::Table {
            table,
            pos,
            attrs: trailing_attrs.or(attrs),
        },
        Block::LinkReferenceDefinition {
            label, url, title, ..
        } => Block::LinkReferenceDefinition {
            label,
            url,
            title,
            pos,
        },
        Block::Comment { value, .. } => Block::Comment { value, pos },
    }
}

/// Overwrite a hook-returned inline node's source span (computed from the
/// number of consumed characters).
pub(crate) fn finalize_hook_inline(node: Inline, pos: Span) -> Inline {
    match node {
        Inline::Text { value, .. } => Inline::Text { value, pos },
        Inline::Emphasis {
            level, children, ..
        } => Inline::Emphasis {
            level,
            children,
            pos,
        },
        Inline::Code { code, .. } => Inline::Code { code, pos },
        Inline::HtmlInline { html, .. } => Inline::HtmlInline { html, pos },
        Inline::Strikethrough { children, .. } => Inline::Strikethrough { children, pos },
        Inline::HardBreak { .. } => Inline::HardBreak { pos },
        Inline::SoftBreak { .. } => Inline::SoftBreak { pos },
        Inline::Image {
            alt, url, title, ..
        } => Inline::Image {
            alt,
            url,
            title,
            pos,
        },
        Inline::Link {
            text,
            url,
            title,
            autolink,
            ..
        } => Inline::Link {
            text,
            url,
            title,
            autolink,
            pos,
        },
        Inline::LinkReference {
            text,
            label,
            url,
            title,
            ..
        } => Inline::LinkReference {
            text,
            label,
            url,
            title,
            pos,
        },
    }
}

// ------------------------------------------------------------------------------------
#[allow(deprecated)]
#[deprecated(since = "1.3.0", note = "Use `visit_document_mut` instead")]
pub fn visit_document_mut_dep<V: AstVisitor + ?Sized>(doc: &mut super::Document, visitor: &mut V) {
    visit_blocks_vec_dep(&mut doc.children, visitor)
}
#[allow(deprecated)]
fn visit_blocks_vec_dep<V: AstVisitor + ?Sized>(children: &mut Vec<Block>, visitor: &mut V) {
    let mut i = 0usize;
    while i < children.len() {
        // take ownership of the node so the visitor can freely replace/remove it
        let mut node = children.remove(i);

        // call visitor
        let ctrl = visitor.visit_block(&mut node);
        // destructure control so we can move fields
        let VisitControl {
            insert_before,
            insert_after,
            replace_with,
            remove,
            recurse,
        } = ctrl;

        // insert_before: put these at the original position
        if !insert_before.is_empty() {
            let before_len = insert_before.len();
            children.splice(i..i, insert_before);
            i += before_len;
        }

        // removal requested
        if remove {
            if !insert_after.is_empty() {
                let after_len = insert_after.len();
                children.splice(i..i, insert_after);
                i += after_len;
            }
            // original removed, continue (don't increment i)
            continue;
        }

        // replacement requested
        if let Some(reps) = replace_with {
            let replacement_len = reps.len();
            children.splice(i..i, reps);
            i += replacement_len;
            continue;
        }

        // keep the (possibly mutated) node; recurse into children if requested
        if recurse {
            match &mut node {
                Block::Heading {
                    children: inline_children,
                    ..
                }
                | Block::Paragraph {
                    children: inline_children,
                    ..
                } => {
                    visit_inlines_vec_dep(inline_children, visitor);
                }
                Block::BlockQuote {
                    children: block_children,
                    ..
                } => {
                    visit_blocks_vec_dep(block_children, visitor);
                }
                Block::List { items, .. } => {
                    for item in items.iter_mut() {
                        visit_blocks_vec_dep(&mut item.children, visitor);
                    }
                }
                Block::Table { table, .. } => {
                    visit_table_dep(table, visitor);
                }
                // CodeBlock, ThematicBreak, HtmlBlock, LinkReferenceDefinition,
                // and Comment have no nested AST children
                _ => {}
            }
        }

        // insert the original (possibly mutated) node back into the vector
        children.insert(i, node);
        i += 1;

        // insert_after
        if !insert_after.is_empty() {
            let after_len = insert_after.len();
            children.splice(i..i, insert_after);
            i += after_len;
        }
    }
}
#[allow(deprecated)]
fn visit_table_dep<V: AstVisitor + ?Sized>(
    table: &mut crate::markdown::ast::tokens::token::Table,
    visitor: &mut V,
) {
    for cell in table.header.cells.iter_mut() {
        visit_inlines_vec_dep(&mut cell.children, visitor);
    }
    for row in table.rows.iter_mut() {
        for cell in row.cells.iter_mut() {
            visit_inlines_vec_dep(&mut cell.children, visitor);
        }
    }
}
#[allow(deprecated)]
fn visit_inlines_vec_dep<V: AstVisitor + ?Sized>(inlines: &mut Vec<Inline>, visitor: &mut V) {
    let mut i = 0usize;
    while i < inlines.len() {
        let mut node = inlines.remove(i);
        let ctrl = visitor.visit_inline(&mut node);
        let InlineVisitControl {
            insert_before,
            insert_after,
            replace_with,
            remove,
            recurse,
        } = ctrl;

        if !insert_before.is_empty() {
            let before_len = insert_before.len();
            inlines.splice(i..i, insert_before);
            i += before_len;
        }

        if remove {
            if !insert_after.is_empty() {
                let after_len = insert_after.len();
                inlines.splice(i..i, insert_after);
                i += after_len;
            }
            continue;
        }
        #[allow(unused_mut)]
        if let Some(reps) = replace_with {
            inlines.splice(i..i, reps);
            continue;
        }

        if recurse {
            match &mut node {
                Inline::Emphasis { children, .. } | Inline::Strikethrough { children, .. } => {
                    visit_inlines_vec_dep(children, visitor);
                }
                Inline::Link { text, .. } | Inline::LinkReference { text, .. } => {
                    visit_inlines_vec_dep(text, visitor);
                }
                // Text, Code, Image, HtmlInline, HardBreak, SoftBreak have no nested inlines
                _ => {}
            }
        }

        inlines.insert(i, node);
        i += 1;

        if !insert_after.is_empty() {
            let after_len = insert_after.len();
            inlines.splice(i..i, insert_after);
            i += after_len;
        }
    }
}