r3bl_tui 0.5.3

TUI library to build modern apps inspired by Elm, with Flexbox, CSS, editor component, emoji support, and more
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
/*
 *   Copyright (c) 2023 R3BL LLC
 *   All rights reserved.
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 */

//! An element is a single markdown element. Eg: `**bold**`, `*italic*`, `[link](http://r3bl.com)`,
//! etc. These can be found in every single line of text. These parsers extract each element into
//! either a string slice or some other intermediate representation.

use constants::*;
use crossterm::style::Stylize;
use nom::{branch::*,
          bytes::complete::*,
          character::complete::*,
          combinator::*,
          error::ErrorKind,
          multi::*,
          sequence::*,
          IResult};
use r3bl_rs_utils_core::{call_if_true, log_debug};

use crate::*;

pub fn parse_element_starts_with_star(input: &str) -> IResult<&str, &str> {
    let it = parse_fenced_no_newline(input, BOLD);
    it
}

pub fn parse_element_starts_with_underscore(input: &str) -> IResult<&str, &str> {
    let it = parse_fenced_no_newline(input, ITALIC);
    it
}

#[rustfmt::skip]
pub fn parse_element_starts_with_backtick(input: &str) -> IResult<&str, &str> {
    

    delimited(
        /* start */ tag(BACK_TICK),
        /* output */ is_not(BACK_TICK),
        /* end */ tag(BACK_TICK)
    )(input)
}

#[rustfmt::skip]
pub fn parse_element_starts_with_left_bracket(input: &str) -> IResult<&str, HyperlinkData<'_>> {
    let (input, output) = pair(
        delimited(
            /* start */ tag(LEFT_BRACKET),
            /* output */ is_not(RIGHT_BRACKET),
            /* end */ tag(RIGHT_BRACKET)
        ),
        delimited(
            /* start */ tag(LEFT_PARENTHESIS),
            /* output */ is_not(RIGHT_PARENTHESIS),
            /* end */ tag(RIGHT_PARENTHESIS)
        ),
    )(input)?;
    Ok((input, HyperlinkData::from(output)))
}

/// Checkboxes are tricky since they begin with "[" which is also used for hyperlinks and images.
/// So some extra hint is need from the code calling this parser to let it know whether to parse
/// a checkbox into plain text, or into a boolean.
#[rustfmt::skip]
pub fn parse_element_checkbox_into_str(input: &str) -> IResult<&str, &str> {
    
    alt((
        recognize(tag(CHECKED)),
        recognize(tag(UNCHECKED))
    ))(input)
}

#[rustfmt::skip]
/// Checkboxes are tricky since they begin with "[" which is also used for hyperlinks and images.
/// So some extra hint is need from the code calling this parser to let it know whether to parse
/// a checkbox into plain text, or into a boolean.
pub fn parse_element_checkbox_into_bool(input: &str) -> IResult<&str, bool> {
    
    alt((
        map(tag(CHECKED), |_| true),
        map(tag(UNCHECKED), |_| false),
    ))(input)
}

#[rustfmt::skip]
pub fn parse_element_starts_with_left_image(input: &str) -> IResult<&str, HyperlinkData<'_>> {
    let (input, output) =pair(
        delimited(/* start */ tag(LEFT_IMAGE), /* output */ is_not(RIGHT_IMAGE), /* end */ tag(RIGHT_IMAGE)),
        delimited(/* start */ tag(LEFT_PARENTHESIS), /* output */ is_not(RIGHT_PARENTHESIS), /* end */ tag(RIGHT_PARENTHESIS)),
    )(input)?;
    Ok((input, HyperlinkData::from(output)))
}

