dioxus-nox-markdown 0.13.2

Headless markdown editor, previewer, and display components for Dioxus
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
use crop::Rope;
use dioxus::prelude::*;
use pulldown_cmark::{Event, Options, Parser, Tag};
use std::ops::Range;

use crate::types::{HeadingEntry, HtmlRenderPolicy, NodeType, OwnedAstNode, ParsedDoc};

/// Configuration for rendering the AST to Dioxus elements.
#[derive(Debug, Clone)]
pub(crate) struct RenderConfig<'a> {
    pub html_render_policy: HtmlRenderPolicy,
    pub highlight_class_prefix: &'a str,
    pub show_code_line_numbers: bool,
    pub show_code_language: bool,
}

pub enum CustomEvent<'a> {
    Standard(Event<'a>),
    Wikilink(String),
    Tag(String),
}

/// Build pulldown-cmark options with GFM extensions enabled.
pub(crate) fn build_cmark_options() -> Options {
    let mut opts = Options::empty();
    opts.insert(Options::ENABLE_STRIKETHROUGH);
    opts.insert(Options::ENABLE_TABLES);
    opts.insert(Options::ENABLE_TASKLISTS);
    opts.insert(Options::ENABLE_FOOTNOTES);
    opts.insert(Options::ENABLE_YAML_STYLE_METADATA_BLOCKS);
    opts
}

/// A node in our custom AST, mapped directly to byte ranges in the Rope.
pub struct AstNode<'a> {
    pub event: CustomEvent<'a>,
    pub range: Range<usize>,
    pub children: Vec<AstNode<'a>>,
}

/// Parse a markdown rope into a `ParsedDoc`.
///
/// Builds a hierarchical AST using pulldown-cmark events, preserving exact byte
/// offsets for the LivePreview mapping.
pub fn parse_document(input: &Rope) -> ParsedDoc {
    parse_document_with_policy(input, HtmlRenderPolicy::Escape)
}

/// Parse a markdown rope into a `ParsedDoc` with explicit HTML render policy.
pub fn parse_document_with_policy(input: &Rope, html_render_policy: HtmlRenderPolicy) -> ParsedDoc {
    parse_document_full(input, html_render_policy, "hl-")
}

/// Parse a markdown rope with explicit HTML render policy and highlight class prefix.
///
/// `highlight_class_prefix` is prepended to CSS class names in syntax-highlighted
/// code blocks (e.g. `"hl-"` produces `<span class="hl-keyword">`).
pub fn parse_document_full(
    input: &Rope,
    html_render_policy: HtmlRenderPolicy,
    highlight_class_prefix: &str,
) -> ParsedDoc {
    parse_document_full_with_config(
        input,
        html_render_policy,
        highlight_class_prefix,
        false,
        true,
    )
}

/// Parse a markdown rope with full configuration (including code block display options).
pub fn parse_document_full_with_config(
    input: &Rope,
    html_render_policy: HtmlRenderPolicy,
    highlight_class_prefix: &str,
    show_code_line_numbers: bool,
    show_code_language: bool,
) -> ParsedDoc {
    let text = input.to_string(); // Temporary string for parsing, rope backing to come next if needed
    let opts = build_cmark_options();
    let parser = Parser::new_ext(&text, opts).into_offset_iter();

    let mut root_children = Vec::new();
    let mut stack: Vec<AstNode> = Vec::new();

    let mut headings = Vec::new();
    let mut front_matter = None;

    // A simple builder to convert the flat event stream into a tree
    for (event, range) in parser {
        match event {
            Event::Start(_) => {
                stack.push(AstNode {
                    event: CustomEvent::Standard(event),
                    range,
                    children: Vec::new(),
                });
            }
            Event::End(_tag_end) => {
                let mut node = stack.pop().expect("Mismatched End event in parser");
                // Update the range to encompass the entire node
                node.range.end = range.end;

                // Track headings
                if let CustomEvent::Standard(Event::Start(Tag::Heading { level, .. })) = &node.event
                {
                    let text_content = extract_text(&node);

                    let mut anchor = String::new();
                    let mut last_was_dash = false;
                    for c in text_content.to_lowercase().chars() {
                        if c.is_alphanumeric() {
                            anchor.push(c);
                            last_was_dash = false;
                        } else if !last_was_dash {
                            anchor.push('-');
                            last_was_dash = true;
                        }
                    }
                    let anchor = anchor.trim_matches('-').to_string();

                    headings.push(HeadingEntry {
                        level: *level as u8,
                        text: text_content.clone(),
                        anchor,
                        line: index_to_line_col(&text, node.range.start).0,
                    });
                } else if let CustomEvent::Standard(Event::Start(Tag::MetadataBlock(_))) =
                    &node.event
                {
                    front_matter = Some(text[node.range.clone()].to_string());
                }

                if let Some(parent) = stack.last_mut() {
                    parent.children.push(node);
                } else {
                    root_children.push(node);
                }
            }
            _ => {
                let node = AstNode {
                    event: CustomEvent::Standard(event),
                    range,
                    children: Vec::new(),
                };
                if let Some(parent) = stack.last_mut() {
                    parent.children.push(node);
                } else {
                    root_children.push(node);
                }
            }
        }
    }

    // Pass 2: Replace standard text nodes with custom Wikilink and Tag nodes
    second_pass_custom_extensions(&mut root_children, &text);

    let config = RenderConfig {
        html_render_policy,
        highlight_class_prefix,
        show_code_line_numbers,
        show_code_language,
    };
    let element = render_ast_to_element(&root_children, &config);
    let ast = root_children.iter().filter_map(to_owned_node).collect();

    ParsedDoc {
        element,
        headings,
        front_matter,
        blocks: Vec::new(), // Inline Blocks to be completely removed/rewritten
        ast,
    }
}

