markdown-ast 0.1.1

Markdown AST representation for document construction and transformation, based on pulldown-cmark.
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
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
//! Parse a Markdown input string into a sequence of Markdown abstract syntax
//! tree [`Block`]s.
//!
//! This crate is intentionally designed to interoperate well with the
//! [`pulldown-cmark`](https://crates.io/crate/pulldown-cmark) crate and the
//! ecosystem around it. See [Motivation and relation to pulldown-cmark](#motivation-and-relation-to-pulldown-cmark)
//! for more information.
//!
//! The AST types are designed to align with the structure defined
//! by the [CommonMark Specification](https://spec.commonmark.org/).
//!
//! # Quick Examples
//!
//! Parse simple Markdown into an AST:
//!
//! ```
//! use markdown_ast::{markdown_to_ast, Block, Inline, Inlines};
//! # use pretty_assertions::assert_eq;
//!
//! let ast = markdown_to_ast("
//! Hello! This is a paragraph **with bold text**.
//! ");
//!
//! assert_eq!(ast, vec![
//!     Block::Paragraph(Inlines(vec![
//!         Inline::Text("Hello! This is a paragraph ".to_owned()),
//!         Inline::Strong(Inlines(vec![
//!             Inline::Text("with bold text".to_owned()),
//!         ])),
//!         Inline::Text(".".to_owned())
//!     ]))
//! ]);
//! ```
//!
//!
//!
//! # API Overview
//!
//! | Function                           | Input      | Output       |
//! |------------------------------------|------------|--------------|
//! | [`markdown_to_ast()`]              | `&str`     | `Vec<Block>` |
//! | [`ast_to_markdown()`]              | `&[Block]` | `String`     |
//! | [`ast_to_events()`]                | `&[Block]` | `Vec<Event>` |
//! | [`events_to_ast()`]                | `&[Event]` | `Vec<Block>` |
//! | [`events_to_markdown()`]           | `&[Event]` | `String`     |
//! | [`markdown_to_events()`]           | `&str`     | `Vec<Event>` |
//! | [`canonicalize()`]                 | `&str`     | `String`     |
//!
//! ##### Terminology
//!
//! This crate is able to process and manipulate Markdown in three different
//! representations:
//!
//! | Term     | Type                 | Description                         |
//! |----------|----------------------|-------------------------------------|
//! | Markdown | `String`             | Raw Markdown source / output string |
//! | Events   | `&[Event]`           | Markdown parsed by [`pulldown-cmark`](https://crates.io/crates/pulldown-cmark) into a flat sequence of parser [`Event`]s |
//! | AST      | `Block` / `&[Block]` | Markdown parsed by `markdown-ast` into a hierarchical structure of [`Block`]s |
//!
//! ##### Processing Steps
//!
//! ```text
//!     String => Events => Blocks => Events => String
//!     |_____ A ______|    |______ C _____|
//!               |______ B _____|    |______ D _____|
//!     |__________ E ___________|
//!                         |___________ F __________|
//!     |____________________ G _____________________|
//! ```
//!
//! - **A** — [`markdown_to_events()`]
//! - **B** — [`events_to_ast()`]
//! - **C** — [`ast_to_events()`]
//! - **D** — [`events_to_markdown()`]
//! - **E** — [`markdown_to_ast()`]
//! - **F** — [`ast_to_markdown()`]
//! - **G** — [`canonicalize()`]
//!
//! Note: **A** wraps [`pulldown_cmark::Parser`], and **D** wraps
//! [`pulldown_cmark_to_cmark::cmark()`].
//!
//!
//!
//! # Detailed Examples
//!
//! #### Parse varied Markdown to an AST representation:
//!
//! ```
//! use markdown_ast::{
//!     markdown_to_ast, Block, HeadingLevel, Inline, Inlines, ListItem
//! };
//! # use pretty_assertions::assert_eq;
//!
//! let ast = markdown_to_ast("
//! ## An Example Document
//!
//! This is a paragraph that
//! is split across *multiple* lines.
//!
//! * This is a list item
//! ");
//!
//! assert_eq!(ast, vec![
//!     Block::Heading(
//!         HeadingLevel::H1,
//!         Inlines(vec![
//!              Inline::Text("An Example Document".to_owned())
//!         ])
//!     ),
//!     Block::Paragraph(Inlines(vec![
//!         Inline::Text("This is a paragraph that".to_owned()),
//!         Inline::SoftBreak,
//!         Inline::Text("is split across ".to_owned()),
//!         Inline::Emphasis(Inlines(vec![
//!             Inline::Text("multiple".to_owned()),
//!         ])),
//!         Inline::Text(" lines.".to_owned()),
//!     ])),
//!     Block::List(vec![
//!         ListItem(vec![
//!             Block::Paragraph(Inlines(vec![
//!                 Inline::Text("This is a list item".to_owned())
//!             ]))
//!         ])
//!     ])
//! ]);
//! ```
//!
//! #### Synthesize Markdown using programmatic construction of the document:
//!
//! *Note:* This is a more user friendly alternative to a "string builder"
//! approach where the raw Markdown string is constructed piece by piece,
//! which suffers from extra bookkeeping that must be done to manage things like
//! indent level and soft vs hard breaks.
//!
//! ```
//! use markdown_ast::{
//!     ast_to_markdown, Block, Inline, Inlines, ListItem,
//!     HeadingLevel,
//! };
//! # use pretty_assertions::assert_eq;
//!
//! let tech_companies = vec![
//!     ("Apple", 1976, 164_000),
//!     ("Microsoft", 1975, 221_000),
//!     ("Nvidia", 1993, 29_600),
//! ];
//!
//! let ast = vec![
//!     Block::Heading(HeadingLevel::H1, Inlines::plain_text("Tech Companies")),
//!     Block::plain_text_paragraph("The following are major tech companies:"),
//!     Block::List(Vec::from_iter(
//!         tech_companies
//!             .into_iter()
//!             .map(|(company_name, founded, employee_count)| {
//!                 ListItem(vec![
//!                     Block::paragraph(vec![Inline::plain_text(company_name)]),
//!                     Block::List(vec![
//!                         ListItem::plain_text(format!("Founded: {founded}")),
//!                         ListItem::plain_text(format!("Employee count: {employee_count}"))
//!                     ])
//!                 ])
//!             })
//!     ))
//! ];
//!
//! let markdown: String = ast_to_markdown(&ast);
//!
//! assert_eq!(markdown, "\
//! ## Tech Companies
//!
//! The following are major tech companies:
//!
//! * Apple
//!  
//!   * Founded: 1976
//!  
//!   * Employee count: 164000
//!
//! * Microsoft
//!  
//!   * Founded: 1975
//!  
//!   * Employee count: 221000
//!
//! * Nvidia
//!  
//!   * Founded: 1993
//!  
//!   * Employee count: 29600\
//! ");
//!
//! ```
//!
//! # Known Issues
//!
//! Currently `markdown-ast` does not escape Markdown content appearing in
//! leaf inline text:
//!
//! ```
//! use markdown_ast::{ast_to_markdown, Block};
//!
//! let ast = vec![
//!     Block::plain_text_paragraph("In the equation a*b*c ...")
//! ];
//!
//! let markdown = ast_to_markdown(&ast);
//!
//! assert_eq!(markdown, "In the equation a*b*c ...");
//! ```
//!
//! which will render as:
//!
//! > In the equation a*b*c ...
//!
//! with the asterisks interpreted as emphasis formatting markers, contrary to
//! the intention of the author.
//!
//! Fixing this robustly will require either:
//!
//! * Adding automatic escaping of Markdown characters in [`Inline::Text`]
//!   during rendering (not ideal)
//!
//! * Adding pre-construction validation checks for [`Inline::Text`] that
//!   prevent constructing an `Inline` with Markdown formatting characters that
//!   have not been escaped correctly by the user.
//!
//! In either case, fixing this bug will be considered a **semver exempt**
//! change in behavior to `markdown-ast`.
//!
//! # Motivation and relation to `pulldown-cmark`
//!
//! [`pulldown-cmark`](https://crates.io/crates/pulldown-cmark) is a popular
//! Markdown parser crate. It provides a streaming event (pull parsing) based
//! representation of a Markdown document. That representation is useful for
//! efficient transformation of a Markdown document into another format, often
//! HTML.
//!
//! However, a streaming parser representation is less amenable to programmatic
//! construction or human-understandable transformations of Markdown documents.
//!
//! `markdown-ast` provides a abstract syntax tree (AST) representation of
//! Markdown that is easy to construct and work with.
//!
//! Additionally, `pulldown-cmark` is widely used in the Rust crate ecosystem,
//! for example for [`mdbook`](https://crates.io/crates/mdbook) extensions.
//! Interoperability with `pulldown-cmark` is an intentional design choice for
//! the usability of `markdown-ast`; one could imagine `markdown-ast` instead
//! abstracting over the underlying parser implementation, but my view is that
//! would limit the utility of `markdown-ast`.
//!