/// This is the lowest priority parser called by [parse_element_markdown_inline], which
/// itself is called repeatedly in a loop in [parse_block_markdown_text_opt_eol()].
///
/// It will match anything that is not a special character. This is used to parse plain
/// text. However, when it encounters a special character, it will break the input at that
/// character and split the input into two parts: the plain text, and the remainder (after
/// the special character).
///
/// This allows other more specialized parsers to then address these special characters
/// (like italic, bold, links, etc). If these more specialized parsers error out, then
/// this parser will be called again to parse the remainder; this time the input will
/// start with the special character; and in this edge case it will take the input until
/// the first new line; or until the end of the input.
///
/// More info: <https://github.com/dimfeld/export-logseq-notes/blob/40f4d78546bec269ad25d99e779f58de64f4a505/src/parse_string.rs#L132>
#[rustfmt::skip]
pub fn parse_plain_text_no_new_line1(input: &str) -> IResult<&str, &str> {
    // Edge case: If the input starts with [ITALIC, UNDERSCORE, BACKTICK, LEFT_BRACKET,
    // NEW_LINE], then take till the first newline.
    if input.starts_with(ITALIC)
        || input.starts_with(BOLD)
        || input.starts_with(BACK_TICK)
        || input.starts_with(LEFT_BRACKET)
        || input.starts_with(LEFT_IMAGE)
    {
        return take_till1(|c: char|
             c == '\n'
        )(input);
    }

    // Otherwise take till the first special character. And split the input there.
    
    recognize(
        many1(
            preceded(
                /* prefix - discarded */
                not(
                    /* starts with special characters */
                    alt((
                        tag(BOLD),
                        tag(ITALIC),
                        tag(BACK_TICK),
                        tag(LEFT_BRACKET),
                        tag(LEFT_IMAGE),
                        tag(NEW_LINE),
                    ))
                ),
                /* output - keep char */
                anychar,
            )
        )
    )(input)
}

/// More info: <https://github.com/dimfeld/export-logseq-notes/blob/40f4d78546bec269ad25d99e779f58de64f4a505/src/parse_string.rs#L132>
#[rustfmt::skip]
pub fn parse_anychar_in_heading_no_new_line1(input: &str) -> IResult<&str, &str> {
    

    recognize(
        many1(
            preceded(
                /* prefix - discarded */
                not(
                    /* starts with special characters */
                    alt((
                        tag(NEW_LINE),
                    ))
                ),
                /* output - keep char */
                anychar,
            )
        )
    )(input)
}

// BOOKM: parser for a single line of markdown
/// Parse a single chunk of markdown text (found in a single line of text) into a [MdLineFragment].
#[rustfmt::skip]
pub fn parse_element_markdown_inline(
    input: &str,
    checkbox_policy: CheckboxParsePolicy,
) -> IResult<&str, MdLineFragment<'_>> {
    let it = match checkbox_policy {
        CheckboxParsePolicy::IgnoreCheckbox => alt((
            map(parse_element_starts_with_underscore, MdLineFragment::Italic),
            map(parse_element_starts_with_star, MdLineFragment::Bold),
            map(parse_element_starts_with_backtick, MdLineFragment::InlineCode),
            map(parse_element_starts_with_left_image, MdLineFragment::Image),
            map(parse_element_starts_with_left_bracket, MdLineFragment::Link),
            map(parse_element_checkbox_into_str, MdLineFragment::Plain),
            map(parse_plain_text_no_new_line1, MdLineFragment::Plain),
        ))(input),
        CheckboxParsePolicy::ParseCheckbox => alt((
            map(parse_element_starts_with_underscore, MdLineFragment::Italic),
            map(parse_element_starts_with_star, MdLineFragment::Bold),
            map(parse_element_starts_with_backtick, MdLineFragment::InlineCode),
            map(parse_element_starts_with_left_image, MdLineFragment::Image),
            map(parse_element_starts_with_left_bracket, MdLineFragment::Link),
            map(parse_element_checkbox_into_bool, MdLineFragment::Checkbox),
            map(parse_plain_text_no_new_line1, MdLineFragment::Plain),
        ))(input)

    };

    call_if_true!(DEBUG_MD_PARSER, {
        log_debug(format!("\n📣📣📣\n input: {:?}", input).green().to_string());
        match it {
            Ok(ref element) => log_debug(format!("✅✅✅ OK {:#?}", element).magenta().to_string()),
            Result::Err(ref error) => log_debug(format!("🟥🟥🟥 NO {:#?}", error)),
        }
    });

    it
}

