socketry-markdown 0.1.1

CommonMark compliant markdown parser in Rust with ASTs and extensions
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
// Released under the MIT License.
// Copyright, 2024, by Bnchi.
// Copyright, 2024-2025, by Titus Wormer.
// Copyright, 2026, by Samuel Williams.

//! State.
//!
//! JS equivalent: https://github.com/syntax-tree/mdast-util-to-markdown/blob/fd6a508/lib/types.js#L195.
use crate::markdown::{
    association::Association,
    construct_name::ConstructName,
    handle::{
        emphasis::peek_emphasis, html::peek_html, image::peek_image,
        image_reference::peek_image_reference, inline_code::peek_inline_code,
        inline_math::peek_inline_math, link::peek_link, link_reference::peek_link_reference,
        strong::peek_strong, Handle,
    },
    r#unsafe::Unsafe,
    util::{
        format_code_as_indented::format_code_as_indented,
        format_heading_as_setext::format_heading_as_setext,
        pattern_in_scope::pattern_in_scope,
        safe::{escape_backslashes, EscapeInfos, SafeConfig},
    },
    Options,
};
use crate::{
    mdast::{AttributeContent, AttributeValue, Node},
    message::Message,
};
use alloc::{
    collections::BTreeMap,
    format,
    string::{String, ToString},
    vec::Vec,
};
use regex::{Captures, Regex, RegexBuilder};

pub struct Info<'a> {
    pub after: &'a str,
    pub before: &'a str,
}

#[derive(Debug)]
/// Different ways to join two (container, flow) nodes.
enum Join {
    /// Join the two nodes with `1` blank line.
    Break,
    /// Join the two nodes with an HTML comment.
    HtmlComment,
    /// Join the two nodes with `d` blank lines.
    Lines(usize),
}

pub struct State<'a> {
    pub bullet_current: Option<char>,
    pub bullet_last_used: Option<char>,
    pub index_stack: Vec<usize>,
    pub options: &'a Options,
    pub stack: Vec<ConstructName>,
    pub r#unsafe: Vec<Unsafe<'a>>,
}

impl<'a> Info<'a> {
    pub fn new(before: &'a str, after: &'a str) -> Self {
        Info { after, before }
    }
}

impl<'a> State<'a> {
    /// JS equivalent: <https://github.com/syntax-tree/mdast-util-to-markdown/blob/main/lib/util/association.js>.
    pub fn association(&self, node: &impl Association) -> String {
        if node.label().is_some() || node.identifier().is_empty() {
            return node.label().clone().unwrap_or_default();
        }

        let character_escape_or_reference =
            RegexBuilder::new(r"\\([!-/:-@\[-`{-~])|&(#(?:\d{1,7}|x[\da-f]{1,6})|[\da-z]{1,31});")
                .case_insensitive(true)
                .build()
                .unwrap();

        character_escape_or_reference
            .replace_all(node.identifier(), Self::decode)
            .into_owned()
    }

    /// JS equivalent: <https://github.com/syntax-tree/mdast-util-to-markdown/blob/fd6a508/lib/util/container-flow.js#L66>.
    fn between(&self, left: &Node, right: &Node, parent: &Node, results: &mut String) {
        if self.options.tight_definitions {
            Self::set_between(&self.tight_definition(left, right), results)
        } else {
            Self::set_between(&self.join_defaults(left, right, parent), results)
        }
    }

    /// JS equivalent: <https://github.com/syntax-tree/mdast-util-to-markdown/blob/fd6a508/lib/util/compile-pattern.js>.
    pub fn compile_pattern(pattern: &mut Unsafe) {
        if pattern.compiled.is_none() {
            let mut pattern_to_compile = String::new();

            if let Some(pattern_before) = pattern.before {
                pattern_to_compile.push('(');
                if pattern.at_break {
                    pattern_to_compile.push_str("[\\r\\n][\\t ]*");
                }
                pattern_to_compile.push_str("(?:");
                pattern_to_compile.push_str(pattern_before);
                pattern_to_compile.push(')');
                pattern_to_compile.push(')');
            } else if pattern.at_break {
                pattern_to_compile.push('(');
                pattern_to_compile.push_str("[\\r\\n][\\t ]*");
                pattern_to_compile.push(')');
            }

            if matches!(
                pattern.character,
                '|' | '\\'
                    | '{'
                    | '}'
                    | '('
                    | ')'
                    | '['
                    | ']'
                    | '^'
                    | '$'
                    | '+'
                    | '*'
                    | '?'
                    | '.'
                    | '-'
            ) {
                pattern_to_compile.push('\\');
            }

            pattern_to_compile.push(pattern.character);

            if let Some(pattern_after) = pattern.after {
                pattern_to_compile.push_str("(?:");
                pattern_to_compile.push_str(pattern_after);
                pattern_to_compile.push(')');
            }

            pattern.set_compiled(
                Regex::new(&pattern_to_compile).expect("A valid unsafe regex pattern"),
            );
        }
    }