mod unflatten;

mod from_events;
mod to_events;

/// Ensure that doc tests in the README.md file get run.
///
/// See: <https://connorgray.com/reference/creating-a-new-rust-crate#test-readmemd-examples>
mod test_readme {
    #![doc = include_str!("../README.md")]
}

use pulldown_cmark::{self as md, CowStr, Event};

pub use pulldown_cmark::HeadingLevel;

//======================================
// AST Representation
//======================================

/// A piece of structural Markdown content.
/// (CommonMark: [blocks](https://spec.commonmark.org/0.30/#blocks),
/// [container blocks](https://spec.commonmark.org/0.30/#container-blocks))
#[derive(Debug, Clone, PartialEq)]
pub enum Block {
    /// CommonMark: [paragraphs](https://spec.commonmark.org/0.30/#paragraphs)
    Paragraph(Inlines),
    /// CommonMark: [lists](https://spec.commonmark.org/0.30/#lists)
    List(Vec<ListItem>),
    /// CommonMark: [ATX heading](https://spec.commonmark.org/0.30/#atx-heading)
    Heading(HeadingLevel, Inlines),
    /// An indented or fenced code block.
    ///
    /// CommonMark: [indented code blocks](https://spec.commonmark.org/0.30/#indented-code-blocks),
    /// [fenced code blocks](https://spec.commonmark.org/0.30/#fenced-code-blocks)
    CodeBlock {
        /// Indicates whether this is a fenced or indented code block.
        ///
        /// If this `CodeBlock` is a fenced code block, this contains its info
        /// string.
        ///
        /// CommonMark: [info string](https://spec.commonmark.org/0.30/#info-string)
        kind: CodeBlockKind,
        code: String,
    },
    /// CommonMark: [block quotes](https://spec.commonmark.org/0.30/#block-quotes)
    BlockQuote {
        // TODO: Document
        kind: Option<md::BlockQuoteKind>,
        blocks: Vec<Block>,
    },
    Table {
        alignments: Vec<md::Alignment>,
        headers: Vec<Inlines>,
        rows: Vec<Vec<Inlines>>,
    },
    /// CommonMark: [thematic breaks](https://spec.commonmark.org/0.30/#thematic-breaks)
    Rule,
}