/// More info: <https://github.com/dimfeld/export-logseq-notes/blob/40f4d78546bec269ad25d99e779f58de64f4a505/src/parse_string.rs#L132>
fn fenced<'a>(
    start: &'a str,
    end: &'a str,
) -> impl FnMut(&'a str) -> IResult<&'a str, &'a str> {
    map(tuple((tag(start), take_until(end), tag(end))), |x| x.1)
}

pub fn parse_fenced_no_newline<'a>(
    input: &'a str,
    delim: &'a str,
) -> IResult<&'a str, &'a str> {
    let it = fenced(delim, delim)(input);
    if let Ok((_, output)) = &it {
        if output.contains(NEW_LINE) {
            return Err(nom::Err::Error(nom::error::Error {
                input: "",
                code: ErrorKind::TakeUntil,
            }));
        };
    }
    it
}

#[cfg(test)]
mod tests_parse_element {
    use nom::{error::Error, Err as NomErr};
    use r3bl_rs_utils_core::assert_eq2;

    use super::*;

    #[test]
    fn test_parse_plain_text_no_new_line1() {
        assert_eq2!(
            parse_plain_text_no_new_line1("this _bar"),
            Ok(("_bar", "this "))
        );

        assert_eq2!(parse_plain_text_no_new_line1("_bar"), Ok(("", "_bar")));

        assert_eq2!(parse_plain_text_no_new_line1("bar_"), Ok(("_", "bar")));
    }

    #[test]
    fn test_parse_fenced_no_newline() {
        let input = "_foo\nbar_";
        let it = parse_fenced_no_newline(input, "_");
        println!("it: {:?}", it);
        assert_eq2!(
            it,
            Err(NomErr::Error(Error {
                input: "",
                code: ErrorKind::TakeUntil
            }))
        );

        let input = "_foo bar_";
        let it = parse_fenced_no_newline(input, "_");
        println!("it: {:?}", it);
        assert_eq2!(it, Ok(("", "foo bar")));
    }

    #[test]
    fn test_fenced() {
        let input = "_foo bar baz_";
        let it = fenced("_", "_")(input);
        println!("it: {:?}", it);
        assert_eq2!(it, Ok(("", "foo bar baz")));

        let input = "_foo bar baz";
        let it = fenced("_", "_")(input);
        println!("it: {:?}", it);
        assert_eq2!(
            it,
            Err(NomErr::Error(Error {
                input: "foo bar baz",
                code: ErrorKind::TakeUntil
            }))
        );

        let input = "foo _bar_ baz";
        let it = fenced("_", "_")(input);
        println!("it: {:?}", it);
        assert_eq2!(
            it,
            Err(NomErr::Error(Error {
                input: "foo _bar_ baz",
                code: ErrorKind::Tag
            }))
        );
    }

    #[test]
    fn test_parse_element_checkbox_into_str() {
        assert_eq2!(
            parse_element_checkbox_into_str("[x] here is a checkbox"),
            Ok((" here is a checkbox", "[x]"))
        );

        assert_eq2!(
            parse_element_checkbox_into_str("[ ] here is a checkbox"),
            Ok((" here is a checkbox", "[ ]"))
        );
    }

    #[test]
    fn test_parse_element_checkbox_into_bool() {
        assert_eq2!(
            parse_element_checkbox_into_bool("[x] here is a checkbox"),
            Ok((" here is a checkbox", true))
        );

        assert_eq2!(
            parse_element_checkbox_into_bool("[ ] here is a checkbox"),
            Ok((" here is a checkbox", false))
        );
    }