fn to_owned_node(node: &AstNode) -> Option<OwnedAstNode> {
    let node_type = match &node.event {
        CustomEvent::Standard(Event::Start(tag)) => match tag {
            Tag::Paragraph => NodeType::Paragraph,
            Tag::Heading { level, .. } => NodeType::Heading(*level as u8),
            Tag::BlockQuote(_) => NodeType::BlockQuote,
            Tag::CodeBlock(pulldown_cmark::CodeBlockKind::Fenced(info)) => {
                let lang = info.split_whitespace().next().unwrap_or("").to_string();
                NodeType::CodeBlock(lang)
            }
            Tag::CodeBlock(pulldown_cmark::CodeBlockKind::Indented) => {
                NodeType::CodeBlock(String::new())
            }
            Tag::List(start) => NodeType::List(*start),
            Tag::Item => NodeType::Item,
            Tag::Emphasis => NodeType::Emphasis,
            Tag::Strong => NodeType::Strong,
            Tag::Strikethrough => NodeType::Strikethrough,
            Tag::Link {
                dest_url, title, ..
            } => NodeType::Link {
                url: dest_url.to_string(),
                title: title.to_string(),
            },
            Tag::Image {
                dest_url, title, ..
            } => NodeType::Image {
                url: dest_url.to_string(),
                title: title.to_string(),
            },
            Tag::Table(_) => NodeType::Table,
            Tag::TableHead => NodeType::TableHead,
            Tag::TableRow => NodeType::TableRow,
            Tag::TableCell => NodeType::TableCell,
            Tag::FootnoteDefinition(s) => NodeType::FootnoteReference(s.to_string()),
            Tag::HtmlBlock => NodeType::HtmlBlock,
            Tag::MetadataBlock(_) => NodeType::Rule,
            Tag::DefinitionList => NodeType::DefinitionList,
            Tag::DefinitionListTitle => NodeType::DefinitionListTitle,
            Tag::DefinitionListDefinition => NodeType::DefinitionListDefinition,
            Tag::Superscript => NodeType::Superscript,
            Tag::Subscript => NodeType::Subscript,
        },
        CustomEvent::Standard(Event::Text(t)) => NodeType::Text(t.to_string()),
        CustomEvent::Standard(Event::Code(c)) => NodeType::Code(c.to_string()),
        CustomEvent::Standard(Event::Html(h)) | CustomEvent::Standard(Event::InlineHtml(h)) => {
            NodeType::Html(h.to_string())
        }
        CustomEvent::Standard(Event::SoftBreak) => NodeType::SoftBreak,
        CustomEvent::Standard(Event::HardBreak) => NodeType::HardBreak,
        CustomEvent::Standard(Event::Rule) => NodeType::Rule,
        CustomEvent::Standard(Event::TaskListMarker(b)) => NodeType::TaskListMarker(*b),
        CustomEvent::Standard(Event::FootnoteReference(f)) => {
            NodeType::FootnoteReference(f.to_string())
        }
        CustomEvent::Wikilink(link) => NodeType::Wikilink(link.clone()),
        CustomEvent::Tag(tag) => NodeType::Tag(tag.clone()),
        _ => return None,
    };

    Some(OwnedAstNode {
        node_type,
        range: node.range.clone(),
        children: node.children.iter().filter_map(to_owned_node).collect(),
    })
}