/// A sequence of [`Inline`]s.
/// (CommonMark: [inlines](https://spec.commonmark.org/0.30/#inlines))
#[derive(Debug, Clone, PartialEq)]
pub struct Inlines(pub Vec<Inline>);

/// An item in a list. (CommonMark: [list items](https://spec.commonmark.org/0.30/#list-items))
#[derive(Debug, Clone, PartialEq)]
pub struct ListItem(pub Vec<Block>);

/// An inline piece of atomic Markdown content.
/// (CommonMark: [inlines](https://spec.commonmark.org/0.30/#inlines))
#[derive(Debug, Clone, PartialEq)]
pub enum Inline {
    Text(String),
    /// CommonMark: [emphasis](https://spec.commonmark.org/0.30/#emphasis-and-strong-emphasis)
    Emphasis(Inlines),
    /// CommonMark: [strong emphasis](https://spec.commonmark.org/0.30/#emphasis-and-strong-emphasis)
    Strong(Inlines),
    /// Strikethrough styled text. (Non-standard.)
    Strikethrough(Inlines),
    /// CommonMark: [code spans](https://spec.commonmark.org/0.30/#code-spans)
    Code(String),
    /// CommonMark: [links](https://spec.commonmark.org/0.30/#links)
    // TODO:
    //  Document every type of Inline::Link value and what its equivalent source
    //  is.
    Link {
        link_type: md::LinkType,
        /// CommonMark: [link destination](https://spec.commonmark.org/0.30/#link-destination)
        dest_url: String,
        /// CommonMark: [link title](https://spec.commonmark.org/0.30/#link-title)
        title: String,
        /// CommonMark: [link label](https://spec.commonmark.org/0.30/#link-label)
        id: String,
        /// CommonMark: [link text](https://spec.commonmark.org/0.30/#link-text)
        content_text: Inlines,
    },
    /// CommonMark: [soft line breaks](https://spec.commonmark.org/0.30/#soft-line-breaks)
    SoftBreak,
    /// CommonMark: [hard line breaks](https://spec.commonmark.org/0.30/#hard-line-breaks)
    HardBreak,
}