    /// JS equivalent: <https://github.com/syntax-tree/mdast-util-to-markdown/blob/main/lib/util/container-flow.js>.
    pub fn container_flow(&mut self, parent: &Node) -> Result<String, Message> {
        let children = parent.children().expect("The node to be a flow parent.");

        if children.is_empty() {
            return Ok(String::new());
        }

        let mut results: String = String::new();
        let mut children_iter = children.iter().peekable();
        let mut index = 0;

        self.index_stack.push(0);

        while let Some(child) = children_iter.next() {
            if index > 0 {
                let top = self
                    .index_stack
                    .last_mut()
                    .expect("The stack is populated with at least one child position");
                *top = index;
            }

            if !matches!(child, Node::List(_)) {
                self.bullet_last_used = None;
            }

            results.push_str(&self.handle(child, &Info::new("\n", "\n"), Some(parent))?);

            if let Some(next_child) = children_iter.peek() {
                self.between(child, next_child, parent, &mut results);
            }

            index += 1;
        }

        self.index_stack.pop();

        Ok(results)
    }

    /// JS equivalent: <https://github.com/syntax-tree/mdast-util-to-markdown/blob/main/lib/util/container-phrasing.js>.
    pub fn container_phrasing(&mut self, parent: &Node, info: &Info) -> Result<String, Message> {
        let children = parent
            .children()
            .expect("The node to be a phrasing parent.");

        if children.is_empty() {
            return Ok(String::new());
        }

        let mut results: String = String::new();
        let mut index = 0;
        let mut children_iter = children.iter().peekable();

        self.index_stack.push(0);

        while let Some(child) = children_iter.next() {
            if index > 0 {
                let top = self
                    .index_stack
                    .last_mut()
                    .expect("The stack is populated with at least one child position");
                *top = index;
            }

            let mut new_info = Info::new(info.before, info.after);
            let mut buffer = [0u8; 4];
            if let Some(child) = children_iter.peek() {
                if let Some(first_char) = self.peek_node(child) {
                    new_info.after = first_char.encode_utf8(&mut buffer);
                } else {
                    new_info.after = self
                        .handle(child, &Info::new("", ""), Some(parent))?
                        .chars()
                        .nth(0)
                        .unwrap_or_default()
                        .encode_utf8(&mut buffer);
                }
            }

            // In some cases, html (text) can be found in phrasing right after an eol.
            // When we’d serialize that, in most cases that would be seen as html
            // (flow).
            // As we can’t escape or so to prevent it from happening, we take a somewhat
            // reasonable approach: replace that eol with a space.
            // See: <https://github.com/syntax-tree/mdast-util-to-markdown/issues/15>
            if !results.is_empty() {
                if info.before == "\r" || info.before == "\n" && matches!(child, Node::Html(_)) {
                    // TODO Remove this check here it might not be needed since we're
                    // checking for the before info.
                    if results.ends_with('\n') || results.ends_with('\r') {
                        results.pop();
                        if results.ends_with('\r') {
                            results.pop();
                        }
                    }
                    results.push(' ');
                    new_info.before = " ";
                } else {
                    new_info.before = &results;
                }
            }

            results.push_str(&self.handle(child, &new_info, Some(parent))?);
            index += 1;
        }

        self.index_stack.pop();

        Ok(results)
    }

    /// JS equvialent: <https://github.com/micromark/micromark/blob/main/packages/micromark-util-decode-string/dev/index.js>.
    fn decode(caps: &Captures) -> String {
        if let Some(first_cap) = caps.get(1) {
            return String::from(first_cap.as_str());
        }

        if let Some(head) = &caps[2].chars().nth(0) {
            if *head == '#' {
                let radix = match caps[2].chars().nth(1) {
                    Some('x') | Some('X') => 16,
                    _ => 10,
                };
                let capture = &caps[2];
                let numeric_encoded = if radix == 16 {
                    &capture[2..]
                } else {
                    &capture[1..]
                };
                return crate::decode_numeric(numeric_encoded, radix);
            }
        }

        crate::decode_named(&caps[2], true).unwrap_or(caps[0].to_string())
    }