fn extract_text(node: &AstNode) -> String {
    let mut buf = String::new();
    for child in &node.children {
        match &child.event {
            CustomEvent::Standard(Event::Text(t)) => buf.push_str(t),
            CustomEvent::Standard(Event::Code(c)) => buf.push_str(c),
            CustomEvent::Wikilink(link) => buf.push_str(link),
            CustomEvent::Tag(tag) => buf.push_str(tag),
            _ => buf.push_str(&extract_text(child)),
        }
    }
    buf
}

/// Convert a byte offset into (line, column) for a given text.
/// Both line and column are 0-based.
pub fn index_to_line_col(text: &str, index: usize) -> (usize, usize) {
    let before = &text[..index];
    let line = before.bytes().filter(|&b| b == b'\n').count();
    let col = match before.rfind('\n') {
        Some(nl_pos) => index - nl_pos - 1,
        None => index,
    };
    (line, col)
}

/// Helper: sanitize href strings.
pub(crate) fn sanitize_href(raw: &str) -> Option<String> {
    let trimmed = raw.trim();
    if trimmed.is_empty() {
        return Some(String::new());
    }
    if trimmed.starts_with('#') || trimmed.starts_with('/') {
        return Some(trimmed.to_string());
    }
    let lower = trimmed.to_lowercase();
    if let Some(colon_pos) = lower.find(':') {
        let scheme = &lower[..colon_pos];
        match scheme {
            "javascript" | "data" | "vbscript" => return None,
            _ => return Some(trimmed.to_string()),
        }
    }
    Some(trimmed.to_string())
}

/// Render the AST tree into a Dioxus Element.
pub(crate) fn render_ast_to_element(children: &[AstNode], config: &RenderConfig) -> Element {
    let kids: Vec<Element> = children
        .iter()
        .map(|node| render_node(node, config))
        .collect();
    rsx! {
        for child in kids {
            {child}
        }
    }
}