#[derive(Debug, Clone, PartialEq)]
pub enum CodeBlockKind {
    Fenced(String),
    Indented,
}

//======================================
// Public API Functions
//======================================

/// Parse Markdown input string into AST [`Block`]s.
pub fn markdown_to_ast(input: &str) -> Vec<Block> {
    /* For Markdown parsing debugging.
    {
        let mut options = md::Options::empty();
        options.insert(md::Options::ENABLE_STRIKETHROUGH);
        let parser = md::Parser::new_ext(input, options);

        let events: Vec<_> = parser.into_iter().collect();

        println!("==== All events =====\n");
        for event in &events {
            println!("{event:?}");
        }
        println!("\n=====================\n");

        println!("==== Unflattened events =====\n");
        for event in unflatten::parse_markdown_to_unflattened_events(input) {
            println!("{event:#?}")
        }
        println!("=============================\n");
    }
    */

    let events = markdown_to_events(input);

    return events_to_ast(events);
}

/// Convert AST [`Block`]s into a Markdown string.
pub fn ast_to_markdown(blocks: &[Block]) -> String {
    let events = ast_to_events(blocks);

    return events_to_markdown(events);
}

/// Convert [`Event`]s into a Markdown string.
///
/// This is a thin wrapper around
/// [`pulldown_cmark_to_cmark::cmark_with_options`], provided in this crate for
/// consistency and ease of use.
pub fn events_to_markdown<'e, I: IntoIterator<Item = Event<'e>>>(
    events: I,
) -> String {
    let mut string = String::new();

    let options = default_to_markdown_options();

    let _: pulldown_cmark_to_cmark::State =
        pulldown_cmark_to_cmark::cmark_with_options(
            events.into_iter(),
            &mut string,
            options,
        )
        .expect("error converting Event sequent to Markdown string");

    string
}

/// Convert AST [`Block`]s into an [`Event`] sequence.
pub fn ast_to_events(blocks: &[Block]) -> Vec<Event> {
    let mut events: Vec<Event> = Vec::new();

    for block in blocks {
        let events = &mut events;

        crate::to_events::block_to_events(&block, events);
    }

    events
}

/// Parse [`Event`]s into AST [`Block`]s.
pub fn events_to_ast<'i, I: IntoIterator<Item = Event<'i>>>(
    events: I,
) -> Vec<Block> {
    let events =
        unflatten::parse_markdown_to_unflattened_events(events.into_iter());

    crate::from_events::ast_events_to_ast(events)
}

/// Parse Markdown input string into [`Event`]s.
///
/// This is a thin wrapper around [`pulldown_cmark::Parser`], provided in this
/// crate for consistency and ease of use.
pub fn markdown_to_events<'i>(
    input: &'i str,
) -> impl Iterator<Item = Event<'i>> {
    // Set up options and parser. Strikethroughs are not part of the CommonMark standard
    // and we therefore must enable it explicitly.
    let mut options = md::Options::empty();
    options.insert(md::Options::ENABLE_STRIKETHROUGH);
    options.insert(md::Options::ENABLE_TABLES);
    md::Parser::new_ext(input, options)
}