    #[test]
    fn test_parse_element_italic() {
        assert_eq2!(
            parse_element_starts_with_underscore("_here is italic_"),
            Ok(("", "here is italic"))
        );

        assert_eq2!(
            parse_element_starts_with_underscore("_here is italic_"),
            Ok(("", "here is italic"))
        );

        assert_eq2!(
            parse_element_starts_with_underscore("*here is italic"),
            Err(NomErr::Error(Error {
                input: "*here is italic",
                code: ErrorKind::Tag
            }))
        );

        assert_eq2!(
            parse_element_starts_with_underscore("here is italic*"),
            Err(NomErr::Error(Error {
                input: "here is italic*",
                code: ErrorKind::Tag,
            }))
        );

        assert_eq2!(
            parse_element_starts_with_underscore("here is italic"),
            Err(NomErr::Error(Error {
                input: "here is italic",
                code: ErrorKind::Tag
            }))
        );

        assert_eq2!(
            parse_element_starts_with_underscore("*"),
            Err(NomErr::Error(Error {
                input: "*",
                code: ErrorKind::Tag
            }))
        );

        assert_eq2!(
            parse_element_starts_with_underscore("**"),
            Err(NomErr::Error(Error {
                input: "**",
                code: ErrorKind::Tag
            }))
        );

        assert_eq2!(
            parse_element_starts_with_underscore(""),
            Err(NomErr::Error(Error {
                input: "",
                code: ErrorKind::Tag
            }))
        );

        assert_eq2!(
            parse_element_starts_with_underscore("**we are doing bold**"),
            Err(NomErr::Error(Error {
                input: "**we are doing bold**",
                code: ErrorKind::Tag
            }))
        );
    }

    #[test]
    fn test_parse_element_bold() {
        assert_eq2!(
            parse_element_starts_with_star("*here is bold*"),
            Ok(("", "here is bold"))
        );

        assert_eq2!(
            parse_element_starts_with_star("*here is bold"),
            Err(NomErr::Error(Error {
                input: "here is bold",
                code: ErrorKind::TakeUntil
            }))
        );

        assert_eq2!(
            parse_element_starts_with_star("here is bold*"),
            Err(NomErr::Error(Error {
                input: "here is bold*",
                code: ErrorKind::Tag
            }))
        );

        assert_eq2!(
            parse_element_starts_with_star("here is bold"),
            Err(NomErr::Error(Error {
                input: "here is bold",
                code: ErrorKind::Tag
            }))
        );

        assert_eq2!(
            parse_element_starts_with_star("*"),
            Err(NomErr::Error(Error {
                input: "",
                code: ErrorKind::TakeUntil
            }))
        );

        assert_eq2!(
            parse_element_starts_with_star(""),
            Err(NomErr::Error(Error {
                input: "",
                code: ErrorKind::Tag
            }))
        );
    }

    #[test]
    fn test_parse_element_code() {
        assert_eq2!(
            parse_element_starts_with_backtick("`here is code"),
            Err(NomErr::Error(Error {
                input: "",
                code: ErrorKind::Tag
            }))
        );
        assert_eq2!(
            parse_element_starts_with_backtick("here is code`"),
            Err(NomErr::Error(Error {
                input: "here is code`",
                code: ErrorKind::Tag
            }))
        );
        assert_eq2!(
            parse_element_starts_with_backtick("``"),
            Err(NomErr::Error(Error {
                input: "`",
                code: ErrorKind::IsNot
            }))
        );
        assert_eq2!(
            parse_element_starts_with_backtick("`"),
            Err(NomErr::Error(Error {
                input: "",
                code: ErrorKind::IsNot
            }))
        );
        assert_eq2!(
            parse_element_starts_with_backtick(""),
            Err(NomErr::Error(Error {
                input: "",
                code: ErrorKind::Tag
            }))
        );
    }