fn render_node(node: &AstNode, config: &RenderConfig) -> Element {
    let start_str = node.range.start.to_string();
    let end_str = node.range.end.to_string();

    match &node.event {
        CustomEvent::Standard(Event::Start(tag)) => {
            render_tag(tag, &node.children, node.range.clone(), config)
        }
        CustomEvent::Standard(Event::Text(t)) => {
            let txt = t.to_string();
            rsx! { "{txt}" }
        }
        CustomEvent::Standard(Event::Code(c)) => {
            let code = c.to_string();
            rsx! { code { "{code}" } }
        }
        CustomEvent::Standard(Event::SoftBreak) => rsx! { " " },
        CustomEvent::Standard(Event::HardBreak) => rsx! { br {} },
        CustomEvent::Standard(Event::Rule) => {
            rsx! { hr { "data-source-start": "{start_str}", "data-source-end": "{end_str}" } }
        }
        CustomEvent::Standard(Event::Html(h)) | CustomEvent::Standard(Event::InlineHtml(h)) => {
            let html = h.to_string();
            match config.html_render_policy {
                HtmlRenderPolicy::Trusted => {
                    rsx! { span { dangerous_inner_html: "{html}" } }
                }
                #[cfg(feature = "sanitize")]
                HtmlRenderPolicy::Sanitized => {
                    let clean = ammonia::clean(&html);
                    rsx! { span { dangerous_inner_html: "{clean}" } }
                }
                // Escape (default) — and Sanitized fallback when feature is disabled
                _ => {
                    rsx! { span { "{html}" } }
                }
            }
        }
        CustomEvent::Standard(Event::TaskListMarker(checked)) => {
            let is_checked = *checked;
            rsx! { input { r#type: "checkbox", checked: is_checked, disabled: true } }
        }
        CustomEvent::Standard(Event::FootnoteReference(f)) => {
            let f_str = f.to_string();
            rsx! { sup { a { href: "#fn-{f_str}", "data-md-footnote-ref": "{f_str}", "{f_str}" } } }
        }
        CustomEvent::Tag(tag) => {
            rsx! { span { "data-md-tag": "{tag}", "data-source-start": "{start_str}", "data-source-end": "{end_str}", "{tag}" } }
        }
        CustomEvent::Wikilink(link) => {
            rsx! { a { "data-md-wikilink": "{link}", "data-source-start": "{start_str}", "data-source-end": "{end_str}", "[[{link}]]" } }
        }
        _ => rsx! { span {} },
    }
}

fn render_tag(
    tag: &Tag,
    children: &[AstNode],
    range: Range<usize>,
    config: &RenderConfig,
) -> Element {
    let start_str = range.start.to_string();
    let end_str = range.end.to_string();
    let kids_elements: Vec<Element> = children
        .iter()
        .map(|node| render_node(node, config))
        .collect();
    let kids = rsx! { for c in kids_elements { {c} } };

    match tag {
        Tag::Paragraph => {
            rsx! { p { "data-source-start": "{start_str}", "data-source-end": "{end_str}", {kids} } }
        }
        Tag::Heading { level, .. } => {
            let l = *level as u8;
            match l {
                1 => {
                    rsx! { h1 { "data-source-start": "{start_str}", "data-source-end": "{end_str}", {kids} } }
                }
                2 => {
                    rsx! { h2 { "data-source-start": "{start_str}", "data-source-end": "{end_str}", {kids} } }
                }
                3 => {
                    rsx! { h3 { "data-source-start": "{start_str}", "data-source-end": "{end_str}", {kids} } }
                }
                4 => {
                    rsx! { h4 { "data-source-start": "{start_str}", "data-source-end": "{end_str}", {kids} } }
                }
                5 => {
                    rsx! { h5 { "data-source-start": "{start_str}", "data-source-end": "{end_str}", {kids} } }
                }
                _ => {
                    rsx! { h6 { "data-source-start": "{start_str}", "data-source-end": "{end_str}", {kids} } }
                }
            }
        }
        Tag::BlockQuote(_) => {
            rsx! { blockquote { "data-source-start": "{start_str}", "data-source-end": "{end_str}", {kids} } }
        }
        Tag::CodeBlock(pulldown_cmark::CodeBlockKind::Fenced(info)) => {
            let lang = info.split_whitespace().next().unwrap_or("").to_string();
            let code_text = extract_text_from_children(children);
            let mut result =
                crate::highlight::highlight_code(&code_text, &lang, config.highlight_class_prefix);
            if config.show_code_line_numbers {
                result.html = crate::highlight::wrap_with_line_numbers(&result.html);
            }
            let highlighted_attr = result.language_matched.then_some("true");
            let line_numbers_attr = config.show_code_line_numbers.then_some("");
            let show_lang_header = config.show_code_language && !lang.is_empty();
            rsx! {
                pre {
                    "data-md-code-block": "",
                    "data-md-language": "{lang}",
                    "data-md-highlighted": highlighted_attr,
                    "data-md-line-numbers": line_numbers_attr,
                    "data-source-start": "{start_str}",
                    "data-source-end": "{end_str}",
                    if show_lang_header {
                        div {
                            "data-md-code-header": "",
                            span { "data-md-code-language": "", "{lang}" }
                        }
                    }
                    code {
                        class: "language-{lang}",
                        dangerous_inner_html: "{result.html}"
                    }
                }
            }
        }
        Tag::CodeBlock(pulldown_cmark::CodeBlockKind::Indented) => {
            let code_text = extract_text_from_children(children);
            let mut result =
                crate::highlight::highlight_code(&code_text, "", config.highlight_class_prefix);
            if config.show_code_line_numbers {
                result.html = crate::highlight::wrap_with_line_numbers(&result.html);
            }
            let line_numbers_attr = config.show_code_line_numbers.then_some("");
            rsx! {
                pre {
                    "data-md-code-block": "",
                    "data-md-line-numbers": line_numbers_attr,
                    "data-source-start": "{start_str}",
                    "data-source-end": "{end_str}",
                    code {
                        dangerous_inner_html: "{result.html}"
                    }
                }
            }
        }
        Tag::List(Some(start_idx)) => {
            let idx = *start_idx as i64;
            rsx! { ol { start: "{idx}", "data-source-start": "{start_str}", "data-source-end": "{end_str}", {kids} } }
        }
        Tag::List(None) => {
            rsx! { ul { "data-source-start": "{start_str}", "data-source-end": "{end_str}", {kids} } }
        }
        Tag::Item => {
            // TaskListMarker handling: check if the first child is a TaskListMarker.
            let is_task = children.first().is_some_and(|c| {
                matches!(c.event, CustomEvent::Standard(Event::TaskListMarker(_)))
            });
            if is_task {
                let checked_str = match &children.first().unwrap().event {
                    CustomEvent::Standard(Event::TaskListMarker(checked)) => {
                        if *checked {
                            "true"
                        } else {
                            "false"
                        }
                    }
                    _ => "false",
                };
                rsx! {
                    li {
                        "data-md-task-item": "",
                        "data-md-task-checked": "{checked_str}",
                        "data-source-start": "{start_str}",
                        "data-source-end": "{end_str}",
                        {kids}
                    }
                }
            } else {
                rsx! { li { "data-source-start": "{start_str}", "data-source-end": "{end_str}", {kids} } }
            }
        }
        Tag::Emphasis => {
            rsx! { em { "data-source-start": "{start_str}", "data-source-end": "{end_str}", {kids} } }
        }
        Tag::Strong => {
            rsx! { strong { "data-source-start": "{start_str}", "data-source-end": "{end_str}", {kids} } }
        }
        Tag::Strikethrough => {
            rsx! { del { "data-source-start": "{start_str}", "data-source-end": "{end_str}", {kids} } }
        }
        Tag::Link {
            dest_url, title, ..
        } => {
            let safe_url = sanitize_href(dest_url).unwrap_or_default();
            let title_str = title.to_string();
            let is_external = safe_url.starts_with("http://") || safe_url.starts_with("https://");
            let external_str = if is_external { "true" } else { "false" };
            rsx! {
                a {
                    href: "{safe_url}",
                    title: "{title_str}",
                    "data-md-link": "",
                    "data-md-link-external": "{external_str}",
                    "data-source-start": "{start_str}",
                    "data-source-end": "{end_str}",
                    {kids}
                }
            }
        }
        Tag::Image {
            dest_url, title, ..
        } => {
            let safe_url = sanitize_href(dest_url).unwrap_or_default();
            let title_str = title.to_string();
            // Alt text is rendered by the kids, but for an img tag we need plaintext alt
            let alt_str = extract_text_from_children(children);
            rsx! {
                img {
                    src: "{safe_url}",
                    alt: "{alt_str}",
                    title: "{title_str}",
                    "data-source-start": "{start_str}",
                    "data-source-end": "{end_str}"
                }
            }
        }
        Tag::Table(_) => rsx! {
            div {
                "data-md-table-wrapper": "",
                "data-source-start": "{start_str}",
                "data-source-end": "{end_str}",
                table { {kids} }
            }
        },
        Tag::TableHead => rsx! { thead { tr { {kids} } } },
        Tag::TableRow => rsx! { tr { {kids} } },
        Tag::TableCell => rsx! { td { {kids} } },
        Tag::FootnoteDefinition(name) => {
            let name_str = name.to_string();
            rsx! {
                div {
                    "data-md-footnote-def": "{name_str}",
                    "data-source-start": "{start_str}",
                    "data-source-end": "{end_str}",
                    {kids}
                }
            }
        }
        Tag::HtmlBlock => {
            rsx! { div { "data-source-start": "{start_str}", "data-source-end": "{end_str}", {kids} } }
        }
        Tag::MetadataBlock(_) => rsx! { div { display: "none" } },
        _ => {
            rsx! { span { "data-source-start": "{start_str}", "data-source-end": "{end_str}", {kids} } }
        }
    }
}

fn extract_text_from_children(children: &[AstNode]) -> String {
    let mut buf = String::new();
    for child in children {
        if let CustomEvent::Standard(Event::Text(t)) = &child.event {
            buf.push_str(t);
        } else {
            buf.push_str(&extract_text_from_children(&child.children));
        }
    }
    buf
}

fn second_pass_custom_extensions<'a>(nodes: &mut Vec<AstNode<'a>>, _text_source: &str) {
    // Pre-process: merge adjacent Text nodes because pulldown_cmark can fragment failed reference links like `[[`
    let mut merged: Vec<AstNode<'a>> = Vec::new();
    for node in nodes.drain(..) {
        if let CustomEvent::Standard(Event::Text(ref t)) = node.event
            && let Some(last) = merged.last_mut()
            && let CustomEvent::Standard(Event::Text(ref mut last_t)) = last.event
        {
            let mut combined = last_t.to_string();
            combined.push_str(t);
            last.event = CustomEvent::Standard(Event::Text(combined.into()));
            last.range.end = node.range.end;
            continue;
        }
        merged.push(node);
    }
    *nodes = merged;

    let mut new_nodes = Vec::new();
    let mut replaced = false;

    for node in nodes.drain(..) {
        let maybe_text = match &node.event {
            CustomEvent::Standard(Event::Text(text)) => Some(text.to_string()),
            _ => None,
        };

        if let Some(text) = maybe_text {
            // Find hashtags and wikilinks inside the text node with native scanning.
            let mut matches = scan_custom_tokens(&text);

            if matches.is_empty() {
                new_nodes.push(node);
                continue;
            }

            replaced = true;
            matches.sort_by_key(|a| a.0);

            let mut last_idx = 0;
            let start_offset = node.range.start;

            for (m_start, m_end, token) in matches {
                if m_start > last_idx {
                    // Push standard text node before the match
                    let slice = &text[last_idx..m_start];
                    new_nodes.push(AstNode {
                        event: CustomEvent::Standard(Event::Text(slice.to_string().into())),
                        range: (start_offset + last_idx)..(start_offset + m_start),
                        children: Vec::new(),
                    });
                }

                // Push custom event
                let event = match token {
                    TokenKind::Tag(tag) => CustomEvent::Tag(tag),
                    TokenKind::Wikilink(link) => CustomEvent::Wikilink(link),
                };
                new_nodes.push(AstNode {
                    event,
                    range: (start_offset + m_start)..(start_offset + m_end),
                    children: Vec::new(),
                });

                last_idx = m_end;
            }

            // Push remainder
            if last_idx < text.len() {
                let slice = &text[last_idx..text.len()];
                new_nodes.push(AstNode {
                    event: CustomEvent::Standard(Event::Text(slice.to_string().into())),
                    range: (start_offset + last_idx)..(start_offset + text.len()),
                    children: Vec::new(),
                });
            }
        } else {
            // Recurse heavily into children
            let mut recursed_node = node;
            second_pass_custom_extensions(&mut recursed_node.children, _text_source);
            new_nodes.push(recursed_node);
        }
    }

    if replaced || !new_nodes.is_empty() {
        *nodes = new_nodes;
    }
}

enum TokenKind {
    Tag(String),
    Wikilink(String),
}

fn scan_custom_tokens(text: &str) -> Vec<(usize, usize, TokenKind)> {
    let bytes = text.as_bytes();
    let mut out = Vec::new();
    let mut i = 0usize;

    while i < bytes.len() {
        // Wikilink: [[...]]
        if bytes[i] == b'[' && i + 1 < bytes.len() && bytes[i + 1] == b'[' {
            let mut j = i + 2;
            let mut found = None;
            while j + 1 < bytes.len() {
                if bytes[j] == b']' && bytes[j + 1] == b']' {
                    found = Some(j);
                    break;
                }
                j += 1;
            }
            if let Some(end_open) = found {
                let inner = &text[i + 2..end_open];
                out.push((i, end_open + 2, TokenKind::Wikilink(inner.to_string())));
                i = end_open + 2;
                continue;
            }
        }

        // Tag: #[A-Za-z0-9_-]+
        if bytes[i] == b'#' {
            let mut j = i + 1;
            while j < bytes.len() {
                let b = bytes[j];
                let valid = (b as char).is_ascii_alphanumeric() || b == b'_' || b == b'-';
                if valid {
                    j += 1;
                } else {
                    break;
                }
            }
            if j > i + 1 {
                out.push((i, j, TokenKind::Tag(text[i..j].to_string())));
                i = j;
                continue;
            }
        }

        let ch_len = text[i..].chars().next().map(|c| c.len_utf8()).unwrap_or(1);
        i += ch_len;
    }

    out
}