/// Canonicalize (or format) a Markdown input by parsing and then converting
/// back to a string.
///
/// **⚠️ Warning ⚠️:** This function is **semver exempt**. The precise
/// canonicalization behavior may change in MINOR or PATCH versions of
/// markdown-ast. (Stabilizing the behavior of this function will require
/// additional options to configure the behavior of
/// [pulldown-cmark-to-cmark](https://crates.io/crates/pulldown-cmark-to-cmark).)
///
/// # Examples
///
/// List items using `-` (minus) are canonicalized to the `*` (asterisk) list
/// marker type:
///
/// ```
/// use markdown_ast::canonicalize;
/// assert_eq!(
/// canonicalize("\
/// - Foo
/// - Bar
/// "),
/// "\
/// * Foo
///
/// * Bar"
/// )
/// ```
///
/// Hard breaks ending in backslash are canonicalized to the "two spaces at the
/// end of the line" form:
///
/// ```
/// use markdown_ast::canonicalize;
/// assert_eq!(
/// canonicalize(r#"
/// This ends in a hard break.\
/// This is a new line."#),
/// // Note: The two spaces at the end of the first line below may not be
/// //       visible, but they're there.
/// "\
/// This ends in a hard break.  
/// This is a new line."
/// )
/// ```
pub fn canonicalize(input: &str) -> String {
    let ast = markdown_to_ast(input);

    return ast_to_markdown(&ast);
}

fn default_to_markdown_options() -> pulldown_cmark_to_cmark::Options<'static> {
    pulldown_cmark_to_cmark::Options {
        // newlines_after_paragraph: 2,
        // newlines_after_headline: 0,
        // newlines_after_codeblock: 0,
        // newlines_after_list: 1,
        // newlines_after_rest: 0,
        code_block_token_count: 3,
        ..pulldown_cmark_to_cmark::Options::default()
    }
}

//======================================
// Impls
//======================================

impl Inline {
    /// Construct a inline containing a piece of plain text.
    pub fn plain_text<S: Into<String>>(s: S) -> Self {
        Inline::Text(s.into())
    }

    pub fn emphasis(inline: Inline) -> Self {
        Inline::Emphasis(Inlines(vec![inline]))
    }

    pub fn strong(inline: Inline) -> Self {
        Inline::Strong(Inlines(vec![inline]))
    }

    pub fn strikethrough(inline: Inline) -> Self {
        Inline::Strikethrough(Inlines(vec![inline]))
    }

    pub fn code<S: Into<String>>(s: S) -> Self {
        Inline::Code(s.into())
    }
}

impl Inlines {
    /// Construct an inlines sequence containing a single inline piece of plain
    /// text.
    pub fn plain_text<S: Into<String>>(inline: S) -> Self {
        return Inlines(vec![Inline::Text(inline.into())]);
    }
}

impl Block {
    /// Construct a paragraph block containing a single inline piece of plain
    /// text.
    pub fn plain_text_paragraph<S: Into<String>>(inline: S) -> Self {
        return Block::Paragraph(Inlines(vec![Inline::Text(inline.into())]));
    }

    pub fn paragraph(text: Vec<Inline>) -> Block {
        Block::Paragraph(Inlines(text))
    }
}

impl ListItem {
    /// Construct a list item containing a single inline piece of plain text.
    pub fn plain_text<S: Into<String>>(inline: S) -> Self {
        return ListItem(vec![Block::Paragraph(Inlines(vec![Inline::Text(
            inline.into(),
        )]))]);
    }
}

impl CodeBlockKind {
    pub fn info_string(&self) -> Option<&str> {
        match self {
            CodeBlockKind::Fenced(info_string) => Some(info_string.as_str()),
            CodeBlockKind::Indented => None,
        }
    }

    pub(crate) fn from_pulldown_cmark(kind: md::CodeBlockKind) -> Self {
        match kind {
            md::CodeBlockKind::Indented => CodeBlockKind::Indented,
            md::CodeBlockKind::Fenced(info_string) => {
                CodeBlockKind::Fenced(info_string.to_string())
            },
        }
    }