    #[test]
    fn test_parse_element_link() {
        assert_eq2!(
            parse_element_starts_with_left_bracket("[title](https://www.example.com)"),
            Ok(("", HyperlinkData::new("title", "https://www.example.com")))
        );
        assert_eq2!(
            parse_element_starts_with_backtick(""),
            Err(NomErr::Error(Error {
                input: "",
                code: ErrorKind::Tag
            }))
        );
    }

    #[test]
    fn test_parse_element_image() {
        assert_eq2!(
            parse_element_starts_with_left_image("![alt text](image.jpg)"),
            Ok(("", HyperlinkData::new("alt text", "image.jpg")))
        );
        assert_eq2!(
            parse_element_starts_with_backtick(""),
            Err(NomErr::Error(Error {
                input: "",
                code: ErrorKind::Tag
            }))
        );
    }

    #[test]
    fn test_parse_element_plaintext_unicode() {
        let result = parse_plain_text_no_new_line1("- straight😃\n");
        let remainder = result.as_ref().unwrap().0;
        let output = result.as_ref().unwrap().1;
        assert_eq2!(remainder, "\n");
        assert_eq2!(output, "- straight😃");
    }

    #[test]
    fn test_parse_element_plaintext() {
        assert_eq2!(
            parse_plain_text_no_new_line1("1234567890"),
            Ok(("", "1234567890"))
        );
        assert_eq2!(
            parse_plain_text_no_new_line1("oh my gosh!"),
            Ok(("", "oh my gosh!"))
        );
        assert_eq2!(
            parse_plain_text_no_new_line1("oh my gosh!["),
            Ok(("![", "oh my gosh"))
        );
        assert_eq2!(
            parse_plain_text_no_new_line1("oh my gosh!*"),
            Ok(("*", "oh my gosh!"))
        );
        assert_eq2!(
            parse_plain_text_no_new_line1("*bold baby bold*"),
            Ok(("", "*bold baby bold*"))
        );
        assert_eq2!(
            parse_plain_text_no_new_line1("[link baby](and then somewhat)"),
            Ok(("", "[link baby](and then somewhat)"))
        );
        assert_eq2!(
            parse_plain_text_no_new_line1("`codeblock for bums`"),
            Ok(("", "`codeblock for bums`"))
        );
        assert_eq2!(
            parse_plain_text_no_new_line1("![ but wait theres more](jk)"),
            Ok(("", "![ but wait theres more](jk)"))
        );
        assert_eq2!(
            parse_plain_text_no_new_line1("here is plaintext"),
            Ok(("", "here is plaintext"))
        );
        assert_eq2!(
            parse_plain_text_no_new_line1("here is plaintext!"),
            Ok(("", "here is plaintext!"))
        );
        assert_eq2!(
            parse_plain_text_no_new_line1("here is plaintext![image starting"),
            Ok(("![image starting", "here is plaintext"))
        );
        assert_eq2!(
            parse_plain_text_no_new_line1("here is plaintext\n"),
            Ok(("\n", "here is plaintext"))
        );
        assert_eq2!(
            parse_plain_text_no_new_line1("*here is italic*"),
            Ok(("", "*here is italic*"))
        );
        assert_eq2!(
            parse_plain_text_no_new_line1("**here is bold**"),
            Ok(("", "**here is bold**"))
        );
        assert_eq2!(
            parse_plain_text_no_new_line1("`here is code`"),
            Ok(("", "`here is code`"))
        );
        assert_eq2!(
            parse_plain_text_no_new_line1("[title](https://www.example.com)"),
            Ok(("", "[title](https://www.example.com)"))
        );
        assert_eq2!(
            parse_plain_text_no_new_line1("![alt text](image.jpg)"),
            Ok(("", "![alt text](image.jpg)"))
        );
        assert_eq2!(
            parse_plain_text_no_new_line1(""),
            Err(NomErr::Error(Error {
                input: "",
                code: ErrorKind::Eof
            }))
        );
    }