    /// No real JS equivalent, it’s written inline.
    fn encode_char(character: char) -> String {
        let hex_code = u32::from(character);
        format!("&#x{:X};", hex_code)
    }

    pub fn enter(&mut self, name: ConstructName) {
        self.stack.push(name);
    }

    pub fn exit(&mut self) {
        self.stack.pop();
    }

    /// No JS equivalent.
    pub fn handle(
        &mut self,
        node: &Node,
        info: &Info,
        parent: Option<&Node>,
    ) -> Result<String, Message> {
        match node {
            Node::Break(r#break) => r#break.handle(self, info, parent, node),
            Node::Blockquote(block_quote) => block_quote.handle(self, info, parent, node),
            Node::Code(code) => code.handle(self, info, parent, node),
            Node::Delete(_) => Ok(format!("~~{}~~", self.container_phrasing(node, info)?)),
            Node::Definition(definition) => definition.handle(self, info, parent, node),
            Node::Emphasis(emphasis) => emphasis.handle(self, info, parent, node),
            Node::Fragment(fragment) => {
                if fragment.children.iter().any(is_phrasing) {
                    self.container_phrasing(node, info)
                } else {
                    self.container_flow(node)
                }
            }
            Node::FootnoteDefinition(definition) => {
                let label = definition
                    .label
                    .as_deref()
                    .unwrap_or(&definition.identifier);
                let mut value = format!("[^{}]:", label);
                if !definition.children.is_empty() {
                    let content = self.container_flow(node)?;
                    value.push(' ');
                    value.push_str(&self.indent_lines(&content, |line, index, blank| {
                        if index == 0 || blank {
                            line.to_string()
                        } else {
                            format!("    {}", line)
                        }
                    }));
                }
                Ok(value)
            }
            Node::FootnoteReference(reference) => Ok(format!(
                "[^{}]",
                reference.label.as_deref().unwrap_or(&reference.identifier)
            )),
            Node::Heading(heading) => heading.handle(self, info, parent, node),
            Node::Html(html) => html.handle(self, info, parent, node),
            Node::ImageReference(image_reference) => {
                image_reference.handle(self, info, parent, node)
            }
            Node::Image(image) => image.handle(self, info, parent, node),
            Node::InlineCode(inline_code) => inline_code.handle(self, info, parent, node),
            Node::LinkReference(link_reference) => link_reference.handle(self, info, parent, node),
            Node::Link(link) => link.handle(self, info, parent, node),
            Node::ListItem(list_item) => list_item.handle(self, info, parent, node),
            Node::List(list) => list.handle(self, info, parent, node),
            Node::Paragraph(paragraph) => paragraph.handle(self, info, parent, node),
            Node::Root(root) => root.handle(self, info, parent, node),
            Node::Strong(strong) => strong.handle(self, info, parent, node),
            Node::Text(text) => text.handle(self, info, parent, node),
            Node::ThematicBreak(thematic_break) => thematic_break.handle(self, info, parent, node),
            Node::Math(math) => math.handle(self, info, parent, node),
            Node::InlineMath(inline_math) => inline_math.handle(self, info, parent, node),
            Node::MdxFlowExpression(expression) => Ok(format!("{{{}}}", expression.value)),
            Node::MdxTextExpression(expression) => Ok(format!("{{{}}}", expression.value)),
            Node::MdxjsEsm(esm) => Ok(esm.value.clone()),
            Node::MdxJsxFlowElement(element) => {
                self.render_jsx(&element.name, &element.attributes, node, true)
            }
            Node::MdxJsxTextElement(element) => {
                self.render_jsx(&element.name, &element.attributes, node, false)
            }
            Node::Table(table) => self.render_table(table),
            Node::TableRow(row) => self.render_table_row(&row.children),
            Node::TableCell(_) => self.container_phrasing(node, info),
            Node::Toml(toml) => Ok(render_frontmatter("+++", &toml.value)),
            Node::Yaml(yaml) => Ok(render_frontmatter("---", &yaml.value)),
        }
    }

    fn render_table(&mut self, table: &crate::mdast::Table) -> Result<String, Message> {
        let mut output = String::new();
        for (index, row) in table.children.iter().enumerate() {
            if index > 0 {
                output.push('\n');
            }

            let cells = row.children().map(Vec::as_slice).unwrap_or(&[]);
            output.push_str(&self.render_table_row(cells)?);

            if index == 0 {
                output.push('\n');
                let cells = row.children().map(Vec::as_slice).unwrap_or(&[]);
                let mut alignments = Vec::new();
                for (cell_index, _) in cells.iter().enumerate() {
                    let alignment = table.align.get(cell_index);
                    alignments.push(match alignment {
                        Some(crate::mdast::AlignKind::Left) => ":---".to_string(),
                        Some(crate::mdast::AlignKind::Right) => "---:".to_string(),
                        Some(crate::mdast::AlignKind::Center) => ":---:".to_string(),
                        Some(crate::mdast::AlignKind::None) | None => "---".to_string(),
                    });
                }
                output.push_str(&format!("| {} |", alignments.join(" | ")));
            }
        }

        Ok(output)
    }

    fn render_table_row(&mut self, cells: &[Node]) -> Result<String, Message> {
        let mut values = Vec::new();
        for cell in cells {
            let content = self.container_phrasing(cell, &Info::new("", ""))?;
            values.push(escape_table_pipes(&content));
        }
        Ok(format!("| {} |", values.join(" | ")))
    }

    fn render_jsx(
        &mut self,
        name: &Option<String>,
        attributes: &[AttributeContent],
        node: &Node,
        flow: bool,
    ) -> Result<String, Message> {
        let mut opening = String::from("<");
        if let Some(name) = name {
            opening.push_str(name);
        }
        for attribute in attributes {
            opening.push(' ');
            opening.push_str(&render_jsx_attribute(attribute));
        }

        let children = node.children().map(Vec::as_slice).unwrap_or(&[]);
        if children.is_empty() && name.is_some() {
            opening.push_str(" />");
            return Ok(opening);
        }
        opening.push('>');

        if flow {
            let content = self.container_flow(node)?;
            if let Some(name) = name {
                Ok(format!("{}{}\n</{}>", opening, content, name))
            } else {
                Ok(format!("{}{}\n</>", opening, content))
            }
        } else {
            let content = self.container_phrasing(node, &Info::new("", ""))?;
            if let Some(name) = name {
                Ok(format!("{}{}</{}>", opening, content, name))
            } else {
                Ok(format!("{}{}</>", opening, content))
            }
        }
    }

    /// JS equivalent: <https://github.com/syntax-tree/mdast-util-to-markdown/blob/main/lib/util/indent-lines.js>.
    pub fn indent_lines(&self, value: &str, map: impl Fn(&str, usize, bool) -> String) -> String {
        let mut result = String::new();
        let mut start = 0;
        let mut line = 0;
        let eol = Regex::new(r"\r?\n|\r").unwrap();

        for m in eol.captures_iter(value) {
            let full_match = m.get(0).unwrap();
            let value_slice = &value[start..full_match.start()];
            result.push_str(&map(value_slice, line, value_slice.is_empty()));
            result.push_str(full_match.as_str());
            start = full_match.start() + full_match.len();
            line += 1;
        }

        result.push_str(&map(&value[start..], line, value.is_empty()));
        result
    }

    /// No real JS equivalent, but see:
    /// <https://github.com/syntax-tree/mdast-util-to-markdown/blob/main/lib/join.js>.
    fn join_defaults(&self, left: &Node, right: &Node, parent: &Node) -> Join {
        if let Node::Code(code) = right {
            if format_code_as_indented(code, self) && matches!(left, Node::List(_)) {
                return Join::HtmlComment;
            }

            if let Node::Code(code) = left {
                if format_code_as_indented(code, self) {
                    return Join::HtmlComment;
                }
            }
        }

        if matches!(parent, Node::ListItem(_) | Node::List(_)) {
            if matches!(left, Node::Paragraph(_)) {
                if matches!(right, Node::Paragraph(_)) {
                    return Join::Break;
                }

                if matches!(right, Node::Definition(_)) {
                    return Join::Break;
                }

                if let Node::Heading(heading) = right {
                    if format_heading_as_setext(heading, self) {
                        return Join::Break;
                    }
                }
            }

            let spread = if let Node::List(list) = parent {
                list.spread
            } else if let Node::ListItem(list_item) = parent {
                list_item.spread
            } else {
                false
            };

            if spread {
                return Join::Lines(1);
            }

            return Join::Lines(0);
        }

        Join::Break
    }

    /// No JS equivalent.
    pub fn new(options: &'a Options) -> Self {
        State {
            bullet_current: None,
            bullet_last_used: None,
            index_stack: Vec::new(),
            options,
            stack: Vec::new(),
            r#unsafe: Unsafe::get_default_unsafe(options),
        }
    }

    /// No JS equivalent.
    fn peek_node(&self, node: &Node) -> Option<char> {
        match node {
            Node::Emphasis(_) => Some(peek_emphasis(self)),
            Node::Html(_) => Some(peek_html()),
            Node::ImageReference(_) => Some(peek_image_reference()),
            Node::Image(_) => Some(peek_image()),
            Node::InlineCode(_) => Some(peek_inline_code()),
            Node::LinkReference(_) => Some(peek_link_reference()),
            Node::Link(link) => Some(peek_link(link, node, self)),
            Node::Strong(_) => Some(peek_strong(self)),
            Node::InlineMath(_) => Some(peek_inline_math()),
            _ => None,
        }
    }

    /// JS equivalent: <https://github.com/syntax-tree/mdast-util-to-markdown/blob/main/lib/util/safe.js>.
    pub fn safe(&mut self, input: &str, config: &SafeConfig) -> String {
        let value = format!("{}{}{}", config.before, input, config.after);
        let mut positions: Vec<usize> = Vec::new();
        let mut result: String = String::new();
        let mut infos: BTreeMap<usize, EscapeInfos> = BTreeMap::new();

        for pattern in &mut self.r#unsafe {
            if !pattern_in_scope(&self.stack, pattern) {
                continue;
            }

            Self::compile_pattern(pattern);

            if let Some(regex) = &pattern.compiled {
                for m in regex.captures_iter(&value) {
                    let full_match = m.get(0).expect("Guaranteed to have a match");
                    let captured_group_len = m
                        .get(1)
                        .map(|captured_group| captured_group.len())
                        .unwrap_or(0);
                    let before = pattern.before.is_some() || pattern.at_break;
                    let after = pattern.after.is_some();
                    let position = full_match.start() + if before { captured_group_len } else { 0 };

                    if positions.contains(&position) {
                        if let Some(entry) = infos.get_mut(&position) {
                            if entry.before && !before {
                                entry.before = false;
                            }
                            if entry.after && !after {
                                entry.after = false;
                            }
                        }
                    } else {
                        infos.insert(position, EscapeInfos { after, before });
                        positions.push(position);
                    }
                }
            }
        }

        positions.sort_unstable();

        let mut start = config.before.len();
        let end = value.len() - config.after.len();

        for (index, position) in positions.iter().enumerate() {
            if *position < start || *position >= end {
                continue;
            }

            // If this character is supposed to be escaped because it has a condition on
            // the next character, and the next character is definitly being escaped,
            // then skip this escape.
            // This will never panic because the bounds are properly checked, and we
            // guarantee that the positions are already keys in the `infos` map before this
            // point in execution.
            if index + 1 < positions.len()
                && position + 1 < end
                && positions[index + 1] == position + 1
                && infos[position].after
                && !infos[&(position + 1)].before
                && !infos[&(position + 1)].after
                || index > 0
                    && positions[index - 1] == position - 1
                    && infos[position].before
                    && !infos[&(position - 1)].before
                    && !infos[&(position - 1)].after
            {
                continue;
            }

            if start != *position {
                result.push_str(&escape_backslashes(&value[start..*position], r"\"));
            }
            start = *position;

            let char_at_pos = value.chars().nth(*position);
            match char_at_pos {
                Some('!'..='/') | Some(':'..='@') | Some('['..='`') | Some('{'..='~') => {
                    if let Some(encode) = &config.encode {
                        let character = char_at_pos.expect("To be a valid char");
                        if *encode != character {
                            result.push('\\');
                        } else {
                            let encoded_char = Self::encode_char(character);
                            result.push_str(&encoded_char);
                            start += character.len_utf8();
                        }
                    } else {
                        result.push('\\');
                    }
                }
                Some(character) => {
                    let encoded_char = Self::encode_char(character);
                    result.push_str(&encoded_char);
                    start += character.len_utf8();
                }
                _ => (),
            };
        }

        // Some of the operations above seem to end up right in a utf8 boundary
        // (see GH-170 for more info).
        // Move back.
        while !value.is_char_boundary(start) {
            start -= 1;
        }
        result.push_str(&escape_backslashes(&value[start..end], config.after));

        result
    }

    /// No real JS equivalent, but see:
    /// <https://github.com/syntax-tree/mdast-util-to-markdown/blob/fd6a508/lib/util/container-flow.js#L66>.
    fn set_between(join: &Join, results: &mut String) {
        if let Join::Break = join {
            results.push_str("\n\n");
        } else if let Join::Lines(n) = join {
            if *n == 1 {
                results.push_str("\n\n");
                return;
            }
            results.push_str("\n".repeat(1 + n).as_ref());
        } else if let Join::HtmlComment = join {
            results.push_str("\n\n<!---->\n\n");
        }
    }

    /// No real JS equivalent, but see:
    /// <https://github.com/syntax-tree/mdast-util-to-markdown/blob/fd6a508/lib/util/container-flow.js#L66>.
    fn tight_definition(&self, left: &Node, right: &Node) -> Join {
        if matches!(left, Node::Definition(_)) && matches!(right, Node::Definition(_)) {
            return Join::Lines(0);
        }

        Join::Break
    }
}

pub(crate) fn is_phrasing(node: &Node) -> bool {
    matches!(
        node,
        Node::Break(_)
            | Node::Delete(_)
            | Node::Emphasis(_)
            | Node::FootnoteReference(_)
            | Node::Image(_)
            | Node::ImageReference(_)
            | Node::InlineCode(_)
            | Node::InlineMath(_)
            | Node::Link(_)
            | Node::LinkReference(_)
            | Node::MdxJsxTextElement(_)
            | Node::MdxTextExpression(_)
            | Node::Strong(_)
            | Node::Text(_)
    )
}

fn render_frontmatter(marker: &str, value: &str) -> String {
    let mut output = format!("{}\n", marker);
    output.push_str(value);
    if !value.ends_with('\n') && !value.ends_with('\r') {
        output.push('\n');
    }
    output.push_str(marker);
    output
}

fn render_jsx_attribute(attribute: &AttributeContent) -> String {
    match attribute {
        AttributeContent::Expression(expression) => format!("{{{}}}", expression.value),
        AttributeContent::Property(property) => match &property.value {
            None => property.name.clone(),
            Some(AttributeValue::Literal(value)) => format!(
                "{}=\"{}\"",
                property.name,
                value.replace('&', "&amp;").replace('"', "&quot;")
            ),
            Some(AttributeValue::Expression(expression)) => {
                format!("{}={{{}}}", property.name, expression.value)
            }
        },
    }
}

fn escape_table_pipes(value: &str) -> String {
    let bytes = value.as_bytes();
    let mut result = String::new();
    let mut index = 0;
    let mut code_ticks = None;

    while index < bytes.len() {
        if bytes[index] == b'`' {
            let start = index;
            while index < bytes.len() && bytes[index] == b'`' {
                index += 1;
            }
            let ticks = index - start;
            if code_ticks == Some(ticks) {
                code_ticks = None;
            } else if code_ticks.is_none() && has_matching_ticks(bytes, index, ticks) {
                code_ticks = Some(ticks);
            }
            result.push_str(&value[start..index]);
        } else if bytes[index] == b'|' && code_ticks.is_none() {
            let slashes = result
                .chars()
                .rev()
                .take_while(|character| *character == '\\')
                .count();
            if slashes % 2 == 0 {
                result.push('\\');
            }
            result.push('|');
            index += 1;
        } else {
            let character = value[index..].chars().next().expect("valid UTF-8");
            result.push(character);
            index += character.len_utf8();
        }
    }

    result
}

fn has_matching_ticks(bytes: &[u8], mut index: usize, ticks: usize) -> bool {
    while index < bytes.len() {
        if bytes[index] == b'`' {
            let start = index;
            while index < bytes.len() && bytes[index] == b'`' {
                index += 1;
            }
            if index - start == ticks {
                return true;
            }
        } else {
            index += 1;
        }
    }
    false
}