    pub(crate) fn to_pulldown_cmark<'s>(&'s self) -> md::CodeBlockKind<'s> {
        match self {
            CodeBlockKind::Fenced(info) => {
                md::CodeBlockKind::Fenced(CowStr::from(info.as_str()))
            },
            CodeBlockKind::Indented => md::CodeBlockKind::Indented,
        }
    }
}

impl IntoIterator for Inlines {
    type Item = Inline;
    type IntoIter = std::vec::IntoIter<Inline>;

    fn into_iter(self) -> Self::IntoIter {
        let Inlines(vec) = self;
        vec.into_iter()
    }
}

//======================================
// Tests: Markdown to AST parsing
//======================================

#[test]
fn test_markdown_to_ast() {
    use indoc::indoc;
    use pretty_assertions::assert_eq;

    assert_eq!(
        markdown_to_ast("hello"),
        vec![Block::paragraph(vec![Inline::Text("hello".into())])]
    );

    //--------------
    // Styled text
    //--------------

    assert_eq!(
        markdown_to_ast("*hello*"),
        vec![Block::paragraph(vec![Inline::emphasis(Inline::Text(
            "hello".into()
        ))])]
    );

    assert_eq!(
        markdown_to_ast("**hello**"),
        vec![Block::paragraph(vec![Inline::strong(Inline::Text(
            "hello".into()
        ))])]
    );

    assert_eq!(
        markdown_to_ast("~~hello~~"),
        vec![Block::paragraph(vec![Inline::strikethrough(Inline::Text(
            "hello".into()
        ))])]
    );

    assert_eq!(
        markdown_to_ast("**`strong code`**"),
        vec![Block::paragraph(vec![Inline::strong(Inline::Code(
            "strong code".into()
        ))])]
    );

    assert_eq!(
        markdown_to_ast("~~`foo`~~"),
        vec![Block::paragraph(vec![Inline::strikethrough(Inline::Code(
            "foo".into()
        ))])]
    );

    assert_eq!(
        markdown_to_ast("**[example](example.com)**"),
        vec![Block::paragraph(vec![Inline::strong(Inline::Link {
            link_type: md::LinkType::Inline,
            dest_url: "example.com".into(),
            title: String::new(),
            id: String::new(),
            content_text: Inlines(vec![Inline::Text("example".into())]),
        })])]
    );

    // Test composition of emphasis, strong, strikethrough and code
    assert_eq!(
        markdown_to_ast("_~~**`foo`**~~_"),
        vec![Block::paragraph(vec![Inline::emphasis(
            Inline::strikethrough(Inline::strong(Inline::Code("foo".into())))
        )])]
    );

    //--------------
    // Lists
    //--------------

    assert_eq!(
        markdown_to_ast("* hello"),
        vec![Block::List(vec![ListItem(vec![Block::paragraph(vec![
            Inline::Text("hello".into())
        ])])])]
    );

    // List items with styled text

    assert_eq!(
        markdown_to_ast("* *hello*"),
        vec![Block::List(vec![ListItem(vec![Block::paragraph(vec![
            Inline::emphasis(Inline::Text("hello".into()))
        ])])])]
    );

    assert_eq!(
        markdown_to_ast("* **hello**"),
        vec![Block::List(vec![ListItem(vec![Block::paragraph(vec![
            Inline::strong(Inline::Text("hello".into()))
        ])])])]
    );

    assert_eq!(
        markdown_to_ast("* ~~hello~~"),
        vec![Block::List(vec![ListItem(vec![Block::paragraph(vec![
            Inline::strikethrough(Inline::Text("hello".into()),)
        ])])])]
    );

    //----------------------------------

    let input = "\
* And **bold** text.
  
  * With nested list items.
    
    * `md2nb` supports nested lists up to three levels deep.
";

    let ast = vec![Block::List(vec![ListItem(vec![
        Block::paragraph(vec![
            Inline::plain_text("And "),
            Inline::strong(Inline::plain_text("bold")),
            Inline::plain_text(" text."),
        ]),
        Block::List(vec![ListItem(vec![
            Block::paragraph(vec![Inline::plain_text(
                "With nested list items.",
            )]),
            Block::List(vec![ListItem(vec![Block::paragraph(vec![
                Inline::code("md2nb"),
                Inline::plain_text(
                    " supports nested lists up to three levels deep.",
                ),
            ])])]),
        ])]),
    ])])];

    assert_eq!(markdown_to_ast(input), ast);

    // Sanity check conversion to event stream.
    assert_eq!(
        markdown_to_events(input).collect::<Vec<_>>(),
        ast_to_events(&ast)
    );

    //----------------------------------
    // Test structures
    //----------------------------------

    assert_eq!(
        markdown_to_ast(indoc!(
            "
            * hello

              world
            "
        )),
        vec![Block::List(vec![ListItem(vec![
            Block::paragraph(vec![Inline::Text("hello".into())]),
            Block::paragraph(vec![Inline::Text("world".into())])
        ])])]
    );

    #[rustfmt::skip]
    assert_eq!(
        markdown_to_ast(indoc!(
            "
            # Example

            * A
              - A.A

                hello world

                * *A.A.A*
            "
        )),
        vec![
            Block::Heading(
                HeadingLevel::H1,
                Inlines(vec![Inline::Text("Example".into())])
            ),
            Block::List(vec![
                ListItem(vec![
                    Block::paragraph(vec![Inline::Text("A".into())]),
                    Block::List(vec![
                        ListItem(vec![
                            Block::paragraph(vec![Inline::Text("A.A".into())]),
                            Block::paragraph(vec![Inline::Text("hello world".into())]),
                            Block::List(vec![
                                ListItem(vec![
                                    Block::paragraph(vec![
                                        Inline::emphasis(
                                            Inline::Text(
                                            "A.A.A".into()),
                                        )
                                    ])
                                ])
                            ])
                        ])
                    ])
                ])
            ])
        ]
    );

    #[rustfmt::skip]
    assert_eq!(
        markdown_to_ast(indoc!(
            "
            * A
              - A.A
                * A.A.A
              - A.B
              - A.C
            "
        )),
        vec![
            Block::List(vec![
                ListItem(vec![
                    Block::paragraph(vec![Inline::Text("A".into())]),
                    Block::List(vec![
                        ListItem(vec![
                            Block::paragraph(vec![Inline::Text("A.A".into())]),
                            Block::List(vec![ListItem(vec![
                                Block::paragraph(vec![Inline::Text("A.A.A".into())]),
                            ])])
                        ]),
                        ListItem(vec![
                            Block::paragraph(vec![Inline::Text("A.B".into())]),
                        ]),
                        ListItem(vec![
                            Block::paragraph(vec![Inline::Text("A.C".into())]),
                        ])
                    ])
                ])
            ])
        ]
    );

    #[rustfmt::skip]
    assert_eq!(
        markdown_to_ast(indoc!(
            "
            # Example

            * A
              - A.A
              - A.B
              * A.C
            "
        )),
        vec![
            Block::Heading(
                HeadingLevel::H1,
                Inlines(vec![Inline::Text("Example".into())])
            ),
            Block::List(vec![
                ListItem(vec![
                    Block::paragraph(vec![Inline::Text("A".into())]),
                    Block::List(vec![
                        ListItem(vec![
                            Block::paragraph(vec![Inline::Text("A.A".into())]),
                        ]),
                        ListItem(vec![
                            Block::paragraph(vec![Inline::Text("A.B".into())]),
                        ]),
                    ]),
                    Block::List(vec![
                        ListItem(vec![
                            Block::paragraph(vec![Inline::Text("A.C".into())])
                        ])
                    ]),
                ]),
            ])
        ]
    );

    #[rustfmt::skip]
    assert_eq!(
        markdown_to_ast(indoc!(
            "
            * A
              - A.A
              - A.B

                separate paragraph

              - A.C
            "
        )),
        vec![
            Block::List(vec![
                ListItem(vec![
                    Block::paragraph(vec![Inline::Text("A".into())]),
                    Block::List(vec![
                        ListItem(vec![
                            Block::paragraph(vec![Inline::Text("A.A".into())]),
                        ]),
                        ListItem(vec![
                            Block::paragraph(vec![Inline::Text("A.B".into())]),
                            Block::paragraph(vec![Inline::Text("separate paragraph".into())]),
                        ]),
                        ListItem(vec![
                            Block::paragraph(vec![Inline::Text("A.C".into())]),
                        ])
                    ])
                ])
            ])
        ]
    );

    #[rustfmt::skip]
    assert_eq!(
        markdown_to_ast(indoc!(
            "
            # Example

            * A
              - A.A
                * A.A.A
                  **soft break**

              - A.B

                separate paragraph

              - A.C
            "
        )),
        vec![
            Block::Heading(
                HeadingLevel::H1,
                Inlines(vec![Inline::Text("Example".into())])
            ),
            Block::List(vec![
                ListItem(vec![
                    Block::paragraph(vec![Inline::Text("A".into())]),
                    Block::List(vec![
                        ListItem(vec![
                            Block::paragraph(vec![Inline::Text("A.A".into())]),
                            Block::List(vec![
                                ListItem(vec![
                                    Block::paragraph(vec![
                                        Inline::Text("A.A.A".into()),
                                        Inline::SoftBreak,
                                        Inline::strong(
                                            Inline::Text("soft break".into()),
                                        )
                                    ]),
                                ])
                            ]),
                        ]),
                        ListItem(vec![
                            Block::paragraph(vec![Inline::Text("A.B".into())]),
                            Block::paragraph(vec![Inline::Text("separate paragraph".into())]),
                        ]),
                        ListItem(vec![
                            Block::paragraph(vec![Inline::Text("A.C".into())]),
                        ]),
                    ])
                ])
            ])
        ]
    );
}