    #[test]
    fn test_parse_element_markdown_inline() {
        assert_eq2!(
            parse_element_markdown_inline(
                "*here is bold*",
                CheckboxParsePolicy::IgnoreCheckbox
            ),
            Ok(("", MdLineFragment::Bold("here is bold")))
        );
        assert_eq2!(
            parse_element_markdown_inline(
                "_here is italic_",
                CheckboxParsePolicy::IgnoreCheckbox
            ),
            Ok(("", MdLineFragment::Italic("here is italic")))
        );
        assert_eq2!(
            parse_element_markdown_inline(
                "`here is code`",
                CheckboxParsePolicy::IgnoreCheckbox
            ),
            Ok(("", MdLineFragment::InlineCode("here is code")))
        );
        assert_eq2!(
            parse_element_markdown_inline(
                "[title](https://www.example.com)",
                CheckboxParsePolicy::IgnoreCheckbox
            ),
            Ok((
                "",
                (MdLineFragment::Link(HyperlinkData::new(
                    "title",
                    "https://www.example.com"
                )))
            ))
        );
        assert_eq2!(
            parse_element_markdown_inline(
                "![alt text](image.jpg)",
                CheckboxParsePolicy::IgnoreCheckbox
            ),
            Ok((
                "",
                (MdLineFragment::Image(HyperlinkData::new("alt text", "image.jpg")))
            ))
        );
        assert_eq2!(
            parse_element_markdown_inline(
                "here is plaintext!",
                CheckboxParsePolicy::IgnoreCheckbox
            ),
            Ok(("", MdLineFragment::Plain("here is plaintext!")))
        );
        assert_eq2!(
            parse_element_markdown_inline(
                "here is some plaintext *but what if we italicize?",
                CheckboxParsePolicy::IgnoreCheckbox
            ),
            Ok((
                "*but what if we italicize?",
                MdLineFragment::Plain("here is some plaintext ")
            ))
        );
        assert_eq2!(
            parse_element_markdown_inline(
                "here is some plaintext \n*but what if we italicize?",
                CheckboxParsePolicy::IgnoreCheckbox
            ),
            Ok((
                "\n*but what if we italicize?",
                MdLineFragment::Plain("here is some plaintext ")
            ))
        );
        assert_eq2!(
            parse_element_markdown_inline("\n", CheckboxParsePolicy::IgnoreCheckbox),
            Err(NomErr::Error(Error {
                input: "\n",
                code: ErrorKind::Not
            }))
        );
        assert_eq2!(
            parse_element_markdown_inline("", CheckboxParsePolicy::IgnoreCheckbox),
            Err(NomErr::Error(Error {
                input: "",
                code: ErrorKind::Eof
            }))
        );

        // Deal with checkboxes: ignore them.
        assert_eq2!(
            parse_element_markdown_inline(
                "[ ] this is a checkbox",
                CheckboxParsePolicy::IgnoreCheckbox
            ),
            Ok((" this is a checkbox", MdLineFragment::Plain("[ ]")))
        );
        assert_eq2!(
            parse_element_markdown_inline(
                "[x] this is a checkbox",
                CheckboxParsePolicy::IgnoreCheckbox
            ),
            Ok((" this is a checkbox", MdLineFragment::Plain("[x]")))
        );

        // Deal with checkboxes: parse them.
        assert_eq2!(
            parse_element_markdown_inline(
                "[ ] this is a checkbox",
                CheckboxParsePolicy::ParseCheckbox
            ),
            Ok((" this is a checkbox", MdLineFragment::Checkbox(false)))
        );
        assert_eq2!(
            parse_element_markdown_inline(
                "[x] this is a checkbox",
                CheckboxParsePolicy::ParseCheckbox
            ),
            Ok((" this is a checkbox", MdLineFragment::Checkbox(true)))
        );
    }
}