//======================================
// Tests: AST to Markdown string
//======================================

#[test]
fn test_ast_to_markdown() {
    use indoc::indoc;
    // use pretty_assertions::assert_eq;

    assert_eq!(
        ast_to_markdown(&[Block::paragraph(vec![Inline::Text(
            "hello".into()
        )])]),
        "hello"
    );

    assert_eq!(
        ast_to_markdown(&[Block::List(vec![ListItem(vec![
            Block::paragraph(vec![Inline::Text("hello".into())]),
            Block::paragraph(vec![Inline::Text("world".into())])
        ])])]),
        indoc!(
            "
            * hello
              
              world"
        ),
    )
}

/// Tests that some of the larger Markdown documents in this repository
/// all round-trip when processed:
#[test]
fn test_md_documents_roundtrip() {
    let kitchen_sink_md =
        include_str!("../../md2nb/docs/examples/kitchen-sink.md");

    // FIXME:
    //  Fix the bugs requiring these hacky removals from kitchen-sink.md
    //  that are needed to make the tests below pass.
    let kitchen_sink_md = kitchen_sink_md
        .replace("\n    \"This is an indented code block.\"\n", "")
        .replace("\nThis is a [shortcut] reference link.\n", "")
        .replace("\nThis is a [full reference][full reference] link.\n", "")
        .replace("\n[full reference]: https://example.org\n", "")
        .replace("[shortcut]: https://example.org\n", "");

    assert_roundtrip(&kitchen_sink_md);

    //==================================
    // README.md
    //==================================

    let readme = include_str!("../../../README.md");

    assert_roundtrip(readme);
}

#[cfg(test)]
fn assert_roundtrip(markdown: &str) {
    use pretty_assertions::assert_eq;

    // Recall:
    //
    //     String => Events => Blocks => Events => String
    //     |_____ A ______|    |______ C _____|
    //               |______ B _____|    |______ D _____|
    //     |__________ E ___________|
    //                         |___________ F __________|

    // Do A to get Events
    let original_events: Vec<Event> = markdown_to_events(markdown).collect();

    // Do B to get AST Blocks
    let ast: Vec<Block> = events_to_ast(original_events.clone());

    // println!("ast = {ast:#?}");

    // Do C to get Events again
    let processed_events: Vec<Event> = ast_to_events(&ast);

    // println!("original_events = {original_events:#?}");

    // Test that A => B => C is equivalent to just A.
    // I.e. that converting an Event stream to and from an AST is lossless.
    assert_eq!(processed_events, original_events);

    // Test that A => B => C => D produces Markdown equivalent to the original
    // Markdown string.
    assert_eq!(ast_to_markdown(&ast), markdown);
}